Search in sources :

Example 41 with User

use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.

the class DefaultAccountRule method enforce.

@Transactional(readOnly = true)
@Override
public void enforce(final User user) {
    this.conf.getSchemasNotPermitted().stream().map(schema -> user.getPlainAttr(schema)).filter(attr -> attr.isPresent()).map(attr -> attr.get().getValuesAsStrings()).filter(values -> (values != null && !values.isEmpty())).forEachOrdered(values -> this.conf.getWordsNotPermitted().add(values.get(0)));
    if (user.getUsername() == null) {
        throw new AccountPolicyException("Invalid account");
    }
    // check min length
    if (this.conf.getMinLength() > 0 && this.conf.getMinLength() > user.getUsername().length()) {
        throw new AccountPolicyException("Username too short");
    }
    // check max length
    if (this.conf.getMaxLength() > 0 && this.conf.getMaxLength() < user.getUsername().length()) {
        throw new AccountPolicyException("Username too long");
    }
    // check words not permitted
    this.conf.getWordsNotPermitted().stream().filter(word -> StringUtils.containsIgnoreCase(user.getUsername(), word)).forEachOrdered(item -> {
        throw new AccountPolicyException("Used word(s) not permitted");
    });
    // check case
    if (this.conf.isAllUpperCase() && !user.getUsername().equals(user.getUsername().toUpperCase())) {
        throw new AccountPolicyException("No lowercase characters permitted");
    }
    if (this.conf.isAllLowerCase() && !user.getUsername().equals(user.getUsername().toLowerCase())) {
        throw new AccountPolicyException("No uppercase characters permitted");
    }
    // check pattern
    Pattern pattern = (this.conf.getPattern() == null) ? DEFAULT_PATTERN : Pattern.compile(this.conf.getPattern());
    if (!pattern.matcher(user.getUsername()).matches()) {
        throw new AccountPolicyException("Username does not match pattern");
    }
    // check prefix
    this.conf.getPrefixesNotPermitted().stream().filter(prefix -> user.getUsername().startsWith(prefix)).forEachOrdered(item -> {
        throw new AccountPolicyException("Prefix not permitted");
    });
    // check suffix
    this.conf.getSuffixesNotPermitted().stream().filter(suffix -> user.getUsername().endsWith(suffix)).forEachOrdered(item -> {
        throw new AccountPolicyException("Suffix not permitted");
    });
}
Also used : AccountRuleConf(org.apache.syncope.common.lib.policy.AccountRuleConf) AccountRule(org.apache.syncope.core.persistence.api.dao.AccountRule) AccountRuleConfClass(org.apache.syncope.core.persistence.api.dao.AccountRuleConfClass) User(org.apache.syncope.core.persistence.api.entity.user.User) Pattern(java.util.regex.Pattern) AccountPolicyException(org.apache.syncope.core.provisioning.api.utils.policy.AccountPolicyException) DefaultAccountRuleConf(org.apache.syncope.common.lib.policy.DefaultAccountRuleConf) StringUtils(org.apache.commons.lang3.StringUtils) Transactional(org.springframework.transaction.annotation.Transactional) Pattern(java.util.regex.Pattern) AccountPolicyException(org.apache.syncope.core.provisioning.api.utils.policy.AccountPolicyException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 42 with User

use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.

the class MigrationPullActions method after.

@Transactional
@Override
public void after(final ProvisioningProfile<?, ?> profile, final SyncDelta delta, final EntityTO entity, final ProvisioningReport result) throws JobExecutionException {
    if (entity instanceof UserTO) {
        // handles ciphered password import
        CipherAlgorithm cipherAlgorithm = null;
        Attribute cipherAlgorithmAttr = delta.getObject().getAttributeByName(CIPHER_ALGORITHM_ATTR);
        if (cipherAlgorithmAttr != null && cipherAlgorithmAttr.getValue() != null && !cipherAlgorithmAttr.getValue().isEmpty()) {
            cipherAlgorithm = CipherAlgorithm.valueOf(cipherAlgorithmAttr.getValue().get(0).toString());
        }
        GuardedString passwordValue = AttributeUtil.getPasswordValue(delta.getObject().getAttributes());
        if (cipherAlgorithm != null && passwordValue != null) {
            User user = userDAO.find(entity.getKey());
            LOG.debug("Setting encoded password for {}", user);
            user.setEncodedPassword(SecurityUtil.decrypt(passwordValue), cipherAlgorithm);
        }
    } else if (entity instanceof GroupTO) {
        // handles group membership
        Attribute membershipsAttr = delta.getObject().getAttributeByName(MEMBERSHIPS_ATTR);
        if (membershipsAttr != null && membershipsAttr.getValue() != null && !membershipsAttr.getValue().isEmpty()) {
            LOG.debug("Found {} for group {}", MEMBERSHIPS_ATTR, entity.getKey());
            for (Object membership : membershipsAttr.getValue()) {
                User member = userDAO.findByUsername(membership.toString());
                if (member == null) {
                    LOG.warn("Could not find member {} for group {}", membership, entity.getKey());
                } else {
                    Set<String> memb = memberships.get(member.getKey());
                    if (memb == null) {
                        memb = new HashSet<>();
                        memberships.put(member.getKey(), memb);
                    }
                    memb.add(entity.getKey());
                }
            }
        }
    } else {
        super.after(profile, delta, entity, result);
    }
}
Also used : CipherAlgorithm(org.apache.syncope.common.lib.types.CipherAlgorithm) User(org.apache.syncope.core.persistence.api.entity.user.User) HashSet(java.util.HashSet) Set(java.util.Set) Attribute(org.identityconnectors.framework.common.objects.Attribute) UserTO(org.apache.syncope.common.lib.to.UserTO) GuardedString(org.identityconnectors.common.security.GuardedString) GroupTO(org.apache.syncope.common.lib.to.GroupTO) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 43 with User

use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.

the class GroupTest method findDynGroups.

/**
 * Static copy of {@link org.apache.syncope.core.persistence.jpa.dao.JPAUserDAO} method with same signature:
 * required for avoiding creating of a new transaction - good for general use case but bad for the way how
 * this test class is architected.
 */
@SuppressWarnings("unchecked")
public List<Group> findDynGroups(final User user) {
    Query query = entityManager().createNativeQuery("SELECT group_id FROM " + JPAGroupDAO.UDYNMEMB_TABLE + " WHERE any_id=?");
    query.setParameter(1, user.getKey());
    List<Group> result = new ArrayList<>();
    query.getResultList().stream().map(resultKey -> resultKey instanceof Object[] ? (String) ((Object[]) resultKey)[0] : ((String) resultKey)).forEachOrdered(actualKey -> {
        Group group = groupDAO.find(actualKey.toString());
        if (group == null) {
        } else if (!result.contains(group)) {
            result.add(group);
        }
    });
    return result;
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) UDynGroupMembership(org.apache.syncope.core.persistence.api.entity.user.UDynGroupMembership) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) GPlainAttrValue(org.apache.syncope.core.persistence.api.entity.group.GPlainAttrValue) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) JPAADynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAADynGroupMembership) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) TypeExtension(org.apache.syncope.core.persistence.api.entity.group.TypeExtension) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) Collection(java.util.Collection) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) ADynGroupMembership(org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership) User(org.apache.syncope.core.persistence.api.entity.user.User) GPlainAttr(org.apache.syncope.core.persistence.api.entity.group.GPlainAttr) JPAUDynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPAUDynGroupMembership) JPAGroupDAO(org.apache.syncope.core.persistence.jpa.dao.JPAGroupDAO) Collectors(java.util.stream.Collectors) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) PlainAttrValueDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrValueDAO) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) Test(org.junit.jupiter.api.Test) List(java.util.List) Query(javax.persistence.Query) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest) AnyTypeClassDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO) PlainAttrDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrDAO) APlainAttr(org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) Transactional(org.springframework.transaction.annotation.Transactional) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject)

