Search in sources :

Example 66 with AttrTO

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

the class ConfigurationUpdate method update.

public void update() {
    if (input.parameterNumber() >= 1) {
        List<AttrTO> attrList = new ArrayList<>();
        boolean failed = false;
        for (String parameter : input.getParameters()) {
            Pair<String, String> pairParameter = Input.toPairParameter(parameter);
            try {
                AttrTO attrTO = configurationSyncopeOperations.get(pairParameter.getKey());
                attrTO.getValues().clear();
                attrTO.getValues().add(pairParameter.getValue());
                configurationSyncopeOperations.set(attrTO);
                attrList.add(attrTO);
            } catch (IllegalArgumentException ex) {
                LOG.error("Error updating configuration", ex);
                configurationResultManager.genericError(ex.getMessage());
                configurationResultManager.genericError(UPDATE_HELP_MESSAGE);
                failed = true;
                break;
            } catch (SyncopeClientException | WebServiceException ex) {
                LOG.error("Error updating configuration", ex);
                if (ex.getMessage().startsWith("NotFound")) {
                    configurationResultManager.notFoundError("Configuration", pairParameter.getKey());
                } else if (ex.getMessage().startsWith("InvalidValues")) {
                    configurationResultManager.genericError(pairParameter.getValue() + " is not a valid value for " + pairParameter.getKey());
                } else {
                    configurationResultManager.genericError(ex.getMessage());
                }
                failed = true;
                break;
            }
        }
        if (!failed) {
            configurationResultManager.fromUpdate(attrList);
        }
    } else {
        configurationResultManager.commandOptionError(UPDATE_HELP_MESSAGE);
    }
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) AttrTO(org.apache.syncope.common.lib.to.AttrTO) ArrayList(java.util.ArrayList) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException)

Example 67 with AttrTO

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

the class UserResultManager method printAttributes.

private void printAttributes(final Set<AttrTO> derAttrTOs) {
    for (final AttrTO attrTO : derAttrTOs) {
        final StringBuilder attributeSentence = new StringBuilder();
        attributeSentence.append("       ").append(attrTO.getSchema()).append(": ").append(attrTO.getValues());
        System.out.println(attributeSentence);
    }
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO)

Example 68 with AttrTO

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

the class ParametersDirectoryPanel method getColumns.

@Override
protected List<IColumn<AttrTO, String>> getColumns() {
    final List<IColumn<AttrTO, String>> columns = new ArrayList<>();
    columns.add(new PropertyColumn<>(new ResourceModel("schema"), "schema"));
    columns.add(new PropertyColumn<AttrTO, String>(new ResourceModel("values"), "values") {

        private static final long serialVersionUID = -1822504503325964706L;

        @Override
        public void populateItem(final Item<ICellPopulator<AttrTO>> item, final String componentId, final IModel<AttrTO> rowModel) {
            PlainSchemaTO modelSchemaTO = (PlainSchemaTO) rowModel.getObject().getSchemaInfo();
            AttrSchemaType type = modelSchemaTO.getType();
            if (type == AttrSchemaType.Binary || type == AttrSchemaType.Encrypted) {
                item.add(new Label(componentId, type.name()).add(new AttributeModifier("style", "font-style:italic")));
            } else {
                super.populateItem(item, componentId, rowModel);
            }
        }
    });
    return columns;
}
Also used : ArrayList(java.util.ArrayList) AttrTO(org.apache.syncope.common.lib.to.AttrTO) Label(org.apache.wicket.markup.html.basic.Label) AttributeModifier(org.apache.wicket.AttributeModifier) ICellPopulator(org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator) PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) IColumn(org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) StringResourceModel(org.apache.wicket.model.StringResourceModel) ResourceModel(org.apache.wicket.model.ResourceModel)

Example 69 with AttrTO

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

