Search in sources :

Example 51 with AttrTO

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

the class UserITCase method updateWithoutPassword.

@Test
public void updateWithoutPassword() {
    UserTO userTO = getUniqueSampleTO("updatewithout@password.com");
    userTO = createUser(userTO).getEntity();
    assertNotNull(userTO);
    UserPatch userPatch = new UserPatch();
    userPatch.setKey(userTO.getKey());
    userPatch.getPlainAttrs().add(new AttrPatch.Builder().operation(PatchOperation.DELETE).attrTO(new AttrTO.Builder().schema("ctype").build()).build());
    userTO = updateUser(userPatch).getEntity();
    assertNotNull(userTO);
    assertFalse(userTO.getPlainAttr("ctype").isPresent());
}
Also used : UserTO(org.apache.syncope.common.lib.to.UserTO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) Test(org.junit.jupiter.api.Test)

Example 52 with AttrTO

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

the class UserITCase method update.

@Test
public void update() {
    UserTO userTO = getUniqueSampleTO("g.h@t.com");
    userTO.getMemberships().add(new MembershipTO.Builder().group("f779c0d4-633b-4be5-8f57-32eb478a3ca5").build());
    userTO = createUser(userTO).getEntity();
    assertFalse(userTO.getDerAttrs().isEmpty());
    assertEquals(1, userTO.getMemberships().size());
    UserPatch userPatch = new UserPatch();
    userPatch.setKey(userTO.getKey());
    userPatch.setPassword(new PasswordPatch.Builder().value("new2Password").build());
    String newUserId = getUUIDString() + "t.w@spre.net";
    userPatch.getPlainAttrs().add(attrAddReplacePatch("userId", newUserId));
    String newFullName = getUUIDString() + "g.h@t.com";
    userPatch.getPlainAttrs().add(attrAddReplacePatch("fullname", newFullName));
    userPatch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group("f779c0d4-633b-4be5-8f57-32eb478a3ca5").build());
    userPatch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(userTO.getMemberships().get(0).getGroupKey()).build());
    userTO = updateUser(userPatch).getEntity();
    assertNotNull(userTO);
    // issue SYNCOPE-15
    assertNotNull(userTO.getCreationDate());
    assertNotNull(userTO.getCreator());
    assertNotNull(userTO.getLastChangeDate());
    assertNotNull(userTO.getLastModifier());
    assertTrue(userTO.getCreationDate().before(userTO.getLastChangeDate()));
    assertEquals(1, userTO.getMemberships().size());
    assertFalse(userTO.getDerAttrs().isEmpty());
    AttrTO userIdAttr = userTO.getPlainAttr("userId").get();
    assertEquals(Collections.singletonList(newUserId), userIdAttr.getValues());
    AttrTO fullNameAttr = userTO.getPlainAttr("fullname").get();
    assertEquals(Collections.singletonList(newFullName), fullNameAttr.getValues());
    // update by username
    userPatch = new UserPatch();
    userPatch.setKey(userTO.getUsername());
    String newUsername = UUID.randomUUID().toString();
    userPatch.setUsername(new StringReplacePatchItem.Builder().value(newUsername).build());
    userTO = updateUser(userPatch).getEntity();
    assertNotNull(userTO);
    assertEquals(newUsername, userTO.getUsername());
}
Also used : PasswordPatch(org.apache.syncope.common.lib.patch.PasswordPatch) StringReplacePatchItem(org.apache.syncope.common.lib.patch.StringReplacePatchItem) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) Test(org.junit.jupiter.api.Test)

Example 53 with AttrTO

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

the class TemplateUtils method check.