Example 44 with User

use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.

the class GroupTest method udynMembership.

@Test
public void udynMembership() {
    // 0. create user matching the condition below
    User user = entityFactory.newEntity(User.class);
    user.setUsername("username");
    user.setRealm(realmDAO.findByFullPath("/even/two"));
    user.add(anyTypeClassDAO.find("other"));
    UPlainAttr attr = entityFactory.newEntity(UPlainAttr.class);
    attr.setOwner(user);
    attr.setSchema(plainSchemaDAO.find("cool"));
    attr.add("true", anyUtilsFactory.getInstance(AnyTypeKind.USER));
    user.add(attr);
    user = userDAO.save(user);
    String newUserKey = user.getKey();
    assertNotNull(newUserKey);
    // 1. create group with dynamic membership
    Group group = entityFactory.newEntity(Group.class);
    group.setRealm(realmDAO.getRoot());
    group.setName("new");
    UDynGroupMembership dynMembership = entityFactory.newEntity(UDynGroupMembership.class);
    dynMembership.setFIQLCond("cool==true");
    dynMembership.setGroup(group);
    group.setUDynMembership(dynMembership);
    Group actual = groupDAO.save(group);
    assertNotNull(actual);
    groupDAO.flush();
    // 2. verify that dynamic membership is there
    actual = groupDAO.find(actual.getKey());
    assertNotNull(actual);
    assertNotNull(actual.getUDynMembership());
    assertNotNull(actual.getUDynMembership().getKey());
    assertEquals(actual, actual.getUDynMembership().getGroup());
    // 3. verify that expected users have the created group dynamically assigned
    List<String> members = groupDAO.findUDynMembers(actual);
    assertEquals(2, members.size());
    assertEquals(new HashSet<>(Arrays.asList("c9b2dec2-00a7-4855-97c0-d854842b4b24", newUserKey)), new HashSet<>(members));
    user = userDAO.findByUsername("bellini");
    assertNotNull(user);
    Collection<Group> dynGroupMemberships = findDynGroups(user);
    assertEquals(1, dynGroupMemberships.size());
    assertTrue(dynGroupMemberships.contains(actual.getUDynMembership().getGroup()));
    // 4. delete the new user and verify that dynamic membership was updated
    userDAO.delete(newUserKey);
    userDAO.flush();
    actual = groupDAO.find(actual.getKey());
    members = groupDAO.findUDynMembers(actual);
    assertEquals(1, members.size());
    assertEquals("c9b2dec2-00a7-4855-97c0-d854842b4b24", members.get(0));
    // 5. delete group and verify that dynamic membership was also removed
    String dynMembershipKey = actual.getUDynMembership().getKey();
    groupDAO.delete(actual);
    groupDAO.flush();
    assertNull(entityManager().find(JPAUDynGroupMembership.class, dynMembershipKey));
    dynGroupMemberships = findDynGroups(user);
    assertTrue(dynGroupMemberships.isEmpty());
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) JPAUDynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPAUDynGroupMembership) UDynGroupMembership(org.apache.syncope.core.persistence.api.entity.user.UDynGroupMembership) JPAUDynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPAUDynGroupMembership) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 45 with User

