Search in sources :

Example 1 with AttrTO

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

the class UserSelfUpdateResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(final IResource.Attributes attributes) {
    ResourceResponse response = new AbstractResource.ResourceResponse();
    response.setContentType(MediaType.TEXT_PLAIN);
    try {
        HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
        if (!xsrfCheck(request)) {
            LOG.error("XSRF TOKEN does not match");
            response.setError(Response.Status.BAD_REQUEST.getStatusCode(), "XSRF TOKEN does not match");
            return response;
        }
        if (!captchaCheck(request.getHeader("captcha"), request.getSession().getAttribute(SyncopeEnduserConstants.CAPTCHA_SESSION_KEY))) {
            throw new IllegalArgumentException("Entered captcha is not matching");
        }
        UserTO userTO = MAPPER.readValue(request.getReader().readLine(), UserTO.class);
        Map<String, CustomAttributesInfo> customForm = SyncopeEnduserApplication.get().getCustomForm();
        // check if request is compliant with customization form rules
        if (UserRequestValidator.compliant(userTO, customForm, false)) {
            // 1. membership attributes management
            Set<AttrTO> membAttrs = new HashSet<>();
            userTO.getPlainAttrs().stream().filter(attr -> (attr.getSchema().contains(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR))).forEachOrdered((attr) -> {
                String[] simpleAttrs = attr.getSchema().split(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR);
                MembershipTO membership = userTO.getMemberships().stream().filter(item -> simpleAttrs[0].equals(item.getGroupName())).findFirst().orElse(null);
                if (membership == null) {
                    membership = new MembershipTO.Builder().group(null, simpleAttrs[0]).build();
                    userTO.getMemberships().add(membership);
                }
                AttrTO clone = SerializationUtils.clone(attr);
                clone.setSchema(simpleAttrs[1]);
                membership.getPlainAttrs().add(clone);
                membAttrs.add(attr);
            });
            userTO.getPlainAttrs().removeAll(membAttrs);
            // 2. millis -> Date conversion for PLAIN attributes of USER and its MEMBERSHIPS
            SyncopeEnduserSession.get().getDatePlainSchemas().stream().map(plainSchema -> {
                millisToDate(userTO.getPlainAttrs(), plainSchema);
                return plainSchema;
            }).forEachOrdered(plainSchema -> {
                userTO.getMemberships().forEach(membership -> {
                    millisToDate(membership.getPlainAttrs(), plainSchema);
                });
            });
            membAttrs.clear();
            userTO.getDerAttrs().stream().filter(attr -> (attr.getSchema().contains(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR))).forEachOrdered(attr -> {
                String[] simpleAttrs = attr.getSchema().split(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR);
                MembershipTO membership = userTO.getMemberships().stream().filter(item -> simpleAttrs[0].equals(item.getGroupName())).findFirst().orElse(null);
                if (membership == null) {
                    membership = new MembershipTO.Builder().group(null, simpleAttrs[0]).build();
                    userTO.getMemberships().add(membership);
                }
                AttrTO clone = SerializationUtils.clone(attr);
                clone.setSchema(simpleAttrs[1]);
                membership.getDerAttrs().add(clone);
                membAttrs.add(attr);
            });
            userTO.getDerAttrs().removeAll(membAttrs);
            membAttrs.clear();
            userTO.getVirAttrs().stream().filter(attr -> (attr.getSchema().contains(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR))).forEachOrdered((attr) -> {
                String[] simpleAttrs = attr.getSchema().split(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR);
                MembershipTO membership = userTO.getMemberships().stream().filter(item -> simpleAttrs[0].equals(item.getGroupName())).findFirst().orElse(null);
                if (membership == null) {
                    membership = new MembershipTO.Builder().group(null, simpleAttrs[0]).build();
                    userTO.getMemberships().add(membership);
                }
                AttrTO clone = SerializationUtils.clone(attr);
                clone.setSchema(simpleAttrs[1]);
                membership.getVirAttrs().add(clone);
                membAttrs.add(attr);
            });
            userTO.getVirAttrs().removeAll(membAttrs);
            // get old user object from session
            UserTO selfTO = SyncopeEnduserSession.get().getSelfTO();
            // align "userTO" and "selfTO" objects
            if (customForm != null && !customForm.isEmpty()) {
                completeUserObject(userTO, selfTO);
            }
            // create diff patch
            UserPatch userPatch = AnyOperations.diff(userTO, selfTO, false);
            // update user by patch
            Response res = SyncopeEnduserSession.get().getService(userTO.getETagValue(), UserSelfService.class).update(userPatch);
            buildResponse(response, res.getStatus(), res.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL) ? "User [" + userTO.getUsername() + "] successfully updated" : "ErrorMessage{{ " + res.getStatusInfo().getReasonPhrase() + " }}");
        } else {
            LOG.warn("Incoming update request [{}] is not compliant with form customization rules." + " Update NOT allowed", userTO.getUsername());
            buildResponse(response, Response.Status.OK.getStatusCode(), "User: " + userTO.getUsername() + " successfully created");
        }
    } catch (final Exception e) {
        LOG.error("Error while updating user", e);
        response.setError(Response.Status.BAD_REQUEST.getStatusCode(), new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
    }
    return response;
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO) EntityTOUtils(org.apache.syncope.common.lib.EntityTOUtils) Set(java.util.Set) AbstractResource(org.apache.wicket.request.resource.AbstractResource) SerializationUtils(org.apache.commons.lang3.SerializationUtils) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) UserSelfService(org.apache.syncope.common.rest.api.service.UserSelfService) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) SyncopeEnduserConstants(org.apache.syncope.client.enduser.SyncopeEnduserConstants) SyncopeEnduserSession(org.apache.syncope.client.enduser.SyncopeEnduserSession) IResource(org.apache.wicket.request.resource.IResource) Response(javax.ws.rs.core.Response) UserRequestValidator(org.apache.syncope.client.enduser.util.UserRequestValidator) Map(java.util.Map) Optional(java.util.Optional) SyncopeEnduserApplication(org.apache.syncope.client.enduser.SyncopeEnduserApplication) CustomAttributesInfo(org.apache.syncope.client.enduser.model.CustomAttributesInfo) Resource(org.apache.syncope.client.enduser.annotations.Resource) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) AnyOperations(org.apache.syncope.common.lib.AnyOperations) UserSelfService(org.apache.syncope.common.rest.api.service.UserSelfService) AttrTO(org.apache.syncope.common.lib.to.AttrTO) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) CustomAttributesInfo(org.apache.syncope.client.enduser.model.CustomAttributesInfo) HashSet(java.util.HashSet)

