Encapsulate the logic to set attributes into the domain model
Closes #28646 Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
This commit is contained in:
parent
74faddec8e
commit
8f8094408e
3 changed files with 24 additions and 51 deletions
|
@ -20,6 +20,7 @@ package org.keycloak.organization.jpa;
|
|||
import org.keycloak.models.GroupModel;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
@ -68,28 +69,14 @@ public final class OrganizationAdapter implements OrganizationModel, JpaModel<Or
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setSingleAttribute(String name, String value) {
|
||||
getGroup().setSingleAttribute(name, value);
|
||||
public void setAttributes(Map<String, List<String>> attributes) {
|
||||
if (attributes == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, List<String> values) {
|
||||
getGroup().setAttribute(name, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttribute(String name) {
|
||||
getGroup().removeAttribute(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFirstAttribute(String name) {
|
||||
return getGroup().getFirstAttribute(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<String> getAttributeStream(String name) {
|
||||
return getGroup().getAttributeStream(name);
|
||||
Set<String> attrsToRemove = getAttributes().keySet();
|
||||
attrsToRemove.removeAll(attributes.keySet());
|
||||
attrsToRemove.forEach(group::removeAttribute);
|
||||
attributes.forEach(group::setAttribute);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -32,18 +32,10 @@ public interface OrganizationModel {
|
|||
|
||||
String getName();
|
||||
|
||||
void setSingleAttribute(String name, String value);
|
||||
|
||||
void setAttribute(String name, List<String> values);
|
||||
|
||||
void removeAttribute(String name);
|
||||
|
||||
String getFirstAttribute(String name);
|
||||
|
||||
Stream<String> getAttributeStream(String name);
|
||||
|
||||
Map<String, List<String>> getAttributes();
|
||||
|
||||
void setAttributes(Map<String, List<String>> attributes);
|
||||
|
||||
Stream<OrganizationDomainModel> getDomains();
|
||||
|
||||
void setDomains(Collection<OrganizationDomainModel> domains);
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.keycloak.organization.admin.resource;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -182,17 +184,9 @@ public class OrganizationResource {
|
|||
}
|
||||
|
||||
model.setName(rep.getName());
|
||||
|
||||
if (rep.getAttributes() != null) {
|
||||
Set<String> attrsToRemove = model.getAttributes().keySet();
|
||||
attrsToRemove.removeAll(rep.getAttributes().keySet());
|
||||
attrsToRemove.forEach(model::removeAttribute);
|
||||
|
||||
rep.getAttributes().entrySet().forEach(entry -> model.setAttribute(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
if (rep.getDomains() != null) {
|
||||
model.setDomains(rep.getDomains().stream().filter(this::validateDomainRepresentation)
|
||||
model.setAttributes(rep.getAttributes());
|
||||
model.setDomains(Optional.ofNullable(rep.getDomains()).orElse(Set.of()).stream()
|
||||
.filter(this::validateDomainRepresentation)
|
||||
.peek(domainRep -> {
|
||||
OrganizationModel orgModel = provider.getByDomainName(domainRep.getName());
|
||||
if (orgModel != null && !Objects.equals(model.getId(), orgModel.getId())) {
|
||||
|
@ -201,7 +195,7 @@ public class OrganizationResource {
|
|||
})
|
||||
.map(this::toModel)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue