Search in sources :

Example 11 with SCIMUser

use of org.apache.syncope.ext.scimv2.api.data.SCIMUser in project syncope by apache.

the class SCIMDataBinder method toSCIMUser.

public SCIMUser toSCIMUser(final UserTO userTO, final String location, final List<String> attributes, final List<String> excludedAttributes) {
    SCIMConf conf = confManager.get();
    List<String> schemas = new ArrayList<>();
    schemas.add(Resource.User.schema());
    if (conf.getEnterpriseUserConf() != null) {
        schemas.add(Resource.EnterpriseUser.schema());
    }
    SCIMUser user = new SCIMUser(userTO.getKey(), schemas, new Meta(Resource.User, userTO.getCreationDate(), userTO.getLastChangeDate() == null ? userTO.getCreationDate() : userTO.getLastChangeDate(), userTO.getETagValue(), location), output(attributes, excludedAttributes, "userName", userTO.getUsername()), !userTO.isSuspended());
    Map<String, AttrTO> attrs = new HashMap<>();
    attrs.putAll(EntityTOUtils.buildAttrMap(userTO.getPlainAttrs()));
    attrs.putAll(EntityTOUtils.buildAttrMap(userTO.getDerAttrs()));
    attrs.putAll(EntityTOUtils.buildAttrMap(userTO.getVirAttrs()));
    if (conf.getUserConf() != null) {
        if (output(attributes, excludedAttributes, "name") && conf.getUserConf().getName() != null) {
            SCIMUserName name = new SCIMUserName();
            if (conf.getUserConf().getName().getFamilyName() != null && attrs.containsKey(conf.getUserConf().getName().getFamilyName())) {
                name.setFamilyName(attrs.get(conf.getUserConf().getName().getFamilyName()).getValues().get(0));
            }
            if (conf.getUserConf().getName().getFormatted() != null && attrs.containsKey(conf.getUserConf().getName().getFormatted())) {
                name.setFormatted(attrs.get(conf.getUserConf().getName().getFormatted()).getValues().get(0));
            }
            if (conf.getUserConf().getName().getGivenName() != null && attrs.containsKey(conf.getUserConf().getName().getGivenName())) {
                name.setGivenName(attrs.get(conf.getUserConf().getName().getGivenName()).getValues().get(0));
            }
            if (conf.getUserConf().getName().getHonorificPrefix() != null && attrs.containsKey(conf.getUserConf().getName().getHonorificPrefix())) {
                name.setHonorificPrefix(attrs.get(conf.getUserConf().getName().getHonorificPrefix()).getValues().get(0));
            }
            if (conf.getUserConf().getName().getHonorificSuffix() != null && attrs.containsKey(conf.getUserConf().getName().getHonorificSuffix())) {
                name.setHonorificSuffix(attrs.get(conf.getUserConf().getName().getHonorificSuffix()).getValues().get(0));
            }
            if (conf.getUserConf().getName().getMiddleName() != null && attrs.containsKey(conf.getUserConf().getName().getMiddleName())) {
                name.setMiddleName(attrs.get(conf.getUserConf().getName().getMiddleName()).getValues().get(0));
            }
            if (!name.isEmpty()) {
                user.setName(name);
            }
        }
        if (output(attributes, excludedAttributes, "displayName") && conf.getUserConf().getDisplayName() != null && attrs.containsKey(conf.getUserConf().getDisplayName())) {
            user.setDisplayName(attrs.get(conf.getUserConf().getDisplayName()).getValues().get(0));
        }
        if (output(attributes, excludedAttributes, "nickName") && conf.getUserConf().getNickName() != null && attrs.containsKey(conf.getUserConf().getNickName())) {
            user.setNickName(attrs.get(conf.getUserConf().getNickName()).getValues().get(0));
        }
        if (output(attributes, excludedAttributes, "profileUrl") && conf.getUserConf().getProfileUrl() != null && attrs.containsKey(conf.getUserConf().getProfileUrl())) {
            user.setProfileUrl(attrs.get(conf.getUserConf().getProfileUrl()).getValues().get(0));
        }
        if (output(attributes, excludedAttributes, "title") && conf.getUserConf().getTitle() != null && attrs.containsKey(conf.getUserConf().getTitle())) {
            user.setTitle(attrs.get(conf.getUserConf().getTitle()).getValues().get(0));
        }
        if (output(attributes, excludedAttributes, "userType") && conf.getUserConf().getUserType() != null && attrs.containsKey(conf.getUserConf().getUserType())) {
            user.setUserType(attrs.get(conf.getUserConf().getUserType()).getValues().get(0));
        }
        if (output(attributes, excludedAttributes, "preferredLanguage") && conf.getUserConf().getPreferredLanguage() != null && attrs.containsKey(conf.getUserConf().getPreferredLanguage())) {
            user.setPreferredLanguage(attrs.get(conf.getUserConf().getPreferredLanguage()).getValues().get(0));
        }
        if (output(attributes, excludedAttributes, "locale") && conf.getUserConf().getLocale() != null && attrs.containsKey(conf.getUserConf().getLocale())) {
            user.setLocale(attrs.get(conf.getUserConf().getLocale()).getValues().get(0));
        }
        if (output(attributes, excludedAttributes, "timezone") && conf.getUserConf().getTimezone() != null && attrs.containsKey(conf.getUserConf().getTimezone())) {
            user.setTimezone(attrs.get(conf.getUserConf().getTimezone()).getValues().get(0));
        }
        if (output(attributes, excludedAttributes, "emails")) {
            fill(attrs, conf.getUserConf().getEmails(), user.getEmails());
        }
        if (output(attributes, excludedAttributes, "phoneNumbers")) {
            fill(attrs, conf.getUserConf().getPhoneNumbers(), user.getPhoneNumbers());
        }
        if (output(attributes, excludedAttributes, "ims")) {
            fill(attrs, conf.getUserConf().getIms(), user.getIms());
        }
        if (output(attributes, excludedAttributes, "photos")) {
            fill(attrs, conf.getUserConf().getPhotos(), user.getPhotos());
        }
        if (output(attributes, excludedAttributes, "addresses")) {
            conf.getUserConf().getAddresses().forEach(addressConf -> {
                SCIMUserAddress address = new SCIMUserAddress();
                if (addressConf.getFormatted() != null && attrs.containsKey(addressConf.getFormatted())) {
                    address.setFormatted(attrs.get(addressConf.getFormatted()).getValues().get(0));
                }
                if (addressConf.getStreetAddress() != null && attrs.containsKey(addressConf.getStreetAddress())) {
                    address.setStreetAddress(attrs.get(addressConf.getStreetAddress()).getValues().get(0));
                }
                if (addressConf.getLocality() != null && attrs.containsKey(addressConf.getLocality())) {
                    address.setLocality(attrs.get(addressConf.getLocality()).getValues().get(0));
                }
                if (addressConf.getRegion() != null && attrs.containsKey(addressConf.getRegion())) {
                    address.setRegion(attrs.get(addressConf.getRegion()).getValues().get(0));
                }
                if (addressConf.getCountry() != null && attrs.containsKey(addressConf.getCountry())) {
                    address.setCountry(attrs.get(addressConf.getCountry()).getValues().get(0));
                }
                if (addressConf.getType() != null) {
                    address.setType(addressConf.getType().name());
                }
                if (addressConf.isPrimary()) {
                    address.setPrimary(true);
                }
                if (!address.isEmpty()) {
                    user.getAddresses().add(address);
                }
            });
        }
        if (output(attributes, excludedAttributes, "x509Certificates")) {
            conf.getUserConf().getX509Certificates().stream().filter(certificate -> attrs.containsKey(certificate)).forEachOrdered(certificate -> {
                user.getX509Certificates().add(new Value(attrs.get(certificate).getValues().get(0)));
            });
        }
        if (conf.getEnterpriseUserConf() != null) {
            SCIMEnterpriseInfo enterpriseInfo = new SCIMEnterpriseInfo();
            if (output(attributes, excludedAttributes, "employeeNumber") && conf.getEnterpriseUserConf().getEmployeeNumber() != null && attrs.containsKey(conf.getEnterpriseUserConf().getEmployeeNumber())) {
                enterpriseInfo.setEmployeeNumber(attrs.get(conf.getEnterpriseUserConf().getEmployeeNumber()).getValues().get(0));
            }
            if (output(attributes, excludedAttributes, "costCenter") && conf.getEnterpriseUserConf().getCostCenter() != null && attrs.containsKey(conf.getEnterpriseUserConf().getCostCenter())) {
                enterpriseInfo.setCostCenter(attrs.get(conf.getEnterpriseUserConf().getCostCenter()).getValues().get(0));
            }
            if (output(attributes, excludedAttributes, "organization") && conf.getEnterpriseUserConf().getOrganization() != null && attrs.containsKey(conf.getEnterpriseUserConf().getOrganization())) {
                enterpriseInfo.setOrganization(attrs.get(conf.getEnterpriseUserConf().getOrganization()).getValues().get(0));
            }
            if (output(attributes, excludedAttributes, "division") && conf.getEnterpriseUserConf().getDivision() != null && attrs.containsKey(conf.getEnterpriseUserConf().getDivision())) {
                enterpriseInfo.setDivision(attrs.get(conf.getEnterpriseUserConf().getDivision()).getValues().get(0));
            }
            if (output(attributes, excludedAttributes, "department") && conf.getEnterpriseUserConf().getDepartment() != null && attrs.containsKey(conf.getEnterpriseUserConf().getDepartment())) {
                enterpriseInfo.setDepartment(attrs.get(conf.getEnterpriseUserConf().getDepartment()).getValues().get(0));
            }
            if (output(attributes, excludedAttributes, "manager") && conf.getEnterpriseUserConf().getManager() != null) {
                SCIMUserManager manager = new SCIMUserManager();
                if (conf.getEnterpriseUserConf().getManager().getKey() != null && attrs.containsKey(conf.getEnterpriseUserConf().getManager().getKey())) {
                    try {
                        UserTO userManager = userLogic.read(attrs.get(conf.getEnterpriseUserConf().getManager().getKey()).getValues().get(0));
                        manager.setValue(userManager.getKey());
                        manager.setRef(StringUtils.substringBefore(location, "/Users") + "/Users/" + userManager.getKey());
                        if (conf.getEnterpriseUserConf().getManager().getDisplayName() != null) {
                            AttrTO displayName = userManager.getPlainAttr(conf.getEnterpriseUserConf().getManager().getDisplayName()).orElse(null);
                            if (displayName == null) {
                                displayName = userManager.getDerAttr(conf.getEnterpriseUserConf().getManager().getDisplayName()).orElse(null);
                            }
                            if (displayName == null) {
                                displayName = userManager.getVirAttr(conf.getEnterpriseUserConf().getManager().getDisplayName()).orElse(null);
                            }
                            if (displayName != null) {
                                manager.setDisplayName(displayName.getValues().get(0));
                            }
                        }
                    } catch (Exception e) {
                        LOG.error("Could not read user {}", conf.getEnterpriseUserConf().getManager().getKey(), e);
                    }
                }
                if (!manager.isEmpty()) {
                    enterpriseInfo.setManager(manager);
                }
            }
            if (!enterpriseInfo.isEmpty()) {
                user.setEnterpriseInfo(enterpriseInfo);
            }
        }
        if (output(attributes, excludedAttributes, "groups")) {
            userTO.getMemberships().forEach(membership -> {
                user.getGroups().add(new Group(membership.getGroupKey(), StringUtils.substringBefore(location, "/Users") + "/Groups/" + membership.getGroupKey(), membership.getGroupName(), Function.direct));
            });
            userTO.getDynMemberships().forEach(membership -> {
                user.getGroups().add(new Group(membership.getGroupKey(), StringUtils.substringBefore(location, "/Users") + "/Groups/" + membership.getGroupKey(), membership.getGroupName(), Function.indirect));
            });
        }
        if (output(attributes, excludedAttributes, "entitlements")) {
            authDataAccessor.getAuthorities(userTO.getUsername()).forEach(authority -> {
                user.getEntitlements().add(new Value(authority.getAuthority() + " on Realm(s) " + authority.getRealms()));
            });
        }
        if (output(attributes, excludedAttributes, "roles")) {
            userTO.getRoles().forEach(role -> {
                user.getRoles().add(new Value(role));
            });
        }
    }
    return user;
}
Also used : Arrays(java.util.Arrays) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) Group(org.apache.syncope.ext.scimv2.api.data.Group) AttrTO(org.apache.syncope.common.lib.to.AttrTO) EntityTOUtils(org.apache.syncope.common.lib.EntityTOUtils) SCIMUserAddressConf(org.apache.syncope.common.lib.scim.SCIMUserAddressConf) ErrorType(org.apache.syncope.ext.scimv2.api.type.ErrorType) LoggerFactory(org.slf4j.LoggerFactory) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) SCIMUserAddress(org.apache.syncope.ext.scimv2.api.data.SCIMUserAddress) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Map(java.util.Map) SCIMUserManager(org.apache.syncope.ext.scimv2.api.data.SCIMUserManager) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) SCIMComplexValue(org.apache.syncope.ext.scimv2.api.data.SCIMComplexValue) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) SCIMGroup(org.apache.syncope.ext.scimv2.api.data.SCIMGroup) Logger(org.slf4j.Logger) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Value(org.apache.syncope.ext.scimv2.api.data.Value) SCIMUserName(org.apache.syncope.ext.scimv2.api.data.SCIMUserName) SCIMEnterpriseInfo(org.apache.syncope.ext.scimv2.api.data.SCIMEnterpriseInfo) Set(java.util.Set) GroupTO(org.apache.syncope.common.lib.to.GroupTO) AuthDataAccessor(org.apache.syncope.core.spring.security.AuthDataAccessor) Function(org.apache.syncope.ext.scimv2.api.type.Function) SCIMUser(org.apache.syncope.ext.scimv2.api.data.SCIMUser) List(java.util.List) Meta(org.apache.syncope.ext.scimv2.api.data.Meta) Component(org.springframework.stereotype.Component) Resource(org.apache.syncope.ext.scimv2.api.type.Resource) SCIMComplexConf(org.apache.syncope.common.lib.scim.SCIMComplexConf) SCIMConf(org.apache.syncope.common.lib.scim.SCIMConf) AnyDAO(org.apache.syncope.core.persistence.api.dao.AnyDAO) SCIMConfManager(org.apache.syncope.core.logic.scim.SCIMConfManager) Member(org.apache.syncope.ext.scimv2.api.data.Member) Optional(java.util.Optional) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) Collections(java.util.Collections) Meta(org.apache.syncope.ext.scimv2.api.data.Meta) SCIMUserManager(org.apache.syncope.ext.scimv2.api.data.SCIMUserManager) Group(org.apache.syncope.ext.scimv2.api.data.Group) SCIMGroup(org.apache.syncope.ext.scimv2.api.data.SCIMGroup) SCIMUser(org.apache.syncope.ext.scimv2.api.data.SCIMUser) HashMap(java.util.HashMap) SCIMUserName(org.apache.syncope.ext.scimv2.api.data.SCIMUserName) ArrayList(java.util.ArrayList) AttrTO(org.apache.syncope.common.lib.to.AttrTO) SCIMUserAddress(org.apache.syncope.ext.scimv2.api.data.SCIMUserAddress) SCIMConf(org.apache.syncope.common.lib.scim.SCIMConf) SCIMEnterpriseInfo(org.apache.syncope.ext.scimv2.api.data.SCIMEnterpriseInfo) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) UserTO(org.apache.syncope.common.lib.to.UserTO) SCIMComplexValue(org.apache.syncope.ext.scimv2.api.data.SCIMComplexValue) Value(org.apache.syncope.ext.scimv2.api.data.Value)

Aggregations

SCIMUser (org.apache.syncope.ext.scimv2.api.data.SCIMUser)10 Response (javax.ws.rs.core.Response)7 ListResponse (org.apache.syncope.ext.scimv2.api.data.ListResponse)7 Test (org.junit.jupiter.api.Test)7 UserTO (org.apache.syncope.common.lib.to.UserTO)5 SCIMGroup (org.apache.syncope.ext.scimv2.api.data.SCIMGroup)5 SCIMComplexValue (org.apache.syncope.ext.scimv2.api.data.SCIMComplexValue)4 SCIMConf (org.apache.syncope.common.lib.scim.SCIMConf)3 BadRequestException (org.apache.syncope.ext.scimv2.api.BadRequestException)3 Group (org.apache.syncope.ext.scimv2.api.data.Group)3 Member (org.apache.syncope.ext.scimv2.api.data.Member)3 SCIMError (org.apache.syncope.ext.scimv2.api.data.SCIMError)3 SCIMUserName (org.apache.syncope.ext.scimv2.api.data.SCIMUserName)3 Value (org.apache.syncope.ext.scimv2.api.data.Value)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2