Search in sources :

Example 61 with AttrTO

use of org.apache.syncope.common.lib.to.AttrTO 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)

Example 62 with AttrTO

use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.

the class AbstractAnyService method read.

@Override
public Set<AttrTO> read(final String key, final SchemaType schemaType) {
    TO any = read(key);
    Set<AttrTO> result;
    switch(schemaType) {
        case DERIVED:
            result = any.getDerAttrs();
            break;
        case VIRTUAL:
            result = any.getVirAttrs();
            break;
        case PLAIN:
        default:
            result = any.getPlainAttrs();
    }
    return result;
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AnyTO(org.apache.syncope.common.lib.to.AnyTO)

Example 63 with AttrTO

use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.

the class UserIssuesITCase method issueSYNCOPE136AES.

@Test
public void issueSYNCOPE136AES() {
    // 1. read configured cipher algorithm in order to be able to restore it at the end of test
    AttrTO pwdCipherAlgo = configurationService.get("password.cipher.algorithm");
    String origpwdCipherAlgo = pwdCipherAlgo.getValues().get(0);
    // 2. set AES password cipher algorithm
    pwdCipherAlgo.getValues().set(0, "AES");
    configurationService.set(pwdCipherAlgo);
    UserTO userTO = null;
    try {
        // 3. create user with no resources
        userTO = UserITCase.getUniqueSampleTO("syncope136_AES@apache.org");
        userTO.getResources().clear();
        userTO = createUser(userTO).getEntity();
        assertNotNull(userTO);
        // 4. update user, assign a propagation priority resource but don't provide any password
        UserPatch userPatch = new UserPatch();
        userPatch.setKey(userTO.getKey());
        userPatch.getResources().add(new StringPatchItem.Builder().operation(PatchOperation.ADD_REPLACE).value(RESOURCE_NAME_LDAP).build());
        userPatch.setPassword(new PasswordPatch.Builder().onSyncope(false).resource(RESOURCE_NAME_LDAP).build());
        ProvisioningResult<UserTO> result = updateUser(userPatch);
        assertNotNull(result);
        userTO = result.getEntity();
        assertNotNull(userTO);
        // 5. verify that propagation was successful
        List<PropagationStatus> props = result.getPropagationStatuses();
        assertNotNull(props);
        assertEquals(1, props.size());
        PropagationStatus prop = props.iterator().next();
        assertNotNull(prop);
        assertEquals(RESOURCE_NAME_LDAP, prop.getResource());
        assertEquals(PropagationTaskExecStatus.SUCCESS, prop.getStatus());
    } finally {
        // restore initial cipher algorithm
        pwdCipherAlgo.getValues().set(0, origpwdCipherAlgo);
        configurationService.set(pwdCipherAlgo);
        if (userTO != null) {
            deleteUser(userTO.getKey());
        }
    }
}
Also used : UserTO(org.apache.syncope.common.lib.to.UserTO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) PropagationStatus(org.apache.syncope.common.lib.to.PropagationStatus) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) Test(org.junit.jupiter.api.Test)

Example 64 with AttrTO

use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.

the class UserIssuesITCase method issueSYNCOPE51.

@Test()
public void issueSYNCOPE51() {
    AttrTO defaultCA = configurationService.get("password.cipher.algorithm");
    String originalCAValue = defaultCA.getValues().get(0);
    defaultCA.getValues().set(0, "MD5");
    configurationService.set(defaultCA);
    AttrTO newCA = configurationService.get(defaultCA.getSchema());
    assertEquals(defaultCA, newCA);
    UserTO userTO = UserITCase.getSampleTO("syncope51@syncope.apache.org");
    userTO.setPassword("password");
    try {
        createUser(userTO);
        fail("Create user should not succeed");
    } catch (SyncopeClientException e) {
        assertEquals(ClientExceptionType.NotFound, e.getType());
        assertTrue(e.getElements().iterator().next().contains("MD5"));
    }
    defaultCA.getValues().set(0, originalCAValue);
    configurationService.set(defaultCA);
    AttrTO oldCA = configurationService.get(defaultCA.getSchema());
    assertEquals(defaultCA, oldCA);
}
Also used : UserTO(org.apache.syncope.common.lib.to.UserTO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) Test(org.junit.jupiter.api.Test)

Example 65 with AttrTO

use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.

the class VirAttrITCase method virAttrCache.

@Test
public void virAttrCache() {
    UserTO userTO = UserITCase.getUniqueSampleTO("virattrcache@apache.org");
    userTO.getVirAttrs().clear();
    AttrTO virAttrTO = new AttrTO();
    virAttrTO.setSchema("virtualdata");
    virAttrTO.getValues().add("virattrcache");
    userTO.getVirAttrs().add(virAttrTO);
    userTO.getMemberships().clear();
    userTO.getResources().clear();
    userTO.getResources().add(RESOURCE_NAME_DBVIRATTR);
    // 1. create user
    UserTO actual = createUser(userTO).getEntity();
    assertNotNull(actual);
    // 2. check for virtual attribute value
    actual = userService.read(actual.getKey());
    assertEquals("virattrcache", actual.getVirAttr("virtualdata").get().getValues().get(0));
    // 3. update virtual attribute directly
    JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);
    String value = queryForObject(jdbcTemplate, 50, "SELECT USERNAME FROM testpull WHERE ID=?", String.class, actual.getKey());
    assertEquals("virattrcache", value);
    jdbcTemplate.update("UPDATE testpull set USERNAME='virattrcache2' WHERE ID=?", actual.getKey());
    value = queryForObject(jdbcTemplate, 50, "SELECT USERNAME FROM testpull WHERE ID=?", String.class, actual.getKey());
    assertEquals("virattrcache2", value);
    // 4. check for cached attribute value
    actual = userService.read(actual.getKey());
    assertEquals("virattrcache", actual.getVirAttr("virtualdata").get().getValues().get(0));
    UserPatch userPatch = new UserPatch();
    userPatch.setKey(actual.getKey());
    userPatch.getVirAttrs().add(attrTO("virtualdata", "virtualupdated"));
    // 5. update virtual attribute
    actual = updateUser(userPatch).getEntity();
    assertNotNull(actual);
    // 6. check for virtual attribute value
    actual = userService.read(actual.getKey());
    assertNotNull(actual);
    assertEquals("virtualupdated", actual.getVirAttr("virtualdata").get().getValues().get(0));
}
Also used : UserTO(org.apache.syncope.common.lib.to.UserTO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) Test(org.junit.jupiter.api.Test)

Aggregations

AttrTO (org.apache.syncope.common.lib.to.AttrTO)70 Test (org.junit.jupiter.api.Test)31 UserTO (org.apache.syncope.common.lib.to.UserTO)30 MembershipTO (org.apache.syncope.common.lib.to.MembershipTO)19 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)17 Map (java.util.Map)15 GroupTO (org.apache.syncope.common.lib.to.GroupTO)15 ArrayList (java.util.ArrayList)14 UserPatch (org.apache.syncope.common.lib.patch.UserPatch)14 List (java.util.List)13 Collections (java.util.Collections)11 StringUtils (org.apache.commons.lang3.StringUtils)11 AnyTO (org.apache.syncope.common.lib.to.AnyTO)10 Optional (java.util.Optional)9 Set (java.util.Set)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 HashMap (java.util.HashMap)8 Collectors (java.util.stream.Collectors)8 EntityTOUtils (org.apache.syncope.common.lib.EntityTOUtils)8 AnyObjectTO (org.apache.syncope.common.lib.to.AnyObjectTO)8