use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.

the class GroupTest method findByOwner.

@Test
public void findByOwner() {
    Group group = groupDAO.find("ebf97068-aa4b-4a85-9f01-680e8c4cf227");
    assertNotNull(group);
    User user = userDAO.find("823074dc-d280-436d-a7dd-07399fae48ec");
    assertNotNull(user);
    assertEquals(user, group.getUserOwner());
    List<Group> ownedGroups = groupDAO.findOwnedByUser(user.getKey());
    assertFalse(ownedGroups.isEmpty());
    assertEquals(1, ownedGroups.size());
    assertTrue(ownedGroups.contains(group));
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

User (org.apache.syncope.core.persistence.api.entity.user.User)135 Test (org.junit.jupiter.api.Test)58 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)56 Transactional (org.springframework.transaction.annotation.Transactional)39 Group (org.apache.syncope.core.persistence.api.entity.group.Group)24 ArrayList (java.util.ArrayList)20 UserTO (org.apache.syncope.common.lib.to.UserTO)20 SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)20 HashSet (java.util.HashSet)18 AttributeCond (org.apache.syncope.core.persistence.api.dao.search.AttributeCond)17 AnyObject (org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject)17 List (java.util.List)16 WorkflowResult (org.apache.syncope.core.provisioning.api.WorkflowResult)16 UPlainAttr (org.apache.syncope.core.persistence.api.entity.user.UPlainAttr)15 Autowired (org.springframework.beans.factory.annotation.Autowired)15 Set (java.util.Set)14 PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)14 PropagationByResource (org.apache.syncope.core.provisioning.api.PropagationByResource)14 Collections (java.util.Collections)11 Collectors (java.util.stream.Collectors)11