Closes #8808 - Convert MapRoleEntity to interface

This commit is contained in:
vramik 2021-11-30 00:29:58 +01:00 committed by Hynek Mlnařík
parent b2288727e5
commit 783eecf612
4 changed files with 60 additions and 112 deletions

View file

@ -21,6 +21,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import static org.keycloak.common.util.StackUtil.getShortStackTrace; import static org.keycloak.common.util.StackUtil.getShortStackTrace;
@ -65,21 +66,23 @@ public class MapRoleAdapter extends AbstractRoleModel<MapRoleEntity> implements
@Override @Override
public boolean isComposite() { public boolean isComposite() {
return ! entity.getCompositeRoles().isEmpty(); return ! (entity.getCompositeRoles() == null || entity.getCompositeRoles().isEmpty());
} }
@Override @Override
public Stream<RoleModel> getCompositesStream() { public Stream<RoleModel> getCompositesStream() {
LOG.tracef("%% %s(%s).getCompositesStream():%d - %s", entity.getName(), entity.getId(), entity.getCompositeRoles().size(), getShortStackTrace()); Set<String> compositeRoles = entity.getCompositeRoles() == null ? Collections.emptySet() : entity.getCompositeRoles();
return entity.getCompositeRoles().stream() LOG.tracef("%% %s(%s).getCompositesStream():%d - %s", entity.getName(), entity.getId(), compositeRoles.size(), getShortStackTrace());
return compositeRoles.stream()
.map(uuid -> session.roles().getRoleById(realm, uuid)) .map(uuid -> session.roles().getRoleById(realm, uuid))
.filter(Objects::nonNull); .filter(Objects::nonNull);
} }
@Override @Override
public Stream<RoleModel> getCompositesStream(String search, Integer first, Integer max) { public Stream<RoleModel> getCompositesStream(String search, Integer first, Integer max) {
LOG.tracef("%% (%s).getCompositesStream(%s, %d, %d):%d - %s", this, search, first, max, entity.getCompositeRoles().size(), getShortStackTrace()); Set<String> compositeRoles = entity.getCompositeRoles() == null ? Collections.emptySet() : entity.getCompositeRoles();
return session.roles().getRolesStream(realm, entity.getCompositeRoles().stream(), search, first, max); LOG.tracef("%% (%s).getCompositesStream(%s, %d, %d):%d - %s", this, search, first, max, compositeRoles.size(), getShortStackTrace());
return session.roles().getRolesStream(realm, compositeRoles.stream(), search, first, max);
} }
@Override @Override
@ -96,7 +99,8 @@ public class MapRoleAdapter extends AbstractRoleModel<MapRoleEntity> implements
@Override @Override
public boolean isClientRole() { public boolean isClientRole() {
return entity.isClientRole(); final Boolean clientRole = entity.isClientRole();
return clientRole == null ? false : clientRole;
} }
@Override @Override
@ -126,7 +130,8 @@ public class MapRoleAdapter extends AbstractRoleModel<MapRoleEntity> implements
@Override @Override
public Map<String, List<String>> getAttributes() { public Map<String, List<String>> getAttributes() {
return entity.getAttributes(); Map<String, List<String>> attributes = entity.getAttributes();
return attributes == null ? Collections.emptyMap() : attributes;
} }
@Override @Override

View file

