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");
});
}
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);
}
}
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;
}
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());
}
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));
}
Aggregations