Example 2 with AttrTO

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

the class AnyOperations method patch.

private static Collection<AttrTO> patch(final Map<String, AttrTO> attrs, final Set<AttrPatch> attrPatches) {
    Map<String, AttrTO> rwattrs = new HashMap<>(attrs);
    attrPatches.forEach(patch -> {
        if (patch.getAttrTO() == null) {
            LOG.warn("Invalid {} specified: {}", AttrPatch.class.getName(), patch);
        } else {
            AttrTO removed = rwattrs.remove(patch.getAttrTO().getSchema());
            if (removed != null && removed.getSchemaInfo() != null) {
                patch.getAttrTO().setSchemaInfo(removed.getSchemaInfo());
            }
            if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
                rwattrs.put(patch.getAttrTO().getSchema(), patch.getAttrTO());
            }
        }
    });
    return rwattrs.values();
}
Also used : HashMap(java.util.HashMap) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AttrPatch(org.apache.syncope.common.lib.patch.AttrPatch)

Example 3 with AttrTO

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

the class VirAttrs method setAttrs.

@Override
protected void setAttrs(final MembershipTO membershipTO) {
    List<AttrTO> attrs = new ArrayList<>();
    Map<String, AttrTO> attrMap = EntityTOUtils.buildAttrMap(anyTO.getVirAttrs());
    membershipSchemas.get(membershipTO.getGroupKey()).values().stream().map(schema -> {
        AttrTO attrTO = new AttrTO();
        attrTO.setSchema(schema.getKey());
        if (attrMap.containsKey(schema.getKey())) {
            attrTO.getValues().addAll(attrMap.get(schema.getKey()).getValues());
        } else {
            attrTO.getValues().add(StringUtils.EMPTY);
        }
        return attrTO;
    }).forEachOrdered(attrTO -> {
        attrs.add(attrTO);
    });
    membershipTO.getVirAttrs().clear();
    membershipTO.getVirAttrs().addAll(attrs);
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO) StringResourceModel(org.apache.wicket.model.StringResourceModel) EntityTOUtils(org.apache.syncope.common.lib.EntityTOUtils) AnyTO(org.apache.syncope.common.lib.to.AnyTO) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ITab(org.apache.wicket.extensions.markup.html.tabs.ITab) Map(java.util.Map) AjaxTextFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AjaxTextFieldPanel) IModel(org.apache.wicket.model.IModel) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) ListView(org.apache.wicket.markup.html.list.ListView) ListModel(org.apache.wicket.model.util.ListModel) Accordion(org.apache.syncope.client.console.wicket.markup.html.bootstrap.tabs.Accordion) ListItem(org.apache.wicket.markup.html.list.ListItem) AbstractTab(org.apache.wicket.extensions.markup.html.tabs.AbstractTab) Model(org.apache.wicket.model.Model) SchemaType(org.apache.syncope.common.lib.types.SchemaType) Collectors(java.util.stream.Collectors) VirSchemaTO(org.apache.syncope.common.lib.to.VirSchemaTO) LabelInfo(org.apache.syncope.client.console.wicket.ajax.markup.html.LabelInfo) List(java.util.List) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) PropertyModel(org.apache.wicket.model.PropertyModel) AjaxWizard(org.apache.syncope.client.console.wizards.AjaxWizard) MultiFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.MultiFieldPanel) AbstractFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AbstractFieldPanel) ResourceModel(org.apache.wicket.model.ResourceModel) Collections(java.util.Collections) AttrTO(org.apache.syncope.common.lib.to.AttrTO) ArrayList(java.util.ArrayList)

