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:
Pedro Igor 2024-04-11 14:15:16 -03:00
parent 74faddec8e
commit 8f8094408e
3 changed files with 24 additions and 51 deletions

View file

@ -20,6 +20,7 @@ package org.keycloak.organization.jpa;
import org.keycloak.models.GroupModel; import org.keycloak.models.GroupModel;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -68,28 +69,14 @@ public final class OrganizationAdapter implements OrganizationModel, JpaModel<Or
} }
@Override @Override
public void setSingleAttribute(String name, String value) { public void setAttributes(Map<String, List<String>> attributes) {
getGroup().setSingleAttribute(name, value); if (attributes == null) {
} return;
}
@Override Set<String> attrsToRemove = getAttributes().keySet();
public void setAttribute(String name, List<String> values) { attrsToRemove.removeAll(attributes.keySet());
getGroup().setAttribute(name, values); attrsToRemove.forEach(group::removeAttribute);
} attributes.forEach(group::setAttribute);
@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);
} }
@Override @Override

View file

@ -32,18 +32,10 @@ public interface OrganizationModel {
String getName(); 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(); Map<String, List<String>> getAttributes();
void setAttributes(Map<String, List<String>> attributes);
Stream<OrganizationDomainModel> getDomains(); Stream<OrganizationDomainModel> getDomains();
void setDomains(Collection<OrganizationDomainModel> domains); void setDomains(Collection<OrganizationDomainModel> domains);

View file

@ -17,6 +17,8 @@
package org.keycloak.organization.admin.resource; package org.keycloak.organization.admin.resource;
import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -182,26 +184,18 @@ public class OrganizationResource {
} }
model.setName(rep.getName()); model.setName(rep.getName());
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())) {
throw ErrorResponse.error("Domain " + domainRep.getName() + " is already linked to another organization", Response.Status.BAD_REQUEST);
}
})
.map(this::toModel)
.collect(Collectors.toSet()));
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)
.peek(domainRep -> {
OrganizationModel orgModel = provider.getByDomainName(domainRep.getName());
if (orgModel != null && !Objects.equals(model.getId(), orgModel.getId())) {
throw ErrorResponse.error("Domain " + domainRep.getName() + " is already linked to another organization", Response.Status.BAD_REQUEST);
}
})
.map(this::toModel)
.collect(Collectors.toSet()));
}
return model; return model;
} }