@ -16,37 +16,23 @@
*/ */
package org.keycloak.models.map.role; package org.keycloak.models.map.role;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import org.keycloak.models.map.annotations.GenerateEntityImplementations;
import org.keycloak.models.map.common.AbstractEntity; import org.keycloak.models.map.common.AbstractEntity;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.common.UpdatableEntity; import org.keycloak.models.map.common.UpdatableEntity;
public class MapRoleEntity extends UpdatableEntity.Impl implements AbstractEntity { @GenerateEntityImplementations(
inherits = "org.keycloak.models.map.role.MapRoleEntity.AbstractRoleEntity"
)
@DeepCloner.Root
public interface MapRoleEntity extends AbstractEntity, UpdatableEntity {
public abstract class AbstractRoleEntity extends UpdatableEntity.Impl implements MapRoleEntity {
private String id; private String id;
private String realmId;
private String name;
private String description;
private boolean clientRole;
private String clientId;
private Set<String> compositeRoles = new HashSet<>();
private Map<String, List<String>> attributes = new HashMap<>();
/**
* Flag signalizing that any of the setters has been meaningfully used.
*/
public MapRoleEntity() {}
public MapRoleEntity(String id, String realmId) {
this.id = id;
this.realmId = realmId;
}
@Override @Override
public String getId() { public String getId() {
@ -60,87 +46,40 @@ public class MapRoleEntity extends UpdatableEntity.Impl implements AbstractEntit
this.updated |= id != null; this.updated |= id != null;
} }
public String getName() {
return name;
} }
public void setName(String name) { default Boolean isComposite() {
this.updated |= ! Objects.equals(this.name, name); return ! (getCompositeRoles() == null || getCompositeRoles().isEmpty());
this.name = name;
} }
public String getDescription() { Boolean isClientRole();
return description;
}
public void setDescription(String description) { String getRealmId();
this.updated |= ! Objects.equals(this.description, description);
this.description = description;
}
public Map<String, List<String>> getAttributes() { String getClientId();
return attributes;
}
public void setAttributes(Map<String, List<String>> attributes) { String getName();
this.updated |= ! Objects.equals(this.attributes, attributes);
this.attributes = attributes;
}
public void setAttribute(String name, List<String> values) { String getDescription();
this.updated |= ! Objects.equals(this.attributes.put(name, values), values);
}
public void removeAttribute(String name) { void setClientRole(Boolean clientRole);
this.updated |= this.attributes.remove(name) != null;
}
public String getRealmId() { void setRealmId(String realmId);
return realmId;
}
public void setRealmId(String realmId) { void setClientId(String clientId);
this.updated |= ! Objects.equals(this.realmId, realmId);
this.realmId = realmId;
}
public boolean isClientRole() { void setName(String name);
return clientRole;
}
public void setClientRole(boolean clientRole) { void setDescription(String description);
this.updated |= ! Objects.equals(this.clientRole, clientRole);
this.clientRole = clientRole;
}
public boolean isComposite() { Set<String> getCompositeRoles();
return ! (compositeRoles == null || compositeRoles.isEmpty()); void setCompositeRoles(Set<String> compositeRoles);
} void addCompositeRole(String roleId);
void removeCompositeRole(String roleId);
public Set<String> getCompositeRoles() { Map<String, List<String>> getAttributes();
return compositeRoles; void setAttributes(Map<String, List<String>> attributes);
} void setAttribute(String name, List<String> values);
void removeAttribute(String name);
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.updated |= ! Objects.equals(this.clientId, clientId);
this.clientId = clientId;
}
public void setCompositeRoles(Set<String> compositeRoles) {
this.updated |= ! Objects.equals(this.compositeRoles, compositeRoles);
this.compositeRoles.clear();
this.compositeRoles.addAll(compositeRoles);
}
public void addCompositeRole(String roleId) {
this.updated |= this.compositeRoles.add(roleId);
}
public void removeCompositeRole(String roleId) {
this.updated |= this.compositeRoles.remove(roleId);
}
} }

View file