Example 4 with AttrTO

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

the class VirAttrs method setAttrs.

@Override
protected void setAttrs() {
    List<AttrTO> attrs = new ArrayList<>();
    Map<String, AttrTO> attrMap = EntityTOUtils.buildAttrMap(anyTO.getVirAttrs());
    schemas.values().stream().map(schema -> {
        AttrTO attrTO = new AttrTO();
        attrTO.setSchema(schema.getKey());
        if (attrMap.containsKey(schema.getKey())) {
            attrTO.getValues().addAll(attrMap.get(schema.getKey()).getValues());
        } else {
            attrTO.getValues().add(StringUtils.EMPTY);
        }
        return attrTO;
    }).forEachOrdered(attrTO -> {
        attrs.add(attrTO);
    });
    anyTO.getVirAttrs().clear();
    anyTO.getVirAttrs().addAll(attrs);
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO) StringResourceModel(org.apache.wicket.model.StringResourceModel) EntityTOUtils(org.apache.syncope.common.lib.EntityTOUtils) AnyTO(org.apache.syncope.common.lib.to.AnyTO) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ITab(org.apache.wicket.extensions.markup.html.tabs.ITab) Map(java.util.Map) AjaxTextFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AjaxTextFieldPanel) IModel(org.apache.wicket.model.IModel) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) ListView(org.apache.wicket.markup.html.list.ListView) ListModel(org.apache.wicket.model.util.ListModel) Accordion(org.apache.syncope.client.console.wicket.markup.html.bootstrap.tabs.Accordion) ListItem(org.apache.wicket.markup.html.list.ListItem) AbstractTab(org.apache.wicket.extensions.markup.html.tabs.AbstractTab) Model(org.apache.wicket.model.Model) SchemaType(org.apache.syncope.common.lib.types.SchemaType) Collectors(java.util.stream.Collectors) VirSchemaTO(org.apache.syncope.common.lib.to.VirSchemaTO) LabelInfo(org.apache.syncope.client.console.wicket.ajax.markup.html.LabelInfo) List(java.util.List) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) PropertyModel(org.apache.wicket.model.PropertyModel) AjaxWizard(org.apache.syncope.client.console.wizards.AjaxWizard) MultiFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.MultiFieldPanel) AbstractFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AbstractFieldPanel) ResourceModel(org.apache.wicket.model.ResourceModel) Collections(java.util.Collections) AttrTO(org.apache.syncope.common.lib.to.AttrTO) ArrayList(java.util.ArrayList)