the class UserSelfCreateResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    ResourceResponse response = new ResourceResponse();
    response.setContentType(MediaType.TEXT_PLAIN);
    try {
        HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
        if (!xsrfCheck(request)) {
            LOG.error("XSRF TOKEN is not matching");
            response.setError(Response.Status.BAD_REQUEST.getStatusCode(), "XSRF TOKEN is not matching");
            return response;
        }
        String jsonString = request.getReader().readLine();
        final UserTO userTO = MAPPER.readValue(jsonString, UserTO.class);
        if (!captchaCheck(request.getHeader("captcha"), request.getSession().getAttribute(SyncopeEnduserConstants.CAPTCHA_SESSION_KEY))) {
            throw new IllegalArgumentException("Entered captcha is not matching");
        }
        if (isSelfRegistrationAllowed() && userTO != null) {
            LOG.debug("User self registration request for [{}]", userTO.getUsername());
            LOG.trace("Request is [{}]", userTO);
            // check if request is compliant with customization form rules
            if (UserRequestValidator.compliant(userTO, SyncopeEnduserApplication.get().getCustomForm(), true)) {
                // 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(memb -> simpleAttrs[0].equals(memb.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(memb -> simpleAttrs[0].equals(memb.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(memb -> simpleAttrs[0].equals(memb.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);
                LOG.debug("Received user self registration request for user: [{}]", userTO.getUsername());
                LOG.trace("Received user self registration request is: [{}]", userTO);
                // adapt request and create user
                final Response res = SyncopeEnduserSession.get().getService(UserSelfService.class).create(userTO, true);
                buildResponse(response, res.getStatus(), Response.Status.Family.SUCCESSFUL.equals(res.getStatusInfo().getFamily()) ? "User[ " + userTO.getUsername() + "] successfully created" : "ErrorMessage{{ " + res.getStatusInfo().getReasonPhrase() + " }}");
            } else {
                LOG.warn("Incoming create request [{}] is not compliant with form customization rules. " + "Create NOT allowed", userTO.getUsername());
                buildResponse(response, Response.Status.OK.getStatusCode(), "User: " + userTO.getUsername() + " successfully created");
            }
        } else {
            response.setError(Response.Status.FORBIDDEN.getStatusCode(), new StringBuilder().append("ErrorMessage{{").append(userTO == null ? "Request received is not valid }}" : "Self registration not allowed }}").toString());
        }
    } catch (Exception e) {
        LOG.error("Unable to create userTO", e);
        response.setError(Response.Status.BAD_REQUEST.getStatusCode(), new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
    }
    return response;
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) AttrTO(org.apache.syncope.common.lib.to.AttrTO) Set(java.util.Set) SerializationUtils(org.apache.commons.lang3.SerializationUtils) 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) Response(javax.ws.rs.core.Response) UserRequestValidator(org.apache.syncope.client.enduser.util.UserRequestValidator) SyncopeEnduserApplication(org.apache.syncope.client.enduser.SyncopeEnduserApplication) Resource(org.apache.syncope.client.enduser.annotations.Resource) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) UserSelfService(org.apache.syncope.common.rest.api.service.UserSelfService) AttrTO(org.apache.syncope.common.lib.to.AttrTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) 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) HashSet(java.util.HashSet)

Example 70 with AttrTO

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

the class BaseUserSelfResource method dateToMillis.

protected void dateToMillis(final Set<AttrTO> attrs, final PlainSchemaTO plainSchema) throws ParseException {
    final FastDateFormat fmt = FastDateFormat.getInstance(plainSchema.getConversionPattern());
    attrs.stream().filter(attr -> (attr.getSchema().equals(plainSchema.getKey()))).forEachOrdered(attr -> {
        for (ListIterator<String> itor = attr.getValues().listIterator(); itor.hasNext(); ) {
            String value = itor.next();
            try {
                itor.set(String.valueOf(fmt.parse(value).getTime()));
            } catch (ParseException ex) {
                LOG.error("Unable to parse date {}", value);
            }
        }
    });
}
Also used : PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) ListIterator(java.util.ListIterator) AttrTO(org.apache.syncope.common.lib.to.AttrTO) Set(java.util.Set) IOException(java.io.IOException) ParseException(java.text.ParseException) StandardCharsets(java.nio.charset.StandardCharsets) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) ParseException(java.text.ParseException)

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