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

View file

@ -16,37 +16,23 @@
*/
package org.keycloak.models.map.role;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.keycloak.models.map.annotations.GenerateEntityImplementations;
import org.keycloak.models.map.common.AbstractEntity;
import org.keycloak.models.map.common.DeepCloner;
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 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
public String getId() {
@ -60,87 +46,40 @@ public class MapRoleEntity extends UpdatableEntity.Impl implements AbstractEntit
this.updated |= id != null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.updated |= ! Objects.equals(this.name, name);
this.name = name;
default Boolean isComposite() {
return ! (getCompositeRoles() == null || getCompositeRoles().isEmpty());
}
public String getDescription() {
return description;
}
Boolean isClientRole();
public void setDescription(String description) {
this.updated |= ! Objects.equals(this.description, description);
this.description = description;
}
String getRealmId();
public Map<String, List<String>> getAttributes() {
return attributes;
}
String getClientId();
public void setAttributes(Map<String, List<String>> attributes) {
this.updated |= ! Objects.equals(this.attributes, attributes);
this.attributes = attributes;
}
String getName();
public void setAttribute(String name, List<String> values) {
this.updated |= ! Objects.equals(this.attributes.put(name, values), values);
}
String getDescription();
public void removeAttribute(String name) {
this.updated |= this.attributes.remove(name) != null;
}
void setClientRole(Boolean clientRole);
public String getRealmId() {
return realmId;
}
void setRealmId(String realmId);
public void setRealmId(String realmId) {
this.updated |= ! Objects.equals(this.realmId, realmId);
this.realmId = realmId;
}
void setClientId(String clientId);
public boolean isClientRole() {
return clientRole;
}
void setName(String name);
public void setClientRole(boolean clientRole) {
this.updated |= ! Objects.equals(this.clientRole, clientRole);
this.clientRole = clientRole;
}
void setDescription(String description);
public boolean isComposite() {
return ! (compositeRoles == null || compositeRoles.isEmpty());
}
Set<String> getCompositeRoles();
void setCompositeRoles(Set<String> compositeRoles);
void addCompositeRole(String roleId);
void removeCompositeRole(String roleId);
public Set<String> getCompositeRoles() {
return compositeRoles;
}
Map<String, List<String>> getAttributes();
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());
MapRoleEntity entity = new MapRoleEntity(id, realm.getId());
entity.setName(name);
MapRoleEntity entity = new MapRoleEntityImpl();
entity.setId(id);
entity.setRealmId(realm.getId());
entity.setName(name);
if (tx.read(entity.getId()) != null) {
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());
MapRoleEntity entity = new MapRoleEntity(id, client.getRealm().getId());
MapRoleEntity entity = new MapRoleEntityImpl();
entity.setId(id);
entity.setRealmId(client.getRealm().getId());
entity.setName(name);
entity.setClientRole(true);
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.realm.MapRealmEntity;
import org.keycloak.models.map.role.MapRoleEntity;
import org.keycloak.models.map.role.MapRoleEntityImpl;
import com.fasterxml.jackson.databind.JavaType;
import java.io.File;
import java.io.IOException;
@ -106,6 +107,7 @@ public class ConcurrentHashMapStorageProviderFactory implements AmphibianProvide
.constructorDC(MapClientEntityImpl.class, MapClientEntityImpl::new)
.constructor(MapProtocolMapperEntity.class, MapProtocolMapperEntityImpl::new)
.constructor(MapGroupEntityImpl.class, MapGroupEntityImpl::new)
.constructor(MapRoleEntityImpl.class, MapRoleEntityImpl::new)
.build();
public static final Map<Class<?>, String> MODEL_TO_NAME = new HashMap<>();
@ -154,10 +156,9 @@ public class ConcurrentHashMapStorageProviderFactory implements AmphibianProvide
static {
INTERFACE_TO_IMPL.put(MapClientEntity.class, MapClientEntityImpl.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(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(MapUserLoginFailureEntity.class, MapUserLoginFailureEntityImpl.class);
// INTERFACE_TO_IMPL.put(MapUserEntity.class, MapUserEntityImpl.class);