Fix realm removal when orgs are enabled

Signed-off-by: Stefan Guilhen <sguilhen@redhat.com>
This commit is contained in:
Stefan Guilhen 2024-05-10 12:15:14 -03:00 committed by Pedro Igor
parent f0620353a4
commit 3186b6db8e

View file

@ -107,13 +107,14 @@ public class JpaOrganizationProvider implements OrganizationProvider {
@Override @Override
public boolean remove(OrganizationModel organization) { public boolean remove(OrganizationModel organization) {
OrganizationEntity entity = getEntity(organization.getId()); OrganizationEntity entity = getEntity(organization.getId());
GroupModel group = getOrganizationGroup(entity);
GroupModel group = getOrganizationGroup(organization); if (group != null) {
//TODO: won't scale, requires a better mechanism for bulk deleting users //TODO: won't scale, requires a better mechanism for bulk deleting users
userProvider.getGroupMembersStream(realm, group).forEach(userModel -> removeMember(organization, userModel)); userProvider.getGroupMembersStream(realm, group).forEach(userModel -> removeMember(organization, userModel));
groupProvider.removeGroup(realm, group); groupProvider.removeGroup(realm, group);
organization.getIdentityProviders().forEach((model) -> removeIdentityProvider(organization, model)); organization.getIdentityProviders().forEach((model) -> removeIdentityProvider(organization, model));
}
em.remove(entity); em.remove(entity);
@ -132,7 +133,7 @@ public class JpaOrganizationProvider implements OrganizationProvider {
throwExceptionIfObjectIsNull(user, "User"); throwExceptionIfObjectIsNull(user, "User");
OrganizationEntity entity = getEntity(organization.getId()); OrganizationEntity entity = getEntity(organization.getId());
GroupModel group = groupProvider.getGroupById(realm, entity.getGroupId()); GroupModel group = getOrganizationGroup(entity);
if (user.isMemberOf(group)) { if (user.isMemberOf(group)) {
return false; return false;
@ -384,8 +385,7 @@ public class JpaOrganizationProvider implements OrganizationProvider {
private GroupModel getOrganizationGroup(OrganizationModel organization) { private GroupModel getOrganizationGroup(OrganizationModel organization) {
throwExceptionIfObjectIsNull(organization, "Organization"); throwExceptionIfObjectIsNull(organization, "Organization");
OrganizationEntity entity = getEntity(organization.getId()); OrganizationEntity entity = getEntity(organization.getId());
GroupModel group = getOrganizationGroup(entity);
GroupModel group = groupProvider.getGroupById(realm, entity.getGroupId());
if (group == null) { if (group == null) {
throw new ModelException("Organization group " + entity.getGroupId() + " not found"); throw new ModelException("Organization group " + entity.getGroupId() + " not found");
@ -394,6 +394,10 @@ public class JpaOrganizationProvider implements OrganizationProvider {
return group; return group;
} }
private GroupModel getOrganizationGroup(OrganizationEntity entity) {
return groupProvider.getGroupById(realm, entity.getGroupId());
}
private void throwExceptionIfObjectIsNull(Object object, String objectName) { private void throwExceptionIfObjectIsNull(Object object, String objectName) {
if (object == null) { if (object == null) {
throw new ModelException(String.format("%s cannot be null", objectName)); throw new ModelException(String.format("%s cannot be null", objectName));