KEYCLOAK-105
This commit is contained in:
parent
cd314acb27
commit
219c0efbaf
7 changed files with 80 additions and 19 deletions
|
@ -84,7 +84,7 @@ public interface RealmModel extends RoleContainerModel, RoleMapperModel, ScopeMa
|
||||||
|
|
||||||
UserModel addUser(String username);
|
UserModel addUser(String username);
|
||||||
|
|
||||||
boolean deleteUser(String name);
|
boolean removeUser(String name);
|
||||||
|
|
||||||
List<String> getDefaultRoles();
|
List<String> getDefaultRoles();
|
||||||
|
|
||||||
|
@ -98,6 +98,8 @@ public interface RealmModel extends RoleContainerModel, RoleMapperModel, ScopeMa
|
||||||
|
|
||||||
ApplicationModel addApplication(String name);
|
ApplicationModel addApplication(String name);
|
||||||
|
|
||||||
|
boolean removeApplication(String id);
|
||||||
|
|
||||||
List<RequiredCredentialModel> getRequiredApplicationCredentials();
|
List<RequiredCredentialModel> getRequiredApplicationCredentials();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import org.keycloak.models.SocialLinkModel;
|
||||||
import org.keycloak.models.UserCredentialModel;
|
import org.keycloak.models.UserCredentialModel;
|
||||||
import org.keycloak.models.UserModel;
|
import org.keycloak.models.UserModel;
|
||||||
import org.keycloak.models.jpa.entities.ApplicationEntity;
|
import org.keycloak.models.jpa.entities.ApplicationEntity;
|
||||||
|
import org.keycloak.models.jpa.entities.ApplicationScopeMappingEntity;
|
||||||
import org.keycloak.models.jpa.entities.ApplicationUserRoleMappingEntity;
|
import org.keycloak.models.jpa.entities.ApplicationUserRoleMappingEntity;
|
||||||
import org.keycloak.models.jpa.entities.CredentialEntity;
|
import org.keycloak.models.jpa.entities.CredentialEntity;
|
||||||
import org.keycloak.models.jpa.entities.OAuthClientEntity;
|
import org.keycloak.models.jpa.entities.OAuthClientEntity;
|
||||||
|
@ -458,24 +459,25 @@ public class RealmAdapter implements RealmModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteUser(String name) {
|
public boolean removeUser(String name) {
|
||||||
TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUserByLoginName", UserEntity.class);
|
TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUserByLoginName", UserEntity.class);
|
||||||
query.setParameter("loginName", name);
|
query.setParameter("loginName", name);
|
||||||
query.setParameter("realm", realm);
|
query.setParameter("realm", realm);
|
||||||
List<UserEntity> results = query.getResultList();
|
List<UserEntity> results = query.getResultList();
|
||||||
if (results.size() == 0) return false;
|
if (results.size() == 0) return false;
|
||||||
|
removeUser(results.get(0));
|
||||||
UserEntity user = results.get(0);
|
|
||||||
|
|
||||||
for (Class r : UserEntity.RELATIONSHIPS) {
|
|
||||||
em.createQuery("delete from " + r.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
em.remove(user);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void removeUser(UserEntity user) {
|
||||||
|
em.createQuery("delete from " + ApplicationScopeMappingEntity.class.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
|
||||||
|
em.createQuery("delete from " + ApplicationUserRoleMappingEntity.class.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
|
||||||
|
em.createQuery("delete from " + RealmScopeMappingEntity.class.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
|
||||||
|
em.createQuery("delete from " + RealmUserRoleMappingEntity.class.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
|
||||||
|
em.createQuery("delete from " + SocialLinkEntity.class.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
|
||||||
|
em.remove(user);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getDefaultRoles() {
|
public List<String> getDefaultRoles() {
|
||||||
Collection<RoleEntity> entities = realm.getDefaultRoles();
|
Collection<RoleEntity> entities = realm.getDefaultRoles();
|
||||||
|
@ -572,6 +574,25 @@ public class RealmAdapter implements RealmModel {
|
||||||
return resource;
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeApplication(String id) {
|
||||||
|
ApplicationEntity application = null;
|
||||||
|
for (ApplicationEntity a : realm.getApplications()) {
|
||||||
|
if (a.getId().equals(id)) {
|
||||||
|
application = a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (application == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
realm.getApplications().remove(application);
|
||||||
|
removeUser(application.getApplicationUser());
|
||||||
|
em.createQuery("delete from " + ApplicationScopeMappingEntity.class.getSimpleName() + " where application = :application").setParameter("application", application).executeUpdate();
|
||||||
|
em.createQuery("delete from " + ApplicationUserRoleMappingEntity.class.getSimpleName() + " where application = :application").setParameter("application", application).executeUpdate();
|
||||||
|
em.remove(application);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApplicationModel getApplicationById(String id) {
|
public ApplicationModel getApplicationById(String id) {
|
||||||
ApplicationEntity app = em.find(ApplicationEntity.class, id);
|
ApplicationEntity app = em.find(ApplicationEntity.class, id);
|
||||||
|
|
|
@ -34,9 +34,6 @@ import java.util.Set;
|
||||||
})
|
})
|
||||||
@Entity
|
@Entity
|
||||||
public class UserEntity {
|
public class UserEntity {
|
||||||
|
|
||||||
public static final Class[] RELATIONSHIPS = new Class[] { ApplicationUserRoleMappingEntity.class, RealmUserRoleMappingEntity.class, SocialLinkEntity.class };
|
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
protected String id;
|
protected String id;
|
||||||
|
|
|
@ -519,7 +519,7 @@ public class RealmAdapter implements RealmModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteUser(String name) {
|
public boolean removeUser(String name) {
|
||||||
User user = findPicketlinkUser(name);
|
User user = findPicketlinkUser(name);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -628,6 +628,19 @@ public class RealmAdapter implements RealmModel {
|
||||||
return resource;
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeApplication(String id) {
|
||||||
|
RelationshipQuery<ApplicationRelationship> query = getRelationshipManager().createRelationshipQuery(ApplicationRelationship.class);
|
||||||
|
query.setParameter(ApplicationRelationship.REALM, realm.getName());
|
||||||
|
query.setParameter(ApplicationRelationship.APPLICATION, id);
|
||||||
|
List<ApplicationRelationship> results = query.getResultList();
|
||||||
|
if (results.size() == 0) return false;
|
||||||
|
ApplicationRelationship relationship = results.get(0);
|
||||||
|
ApplicationData application = partitionManager.getPartition(ApplicationData.class, relationship.getApplication());
|
||||||
|
partitionManager.remove(application);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasRole(UserModel user, RoleModel role) {
|
public boolean hasRole(UserModel user, RoleModel role) {
|
||||||
return SampleModel.hasRole(getRelationshipManager(), ((UserAdapter) user).getUser(), ((RoleAdapter) role).getRole());
|
return SampleModel.hasRole(getRelationshipManager(), ((UserAdapter) user).getUser(), ((RoleAdapter) role).getRole());
|
||||||
|
|
|
@ -54,6 +54,12 @@ public class ApplicationResource extends RoleContainerResource {
|
||||||
return applicationManager.toRepresentation(application);
|
return applicationManager.toRepresentation(application);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DELETE
|
||||||
|
@NoCache
|
||||||
|
public void deleteApplication() {
|
||||||
|
realm.removeApplication(application.getId());
|
||||||
|
}
|
||||||
|
|
||||||
@Path("credentials")
|
@Path("credentials")
|
||||||
@PUT
|
@PUT
|
||||||
@Consumes("application/json")
|
@Consumes("application/json")
|
||||||
|
|
|
@ -111,7 +111,7 @@ public class UsersResource {
|
||||||
@DELETE
|
@DELETE
|
||||||
@NoCache
|
@NoCache
|
||||||
public void deleteUser(final @PathParam("username") String username) {
|
public void deleteUser(final @PathParam("username") String username) {
|
||||||
realm.deleteUser(username);
|
realm.removeUser(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
|
|
|
@ -176,7 +176,7 @@ public class AdapterTest extends AbstractKeycloakTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void deleteUser() throws Exception {
|
public void testDeleteUser() throws Exception {
|
||||||
test1CreateRealm();
|
test1CreateRealm();
|
||||||
|
|
||||||
UserModel user = realmModel.addUser("bburke");
|
UserModel user = realmModel.addUser("bburke");
|
||||||
|
@ -198,11 +198,33 @@ public class AdapterTest extends AbstractKeycloakTest {
|
||||||
cred.setValue("password");
|
cred.setValue("password");
|
||||||
realmModel.updateCredential(user, cred);
|
realmModel.updateCredential(user, cred);
|
||||||
|
|
||||||
Assert.assertTrue(realmModel.deleteUser("bburke"));
|
Assert.assertTrue(realmModel.removeUser("bburke"));
|
||||||
Assert.assertFalse(realmModel.deleteUser("bburke"));
|
Assert.assertFalse(realmModel.removeUser("bburke"));
|
||||||
Assert.assertNull(realmModel.getUser("bburke"));
|
Assert.assertNull(realmModel.getUser("bburke"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemoveApplication() throws Exception {
|
||||||
|
test1CreateRealm();
|
||||||
|
|
||||||
|
UserModel user = realmModel.addUser("bburke");
|
||||||
|
|
||||||
|
OAuthClientModel client = realmModel.addOAuthClient("client");
|
||||||
|
|
||||||
|
ApplicationModel app = realmModel.addApplication("test-app");
|
||||||
|
|
||||||
|
RoleModel appRole = app.addRole("test");
|
||||||
|
app.grantRole(user, appRole);
|
||||||
|
app.addScopeMapping(client.getOAuthAgent(), appRole);
|
||||||
|
|
||||||
|
RoleModel realmRole = realmModel.addRole("test");
|
||||||
|
realmModel.addScopeMapping(app.getApplicationUser(), realmRole);
|
||||||
|
|
||||||
|
Assert.assertTrue(realmModel.removeApplication(app.getId()));
|
||||||
|
Assert.assertFalse(realmModel.removeApplication(app.getId()));
|
||||||
|
Assert.assertNull(realmModel.getApplicationById(app.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUserSearch() throws Exception {
|
public void testUserSearch() throws Exception {
|
||||||
test1CreateRealm();
|
test1CreateRealm();
|
||||||
|
|
Loading…
Reference in a new issue