KEYCLOAK-19480 Introduce MapProtocolMapperEntity

This commit is contained in:
Hynek Mlnarik 2021-10-04 12:16:40 +02:00 committed by Hynek Mlnařík
parent 00feef4dbe
commit 576292a662
4 changed files with 102 additions and 25 deletions

View file

@ -503,10 +503,35 @@ public abstract class MapClientAdapter extends AbstractClientModel<MapClientEnti
/*************** Protocol mappers ****************/ /*************** Protocol mappers ****************/
private static MapProtocolMapperEntity fromModel(ProtocolMapperModel model) {
MapProtocolMapperEntity res = new MapProtocolMapperEntityImpl();
res.setId(model.getId());
res.setName(model.getName());
res.setProtocolMapper(model.getProtocolMapper());
res.setConfig(model.getConfig());
return res;
}
private ProtocolMapperModel toModel(MapProtocolMapperEntity entity) {
ProtocolMapperModel res = new ProtocolMapperModel();
res.setId(entity.getId());
res.setName(entity.getName());
res.setProtocolMapper(entity.getProtocolMapper());
res.setConfig(entity.getConfig());
res.setProtocol(safeGetProtocol());
return res;
}
@Override @Override
public Stream<ProtocolMapperModel> getProtocolMappersStream() { public Stream<ProtocolMapperModel> getProtocolMappersStream() {
final Map<String, ProtocolMapperModel> protocolMappers = entity.getProtocolMappers(); final Map<String, MapProtocolMapperEntity> protocolMappers = entity.getProtocolMappers();
return protocolMappers == null ? Stream.empty() : protocolMappers.values().stream().distinct(); return protocolMappers == null ? Stream.empty() : protocolMappers.values().stream().distinct()
.map(this::toModel);
}
private String safeGetProtocol() {
return entity.getProtocol() == null ? "openid-connect" : entity.getProtocol();
} }
@Override @Override
@ -515,20 +540,17 @@ public abstract class MapClientAdapter extends AbstractClientModel<MapClientEnti
return null; return null;
} }
ProtocolMapperModel pm = new ProtocolMapperModel(); MapProtocolMapperEntity pm = fromModel(model);
pm.setId(KeycloakModelUtils.generateId()); if (pm.getId() == null) {
pm.setName(model.getName()); String id = KeycloakModelUtils.generateId();
pm.setProtocol(model.getProtocol()); pm.setId(id);
pm.setProtocolMapper(model.getProtocolMapper()); }
if (model.getConfig() == null) {
if (model.getConfig() != null) {
pm.setConfig(new HashMap<>(model.getConfig()));
} else {
pm.setConfig(new HashMap<>()); pm.setConfig(new HashMap<>());
} }
entity.setProtocolMapper(pm.getId(), pm); entity.setProtocolMapper(pm.getId(), pm);
return pm; return toModel(pm);
} }
@Override @Override
@ -543,20 +565,25 @@ public abstract class MapClientAdapter extends AbstractClientModel<MapClientEnti
public void updateProtocolMapper(ProtocolMapperModel mapping) { public void updateProtocolMapper(ProtocolMapperModel mapping) {
final String id = mapping == null ? null : mapping.getId(); final String id = mapping == null ? null : mapping.getId();
if (id != null) { if (id != null) {
entity.setProtocolMapper(id, mapping); entity.setProtocolMapper(id, fromModel(mapping));
} }
} }
@Override @Override
public ProtocolMapperModel getProtocolMapperById(String id) { public ProtocolMapperModel getProtocolMapperById(String id) {
return entity.getProtocolMapper(id); MapProtocolMapperEntity protocolMapper = entity.getProtocolMapper(id);
return protocolMapper == null ? null : toModel(protocolMapper);
} }
@Override @Override
public ProtocolMapperModel getProtocolMapperByName(String protocol, String name) { public ProtocolMapperModel getProtocolMapperByName(String protocol, String name) {
final Map<String, ProtocolMapperModel> protocolMappers = entity.getProtocolMappers(); final Map<String, MapProtocolMapperEntity> protocolMappers = entity.getProtocolMappers();
if (! Objects.equals(protocol, safeGetProtocol())) {
return null;
}
return protocolMappers == null ? null : protocolMappers.values().stream() return protocolMappers == null ? null : protocolMappers.values().stream()
.filter(pm -> Objects.equals(pm.getProtocol(), protocol) && Objects.equals(pm.getName(), name)) .filter(pm -> Objects.equals(pm.getName(), name))
.map(this::toModel)
.findAny() .findAny()
.orElse(null); .orElse(null);
} }