Example 5 with AttrTO

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

the class PlainAttrs method setAttrs.

@Override
protected void setAttrs() {
    List<AttrTO> attrs = new ArrayList<>();
    Map<String, AttrTO> attrMap = EntityTOUtils.buildAttrMap(anyTO.getPlainAttrs());
    schemas.values().stream().map(schema -> {
        AttrTO attrTO = new AttrTO();
        attrTO.setSchema(schema.getKey());
        if (attrMap.get(schema.getKey()) == null || attrMap.get(schema.getKey()).getValues().isEmpty()) {
            attrTO.getValues().add("");
            // is important to set the schema info only after values setting
            attrTO.setSchemaInfo(schema);
        } else {
            attrTO = attrMap.get(schema.getKey());
        }
        return attrTO;
    }).forEachOrdered(attrTO -> {
        attrs.add(attrTO);
    });
    anyTO.getPlainAttrs().clear();
    anyTO.getPlainAttrs().addAll(attrs);
}
Also used : Form(org.apache.wicket.markup.html.form.Form) AttrTO(org.apache.syncope.common.lib.to.AttrTO) StringResourceModel(org.apache.wicket.model.StringResourceModel) EntityTOUtils(org.apache.syncope.common.lib.EntityTOUtils) AnyTO(org.apache.syncope.common.lib.to.AnyTO) FieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.FieldPanel) StringUtils(org.apache.commons.lang3.StringUtils) AjaxDropDownChoicePanel(org.apache.syncope.client.console.wicket.markup.html.form.AjaxDropDownChoicePanel) ArrayList(java.util.ArrayList) AjaxCheckBoxPanel(org.apache.syncope.client.console.wicket.markup.html.form.AjaxCheckBoxPanel) AjaxSpinnerFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AjaxSpinnerFieldPanel) PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) EncryptedFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.EncryptedFieldPanel) SchemaUtils(org.apache.syncope.client.console.commons.SchemaUtils) ITab(org.apache.wicket.extensions.markup.html.tabs.ITab) Map(java.util.Map) ListUtils(org.apache.commons.collections4.ListUtils) AjaxTextFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AjaxTextFieldPanel) IModel(org.apache.wicket.model.IModel) AjaxDateTimeFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AjaxDateTimeFieldPanel) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) ListView(org.apache.wicket.markup.html.list.ListView) IChoiceRenderer(org.apache.wicket.markup.html.form.IChoiceRenderer) ListModel(org.apache.wicket.model.util.ListModel) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) Accordion(org.apache.syncope.client.console.wicket.markup.html.bootstrap.tabs.Accordion) ListItem(org.apache.wicket.markup.html.list.ListItem) AbstractTab(org.apache.wicket.extensions.markup.html.tabs.AbstractTab) Model(org.apache.wicket.model.Model) SchemaType(org.apache.syncope.common.lib.types.SchemaType) PageReference(org.apache.wicket.PageReference) GroupTO(org.apache.syncope.common.lib.to.GroupTO) Collectors(java.util.stream.Collectors) AjaxDateFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AjaxDateFieldPanel) LabelInfo(org.apache.syncope.client.console.wicket.ajax.markup.html.LabelInfo) List(java.util.List) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) PropertyModel(org.apache.wicket.model.PropertyModel) AjaxWizard(org.apache.syncope.client.console.wizards.AjaxWizard) MultiFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.MultiFieldPanel) BinaryFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.BinaryFieldPanel) AbstractFieldPanel(org.apache.syncope.client.console.wicket.markup.html.form.AbstractFieldPanel) UserTO(org.apache.syncope.common.lib.to.UserTO) ResourceModel(org.apache.wicket.model.ResourceModel) Collections(java.util.Collections) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) AttrTO(org.apache.syncope.common.lib.to.AttrTO) ArrayList(java.util.ArrayList)

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