public void check(final Map<String, AnyTO> templates, final ClientExceptionType clientExceptionType) {
    SyncopeClientException sce = SyncopeClientException.build(clientExceptionType);
    templates.values().forEach(value -> {
        value.getPlainAttrs().stream().filter(attrTO -> !attrTO.getValues().isEmpty() && !JexlUtils.isExpressionValid(attrTO.getValues().get(0))).forEachOrdered(attrTO -> {
            sce.getElements().add("Invalid JEXL: " + attrTO.getValues().get(0));
        });
        value.getVirAttrs().stream().filter(attrTO -> !attrTO.getValues().isEmpty() && !JexlUtils.isExpressionValid(attrTO.getValues().get(0))).forEachOrdered((attrTO) -> {
            sce.getElements().add("Invalid JEXL: " + attrTO.getValues().get(0));
        });
        if (value instanceof UserTO) {
            UserTO template = (UserTO) value;
            if (StringUtils.isNotBlank(template.getUsername()) && !JexlUtils.isExpressionValid(template.getUsername())) {
                sce.getElements().add("Invalid JEXL: " + template.getUsername());
            }
            if (StringUtils.isNotBlank(template.getPassword()) && !JexlUtils.isExpressionValid(template.getPassword())) {
                sce.getElements().add("Invalid JEXL: " + template.getPassword());
            }
        } else if (value instanceof GroupTO) {
            GroupTO template = (GroupTO) value;
            if (StringUtils.isNotBlank(template.getName()) && !JexlUtils.isExpressionValid(template.getName())) {
                sce.getElements().add("Invalid JEXL: " + template.getName());
            }
        }
    });
    if (!sce.isEmpty()) {
        throw sce;
    }
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) EntityTOUtils(org.apache.syncope.common.lib.EntityTOUtils) AnyTO(org.apache.syncope.common.lib.to.AnyTO) User(org.apache.syncope.core.persistence.api.entity.user.User) Autowired(org.springframework.beans.factory.annotation.Autowired) MapContext(org.apache.commons.jexl3.MapContext) GroupTO(org.apache.syncope.common.lib.to.GroupTO) StringUtils(org.apache.commons.lang3.StringUtils) JexlUtils(org.apache.syncope.core.provisioning.java.jexl.JexlUtils) Component(org.springframework.stereotype.Component) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Map(java.util.Map) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) GroupableRelatableTO(org.apache.syncope.common.lib.to.GroupableRelatableTO) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyTemplate(org.apache.syncope.core.persistence.api.entity.AnyTemplate) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) Transactional(org.springframework.transaction.annotation.Transactional) UserTO(org.apache.syncope.common.lib.to.UserTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) GroupTO(org.apache.syncope.common.lib.to.GroupTO)

Example 54 with AttrTO

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

the class TemplateUtils method fill.

private void fill(final AnyTO anyTO, final AnyTO template) {
    MapContext jexlContext = new MapContext();
    JexlUtils.addFieldsToContext(anyTO, jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getPlainAttrs(), jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getDerAttrs(), jexlContext);
    JexlUtils.addAttrTOsToContext(anyTO.getVirAttrs(), jexlContext);
    if (template.getRealm() != null) {
        String evaluated = JexlUtils.evaluate(template.getRealm(), jexlContext);
        if (StringUtils.isNotBlank(evaluated)) {
            anyTO.setRealm(evaluated);
        }
    }
    Map<String, AttrTO> currentAttrMap = EntityTOUtils.buildAttrMap(anyTO.getPlainAttrs());
    for (AttrTO templatePlainAttr : template.getPlainAttrs()) {
        if (!templatePlainAttr.getValues().isEmpty() && (!currentAttrMap.containsKey(templatePlainAttr.getSchema()) || currentAttrMap.get(templatePlainAttr.getSchema()).getValues().isEmpty())) {
            AttrTO evaluated = evaluateAttr(templatePlainAttr, jexlContext);
            if (!evaluated.getValues().isEmpty()) {
                anyTO.getPlainAttrs().add(evaluated);
                jexlContext.set(evaluated.getSchema(), evaluated.getValues().get(0));
            }
        }
    }
    currentAttrMap = EntityTOUtils.buildAttrMap(anyTO.getDerAttrs());
    for (AttrTO templateDerAttr : template.getDerAttrs()) {
        if (!currentAttrMap.containsKey(templateDerAttr.getSchema())) {
            anyTO.getDerAttrs().add(templateDerAttr);
        }
    }
    currentAttrMap = EntityTOUtils.buildAttrMap(anyTO.getVirAttrs());
    for (AttrTO templateVirAttr : template.getVirAttrs()) {
        if (!templateVirAttr.getValues().isEmpty() && (!currentAttrMap.containsKey(templateVirAttr.getSchema()) || currentAttrMap.get(templateVirAttr.getSchema()).getValues().isEmpty())) {
            AttrTO evaluated = evaluateAttr(templateVirAttr, jexlContext);
            if (!evaluated.getValues().isEmpty()) {
                anyTO.getVirAttrs().add(evaluated);
                jexlContext.set(evaluated.getSchema(), evaluated.getValues().get(0));
            }
        }
    }
    anyTO.getResources().addAll(template.getResources());
    anyTO.getAuxClasses().addAll(template.getAuxClasses());
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO) MapContext(org.apache.commons.jexl3.MapContext)

Example 55 with AttrTO

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

the class TemplateUtils method evaluateAttr.

private AttrTO evaluateAttr(final AttrTO template, final MapContext jexlContext) {
    AttrTO result = new AttrTO();
    result.setSchema(template.getSchema());
    if (template.getValues() != null && !template.getValues().isEmpty()) {
        template.getValues().forEach(value -> {
            String evaluated = JexlUtils.evaluate(value, jexlContext);
            if (StringUtils.isNotBlank(evaluated)) {
                result.getValues().add(evaluated);
            }
        });
    }
    return result;
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO)

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