View file

@ -16,7 +16,6 @@
*/ */
package org.keycloak.models.map.client; package org.keycloak.models.map.client;
import org.keycloak.models.ProtocolMapperModel;
import org.keycloak.models.map.common.AbstractEntity; import org.keycloak.models.map.common.AbstractEntity;
import org.keycloak.models.map.common.UpdatableEntity; import org.keycloak.models.map.common.UpdatableEntity;
import java.util.Collection; import java.util.Collection;
@ -35,7 +34,7 @@ import org.keycloak.models.map.annotations.GenerateEntityImplementations;
@GenerateEntityImplementations(inherits="org.keycloak.models.map.client.MapClientEntity.AbstractClientEntity") @GenerateEntityImplementations(inherits="org.keycloak.models.map.client.MapClientEntity.AbstractClientEntity")
public interface MapClientEntity extends AbstractEntity, UpdatableEntity { public interface MapClientEntity extends AbstractEntity, UpdatableEntity {
static abstract class AbstractClientEntity implements MapClientEntity { public abstract class AbstractClientEntity implements MapClientEntity {
/** /**
* Flag signalizing that any of the setters has been meaningfully used. * Flag signalizing that any of the setters has been meaningfully used.
*/ */
@ -79,9 +78,9 @@ public interface MapClientEntity extends AbstractEntity, UpdatableEntity {
void setClientScope(String id, Boolean defaultScope); void setClientScope(String id, Boolean defaultScope);
void removeClientScope(String id); void removeClientScope(String id);
ProtocolMapperModel getProtocolMapper(String id); MapProtocolMapperEntity getProtocolMapper(String id);
Map<String, ProtocolMapperModel> getProtocolMappers(); Map<String, MapProtocolMapperEntity> getProtocolMappers();
void setProtocolMapper(String id, ProtocolMapperModel mapping); void setProtocolMapper(String id, MapProtocolMapperEntity mapping);
void removeProtocolMapper(String id); void removeProtocolMapper(String id);
void addRedirectUri(String redirectUri); void addRedirectUri(String redirectUri);

View file

@ -16,7 +16,6 @@
*/ */
package org.keycloak.models.map.client; package org.keycloak.models.map.client;
import org.keycloak.models.ProtocolMapperModel;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -164,12 +163,12 @@ public class MapClientEntityLazyDelegate implements MapClientEntity {
} }
@Override @Override
public ProtocolMapperModel getProtocolMapper(String id) { public MapProtocolMapperEntity getProtocolMapper(String id) {
return getReadDelegate().getProtocolMapper(id); return getReadDelegate().getProtocolMapper(id);
} }
@Override @Override
public Map<String,ProtocolMapperModel> getProtocolMappers() { public Map<String,MapProtocolMapperEntity> getProtocolMappers() {
return getReadDelegate().getProtocolMappers(); return getReadDelegate().getProtocolMappers();
} }
@ -459,7 +458,7 @@ public class MapClientEntityLazyDelegate implements MapClientEntity {
} }
@Override @Override
public void setProtocolMapper(String id, ProtocolMapperModel mapping) { public void setProtocolMapper(String id, MapProtocolMapperEntity mapping) {
getWriteDelegate().setProtocolMapper(id, mapping); getWriteDelegate().setProtocolMapper(id, mapping);
} }

View file

@ -0,0 +1,52 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.models.map.client;
import org.keycloak.models.map.annotations.GenerateEntityImplementations;
import org.keycloak.models.map.common.UpdatableEntity;
import java.util.Map;
/**
*
* @author hmlnarik
*/
@GenerateEntityImplementations(
inherits = "org.keycloak.models.map.client.MapProtocolMapperEntity.AbstractProtocolMapperEntity"
)
public interface MapProtocolMapperEntity extends UpdatableEntity {
public abstract class AbstractProtocolMapperEntity implements MapProtocolMapperEntity {
protected boolean updated;
@Override
public boolean isUpdated() {
return this.updated;
}
}
String getId();
void setId(String id);
String getName();
void setName(String name);
String getProtocolMapper();
void setProtocolMapper(String protocolMapper);
Map<String, String> getConfig();
void setConfig(Map<String, String> config);
}