@ -66,9 +66,10 @@ public class MapRoleProvider implements RoleProvider {
LOG.tracef("addRealmRole(%s, %s, %s)%s", realm, id, name, getShortStackTrace()); LOG.tracef("addRealmRole(%s, %s, %s)%s", realm, id, name, getShortStackTrace());
MapRoleEntity entity = new MapRoleEntity(id, realm.getId()); MapRoleEntity entity = new MapRoleEntityImpl();
entity.setName(name); entity.setId(id);
entity.setRealmId(realm.getId()); entity.setRealmId(realm.getId());
entity.setName(name);
if (tx.read(entity.getId()) != null) { if (tx.read(entity.getId()) != null) {
throw new ModelDuplicateException("Role exists: " + id); throw new ModelDuplicateException("Role exists: " + id);
} }
@ -121,7 +122,9 @@ public class MapRoleProvider implements RoleProvider {
LOG.tracef("addClientRole(%s, %s, %s)%s", client, id, name, getShortStackTrace()); LOG.tracef("addClientRole(%s, %s, %s)%s", client, id, name, getShortStackTrace());
MapRoleEntity entity = new MapRoleEntity(id, client.getRealm().getId()); MapRoleEntity entity = new MapRoleEntityImpl();
entity.setId(id);
entity.setRealmId(client.getRealm().getId());
entity.setName(name); entity.setName(name);
entity.setClientRole(true); entity.setClientRole(true);
entity.setClientId(client.getId()); entity.setClientId(client.getId());

View file

@ -56,6 +56,7 @@ import org.keycloak.models.map.group.MapGroupEntityImpl;
import org.keycloak.models.map.loginFailure.MapUserLoginFailureEntity; import org.keycloak.models.map.loginFailure.MapUserLoginFailureEntity;
import org.keycloak.models.map.realm.MapRealmEntity; import org.keycloak.models.map.realm.MapRealmEntity;
import org.keycloak.models.map.role.MapRoleEntity; import org.keycloak.models.map.role.MapRoleEntity;
import org.keycloak.models.map.role.MapRoleEntityImpl;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -106,6 +107,7 @@ public class ConcurrentHashMapStorageProviderFactory implements AmphibianProvide
.constructorDC(MapClientEntityImpl.class, MapClientEntityImpl::new) .constructorDC(MapClientEntityImpl.class, MapClientEntityImpl::new)
.constructor(MapProtocolMapperEntity.class, MapProtocolMapperEntityImpl::new) .constructor(MapProtocolMapperEntity.class, MapProtocolMapperEntityImpl::new)
.constructor(MapGroupEntityImpl.class, MapGroupEntityImpl::new) .constructor(MapGroupEntityImpl.class, MapGroupEntityImpl::new)
.constructor(MapRoleEntityImpl.class, MapRoleEntityImpl::new)
.build(); .build();
public static final Map<Class<?>, String> MODEL_TO_NAME = new HashMap<>(); public static final Map<Class<?>, String> MODEL_TO_NAME = new HashMap<>();
@ -154,10 +156,9 @@ public class ConcurrentHashMapStorageProviderFactory implements AmphibianProvide
static { static {
INTERFACE_TO_IMPL.put(MapClientEntity.class, MapClientEntityImpl.class); INTERFACE_TO_IMPL.put(MapClientEntity.class, MapClientEntityImpl.class);
// INTERFACE_TO_IMPL.put(MapClientScopeEntity.class, MapClientScopeEntityImpl.class); // INTERFACE_TO_IMPL.put(MapClientScopeEntity.class, MapClientScopeEntityImpl.class);
// INTERFACE_TO_IMPL.put(MapClientEntity.class, MapClientEntityImpl.class);
INTERFACE_TO_IMPL.put(MapGroupEntity.class, MapGroupEntityImpl.class); INTERFACE_TO_IMPL.put(MapGroupEntity.class, MapGroupEntityImpl.class);
// INTERFACE_TO_IMPL.put(MapRealmEntity.class, MapRealmEntityImpl.class); // INTERFACE_TO_IMPL.put(MapRealmEntity.class, MapRealmEntityImpl.class);
// INTERFACE_TO_IMPL.put(MapRoleEntity.class, MapRoleEntityImpl.class); INTERFACE_TO_IMPL.put(MapRoleEntity.class, MapRoleEntityImpl.class);
// INTERFACE_TO_IMPL.put(MapRootAuthenticationSessionEntity.class, MapRootAuthenticationSessionEntityImpl.class); // INTERFACE_TO_IMPL.put(MapRootAuthenticationSessionEntity.class, MapRootAuthenticationSessionEntityImpl.class);
// INTERFACE_TO_IMPL.put(MapUserLoginFailureEntity.class, MapUserLoginFailureEntityImpl.class); // INTERFACE_TO_IMPL.put(MapUserLoginFailureEntity.class, MapUserLoginFailureEntityImpl.class);
// INTERFACE_TO_IMPL.put(MapUserEntity.class, MapUserEntityImpl.class); // INTERFACE_TO_IMPL.put(MapUserEntity.class, MapUserEntityImpl.class);