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 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;
|
||||||
}
|
}
|
||||||
|
Set<String> attrsToRemove = getAttributes().keySet();
|
||||||
@Override
|
attrsToRemove.removeAll(attributes.keySet());
|
||||||
public void setAttribute(String name, List<String> values) {
|
attrsToRemove.forEach(group::removeAttribute);
|
||||||
getGroup().setAttribute(name, values);
|
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
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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,17 +184,9 @@ public class OrganizationResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
model.setName(rep.getName());
|
model.setName(rep.getName());
|
||||||
|
model.setAttributes(rep.getAttributes());
|
||||||
if (rep.getAttributes() != null) {
|
model.setDomains(Optional.ofNullable(rep.getDomains()).orElse(Set.of()).stream()
|
||||||
Set<String> attrsToRemove = model.getAttributes().keySet();
|
.filter(this::validateDomainRepresentation)
|
||||||
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 -> {
|
.peek(domainRep -> {
|
||||||
OrganizationModel orgModel = provider.getByDomainName(domainRep.getName());
|
OrganizationModel orgModel = provider.getByDomainName(domainRep.getName());
|
||||||
if (orgModel != null && !Objects.equals(model.getId(), orgModel.getId())) {
|
if (orgModel != null && !Objects.equals(model.getId(), orgModel.getId())) {
|
||||||
|
@ -201,7 +195,7 @@ public class OrganizationResource {
|
||||||
})
|
})
|
||||||
.map(this::toModel)
|
.map(this::toModel)
|
||||||
.collect(Collectors.toSet()));
|
.collect(Collectors.toSet()));
|
||||||
}
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue