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);
|
||||
|
||||
boolean deleteUser(String name);
|
||||
boolean removeUser(String name);
|
||||
|
||||
List<String> getDefaultRoles();
|
||||
|
||||
|
@ -98,6 +98,8 @@ public interface RealmModel extends RoleContainerModel, RoleMapperModel, ScopeMa
|
|||
|
||||
ApplicationModel addApplication(String name);
|
||||
|
||||
boolean removeApplication(String id);
|
||||
|
||||
List<RequiredCredentialModel> getRequiredApplicationCredentials();
|
||||
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ import org.keycloak.models.SocialLinkModel;
|
|||
import org.keycloak.models.UserCredentialModel;
|
||||
import org.keycloak.models.UserModel;
|
||||
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.CredentialEntity;
|
||||
import org.keycloak.models.jpa.entities.OAuthClientEntity;
|
||||
|
@ -458,22 +459,23 @@ public class RealmAdapter implements RealmModel {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteUser(String name) {
|
||||
public boolean removeUser(String name) {
|
||||
TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUserByLoginName", UserEntity.class);
|
||||
query.setParameter("loginName", name);
|
||||
query.setParameter("realm", realm);
|
||||
List<UserEntity> results = query.getResultList();
|
||||
if (results.size() == 0) return false;
|
||||
|
||||
UserEntity user = results.get(0);
|
||||
|
||||
for (Class r : UserEntity.RELATIONSHIPS) {
|
||||
em.createQuery("delete from " + r.getSimpleName() + " where user = :user").setParameter("user", user).executeUpdate();
|
||||
removeUser(results.get(0));
|
||||
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);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -572,6 +574,25 @@ public class RealmAdapter implements RealmModel {
|
|||
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
|
||||
public ApplicationModel getApplicationById(String id) {
|
||||
ApplicationEntity app = em.find(ApplicationEntity.class, id);
|
||||
|
|
|
@ -34,9 +34,6 @@ import java.util.Set;
|
|||
})
|
||||
@Entity
|
||||
public class UserEntity {
|
||||
|
||||
public static final Class[] RELATIONSHIPS = new Class[] { ApplicationUserRoleMappingEntity.class, RealmUserRoleMappingEntity.class, SocialLinkEntity.class };
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
protected String id;
|
||||
|
|
|
@ -519,7 +519,7 @@ public class RealmAdapter implements RealmModel {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteUser(String name) {
|
||||
public boolean removeUser(String name) {
|
||||
User user = findPicketlinkUser(name);
|
||||
if (user == null) {
|
||||
return false;
|
||||
|
@ -628,6 +628,19 @@ public class RealmAdapter implements RealmModel {
|
|||
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
|
||||
public boolean hasRole(UserModel user, RoleModel role) {
|
||||
return SampleModel.hasRole(getRelationshipManager(), ((UserAdapter) user).getUser(), ((RoleAdapter) role).getRole());
|
||||
|
|
|
@ -54,6 +54,12 @@ public class ApplicationResource extends RoleContainerResource {
|
|||
return applicationManager.toRepresentation(application);
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@NoCache
|
||||
public void deleteApplication() {
|
||||
realm.removeApplication(application.getId());
|
||||
}
|
||||
|
||||
@Path("credentials")
|
||||
@PUT
|
||||
@Consumes("application/json")
|
||||
|
|
|
@ -111,7 +111,7 @@ public class UsersResource {
|
|||
@DELETE
|
||||
@NoCache
|
||||
public void deleteUser(final @PathParam("username") String username) {
|
||||
realm.deleteUser(username);
|
||||
realm.removeUser(username);
|
||||
}
|
||||
|
||||
@GET
|
||||
|
|
|
@ -176,7 +176,7 @@ public class AdapterTest extends AbstractKeycloakTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void deleteUser() throws Exception {
|
||||
public void testDeleteUser() throws Exception {
|
||||
test1CreateRealm();
|
||||
|
||||
UserModel user = realmModel.addUser("bburke");
|
||||
|
@ -198,11 +198,33 @@ public class AdapterTest extends AbstractKeycloakTest {
|
|||
cred.setValue("password");
|
||||
realmModel.updateCredential(user, cred);
|
||||
|
||||
Assert.assertTrue(realmModel.deleteUser("bburke"));
|
||||
Assert.assertFalse(realmModel.deleteUser("bburke"));
|
||||
Assert.assertTrue(realmModel.removeUser("bburke"));
|
||||
Assert.assertFalse(realmModel.removeUser("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
|
||||
public void testUserSearch() throws Exception {
|
||||
test1CreateRealm();
|
||||
|
|
Loading…
Reference in a new issue