All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.xlrit.gears.plugin.scim.UserHandler Maven / Gradle / Ivy
package com.xlrit.gears.plugin.scim;
import static com.xlrit.gears.plugin.scim.Helper.*;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xlrit.gears.base.repository.UserRepository;
import com.xlrit.gears.engine.util.EngineUtils;
import de.captaingoldfish.scim.sdk.common.constants.enums.SortOrder;
import de.captaingoldfish.scim.sdk.common.exceptions.ResourceNotFoundException;
import de.captaingoldfish.scim.sdk.common.resources.User;
import de.captaingoldfish.scim.sdk.common.resources.complex.Meta;
import de.captaingoldfish.scim.sdk.common.resources.complex.Name;
import de.captaingoldfish.scim.sdk.common.resources.multicomplex.Email;
import de.captaingoldfish.scim.sdk.common.resources.multicomplex.MultiComplexNode;
import de.captaingoldfish.scim.sdk.common.schemas.SchemaAttribute;
import de.captaingoldfish.scim.sdk.server.endpoints.Context;
import de.captaingoldfish.scim.sdk.server.endpoints.ResourceHandler;
import de.captaingoldfish.scim.sdk.server.filter.FilterNode;
import de.captaingoldfish.scim.sdk.server.response.PartialListResponse;
public class UserHandler extends ResourceHandler {
private static final Logger LOG = LoggerFactory.getLogger(UserHandler.class);
private final UserRepository users;
public UserHandler(UserRepository users) {
this.users = users;
}
@Override
public User createResource(User provUser, Context context) {
LOG.info("createResource: user={}", provUser);
String id = provUser.getExternalId().map(EngineUtils::uuidToUlid).orElseThrow();
LOG.debug("createResource: externalId '{}' converted to id '{}'", provUser.getExternalId().get(), id);
var nativeUser = users.create(id);
OffsetDateTime now = OffsetDateTime.now();
nativeUser.setCreatedAt(now); // TODO is dit nog nodig ivm AuditEntityListener?
nativeUser.setModifiedAt(now);
copy(provUser, nativeUser);
LOG.info("result: {}", nativeUser);
return convert(nativeUser);
}
@Override
public User getResource(String id, List attributes, List excludedAttributes, Context context) {
LOG.info("getResource: id={}, attributes={}, excludedAttributes={}", id, attributes, excludedAttributes);
var nativeUser = users.findById(id);
if (nativeUser == null) throw new ResourceNotFoundException("User not found");
LOG.info("result: {}", nativeUser);
return convert(nativeUser);
}
@Override
public PartialListResponse listResources(long startIndex, int count, FilterNode filter, SchemaAttribute sortBy, SortOrder sortOrder, List attributes, List excludedAttributes, Context context) {
LOG.info("listResources: start={}, count={}, filter={}, sort={} {}, attrs={}", startIndex, count, filter, sortBy, sortOrder, attributes);
// TODO consider supporting native filtering
var nativeUsers = users.findAll();
return toResponse(nativeUsers);
}
private PartialListResponse toResponse(List users) {
var list = users.stream()
.map(this::convert)
.collect(Collectors.toList());
return createResponse(list);
}
@Override
public User updateResource(User provUser, Context context) {
LOG.info("updateResource: user={}", provUser);
var nativeUser = provUser.getId().map(users::findById).orElse(null);
if (nativeUser == null) throw new ResourceNotFoundException("User not found");
copy(provUser, nativeUser);
LOG.info("result: {}", nativeUser);
return convert(nativeUser);
}
@Override
public void deleteResource(String id, Context context) {
LOG.info("deleteResource: user={}", id);
var nativeUser = users.findById(id);
if (nativeUser == null) throw new ResourceNotFoundException("User not found");
nativeUser.setActive(false);
}
private void copy(User provUser, com.xlrit.gears.base.model.User nativeUser) {
provUser.getUserName().ifPresent(nativeUser::setUsername);
provUser.getExternalId().ifPresent(nativeUser::setExternalId);
provUser.isActive().ifPresent(nativeUser::setActive);
provUser.getMeta().ifPresent(meta -> {
meta.getCreated().ifPresent(created -> nativeUser.setCreatedAt(toLocalDateTime(created)));
meta.getLastModified().ifPresent(modified -> nativeUser.setModifiedAt(toLocalDateTime(modified)));
});
provUser.getName().ifPresent(name -> {
name.getFamilyName().ifPresent(nativeUser::setFamilyName);
name.getGivenName().ifPresent(nativeUser::setGivenName);
});
provUser.getEmails().stream()
.filter(email -> contains(email.getType(), "work") && email.getValue().isPresent())
.findFirst()
.flatMap(MultiComplexNode::getValue)
.ifPresent(nativeUser::setEmailAddress);
}
private User convert(com.xlrit.gears.base.model.User nativeUser) {
User provUser = new User();
provUser.setId(nativeUser.getId());
provUser.setUserName(nativeUser.getUsername());
provUser.setExternalId(nativeUser.getExternalId());
provUser.setActive(nativeUser.getActive());
provUser.setMeta(
Meta.builder()
.resourceType("User")
.created(nativeUser.getCreatedAt())
.lastModified(nativeUser.getModifiedAt())
.build()
);
if (nativeUser.getFamilyName() != null || nativeUser.getGivenName() != null) {
String formatted = nativeUser.getFullName();
provUser.setName(new Name(formatted, nativeUser.getFamilyName(), nativeUser.getGivenName(), null, null, null));
}
if (nativeUser.getEmailAddress() != null) {
provUser.setEmails(List.of(Email.builder().type("work").value(nativeUser.getEmailAddress()).build()));
}
return provUser;
}
}