Search in sources :

Example 1 with UserResource

use of io.jans.scim.model.scim2.user.UserResource in project oxTrust by GluuFederation.

the class Scim2UserService method updateUser.

public UserResource updateUser(String id, UserResource user, String url) throws InvalidAttributeValueException {
    // This is never null (see decorator involved)
    GluuCustomPerson gluuPerson = personService.getPersonByInum(id);
    UserResource tmpUser = new UserResource();
    transferAttributesToUserResource(gluuPerson, tmpUser, url);
    long now = System.currentTimeMillis();
    tmpUser.getMeta().setLastModified(ISODateTimeFormat.dateTime().withZoneUTC().print(now));
    tmpUser = (UserResource) ScimResourceUtil.transferToResourceReplace(user, tmpUser, extService.getResourceExtensions(user.getClass()));
    replacePersonInfo(gluuPerson, tmpUser, url);
    return tmpUser;
}
Also used : GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) UserResource(org.gluu.oxtrust.model.scim2.user.UserResource)

Example 2 with UserResource

use of io.jans.scim.model.scim2.user.UserResource in project jans by JanssenProject.

the class BulkWebService method execute.

private Pair<Response, String> execute(Verb verb, BaseScimWebService ws, String data, String fragment) {
    Response response = null;
    String idCreated = null;
    try {
        if (ws == userWS)
            switch(verb) {
                case PUT:
                    UserResource user = mapper.readValue(data, UserResource.class);
                    response = userWS.updateUser(user, fragment, "id", null);
                    break;
                case DELETE:
                    response = userWS.deleteUser(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = userWS.patchUser(pr, fragment, "id", null);
                    break;
                case POST:
                    user = mapper.readValue(data, UserResource.class);
                    response = userWS.createUser(user, "id", null);
                    if (CREATED.getStatusCode() == response.getStatus()) {
                        user = mapper.readValue(response.getEntity().toString(), UserResource.class);
                        idCreated = user.getId();
                    }
                    break;
            }
        else if (ws == groupWS)
            switch(verb) {
                case PUT:
                    GroupResource group = mapper.readValue(data, GroupResource.class);
                    response = groupWS.updateGroup(group, fragment, "id", null);
                    break;
                case DELETE:
                    response = groupWS.deleteGroup(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = groupWS.patchGroup(pr, fragment, "id", null);
                    break;
                case POST:
                    group = mapper.readValue(data, GroupResource.class);
                    response = groupWS.createGroup(group, "id", null);
                    if (CREATED.getStatusCode() == response.getStatus()) {
                        group = mapper.readValue(response.getEntity().toString(), GroupResource.class);
                        idCreated = group.getId();
                    }
                    break;
            }
        else if (ws == fidoDeviceWS)
            switch(verb) {
                case PUT:
                    FidoDeviceResource dev = mapper.readValue(data, FidoDeviceResource.class);
                    response = fidoDeviceWS.updateDevice(dev, fragment, "id", null);
                    break;
                case DELETE:
                    response = fidoDeviceWS.deleteDevice(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = fidoDeviceWS.patchDevice(pr, fragment, "id", null);
                    break;
                case POST:
                    response = fidoDeviceWS.createDevice();
                    break;
            }
        else if (ws == fido2DeviceWS)
            switch(verb) {
                case PUT:
                    Fido2DeviceResource dev = mapper.readValue(data, Fido2DeviceResource.class);
                    response = fido2DeviceWS.updateF2Device(dev, fragment, "id", null);
                    break;
                case DELETE:
                    response = fido2DeviceWS.deleteF2Device(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = fido2DeviceWS.patchF2Device(pr, fragment, "id", null);
                    break;
                case POST:
                    response = fido2DeviceWS.createDevice();
                    break;
            }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return new Pair<>(response, idCreated);
}
Also used : BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) Response(javax.ws.rs.core.Response) Fido2DeviceResource(io.jans.scim.model.scim2.fido.Fido2DeviceResource) FidoDeviceResource(io.jans.scim.model.scim2.fido.FidoDeviceResource) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) GroupResource(io.jans.scim.model.scim2.group.GroupResource) Pair(io.jans.util.Pair)

Example 3 with UserResource

use of io.jans.scim.model.scim2.user.UserResource in project jans by JanssenProject.

the class Scim2UserService method transferAttributesToUserResource.

public void transferAttributesToUserResource(ScimCustomPerson person, UserResource res, String url) {
    log.debug("transferAttributesToUserResource");
    res.setId(person.getInum());
    res.setExternalId(person.getAttribute("jansExtId"));
    Meta meta = new Meta();
    meta.setResourceType(ScimResourceUtil.getType(res.getClass()));
    meta.setCreated(person.getAttribute("jansMetaCreated"));
    if (meta.getCreated() == null) {
        Date date = person.getCreationDate();
        meta.setCreated(date == null ? null : DateUtil.millisToISOString(date.getTime()));
    }
    meta.setLastModified(person.getAttribute("jansMetaLastMod"));
    if (meta.getLastModified() == null) {
        Date date = person.getUpdatedAt();
        meta.setLastModified(date == null ? null : DateUtil.millisToISOString(date.getTime()));
    }
    meta.setLocation(person.getAttribute("jansMetaLocation"));
    if (meta.getLocation() == null) {
        meta.setLocation(url + "/" + person.getInum());
    }
    res.setMeta(meta);
    // Set values in order of appearance in UserResource class
    res.setUserName(person.getUid());
    Name name = new Name();
    name.setGivenName(person.getGivenName());
    name.setFamilyName(person.getSurname());
    name.setMiddleName(person.getAttribute("middleName"));
    name.setHonorificPrefix(person.getAttribute("jansHonorificPrefix"));
    name.setHonorificSuffix(person.getAttribute("jansHonorificSuffix"));
    String formatted = person.getAttribute("jansNameFormatted");
    if (formatted == null) {
        // recomputes the formatted name if absent in LDAP
        name.computeFormattedName();
    } else {
        name.setFormatted(formatted);
    }
    res.setName(name);
    res.setDisplayName(person.getDisplayName());
    res.setNickName(person.getAttribute("nickname"));
    res.setProfileUrl(person.getAttribute("jansProfileURL"));
    res.setTitle(person.getAttribute("jansTitle"));
    res.setUserType(person.getAttribute("jansUsrTyp"));
    res.setPreferredLanguage(person.getPreferredLanguage());
    res.setLocale(person.getAttribute("locale"));
    res.setTimezone(person.getTimezone());
    res.setActive(Boolean.valueOf(person.getAttribute("jansActive")) || GluuBoolean.getByValue(person.getAttribute("jansStatus")).isBooleanValue());
    res.setPassword(person.getUserPassword());
    res.setEmails(getAttributeListValue(person, Email.class, "jansEmail"));
    if (res.getEmails() == null) {
        // There can be cases where jansEmail is not synced with mail attribute....
        List<Email> emails = person.getAttributeList("mail").stream().map(m -> {
            Email email = new Email();
            email.setValue(m);
            email.setPrimary(false);
            return email;
        }).collect(Collectors.toList());
        res.setEmails(emails.size() == 0 ? null : emails);
    }
    res.setPhoneNumbers(getAttributeListValue(person, PhoneNumber.class, "jansPhoneValue"));
    res.setIms(getAttributeListValue(person, InstantMessagingAddress.class, "jansImsValue"));
    res.setPhotos(getAttributeListValue(person, Photo.class, "jansPhotos"));
    res.setAddresses(getAttributeListValue(person, Address.class, "jansAddres"));
    List<String> listOfGroups = person.getMemberOf();
    if (listOfGroups != null && listOfGroups.size() > 0) {
        List<Group> groupList = new ArrayList<>();
        for (String groupDN : listOfGroups) {
            try {
                GluuGroup gluuGroup = groupService.getGroupByDn(groupDN);
                Group group = new Group();
                group.setValue(gluuGroup.getInum());
                String reference = groupEndpointUrl + "/" + gluuGroup.getInum();
                group.setRef(reference);
                group.setDisplay(gluuGroup.getDisplayName());
                // Only support direct membership: see section 4.1.2 of RFC 7644
                group.setType(Group.Type.DIRECT);
                groupList.add(group);
            } catch (Exception e) {
                log.warn("transferAttributesToUserResource. Group with dn {} could not be added to User Resource. {}", groupDN, person.getUid());
                log.error(e.getMessage(), e);
            }
        }
        if (groupList.size() > 0) {
            res.setGroups(groupList);
        }
    }
    res.setEntitlements(getAttributeListValue(person, Entitlement.class, "jansEntitlements"));
    res.setRoles(getAttributeListValue(person, Role.class, "jansRole"));
    res.setX509Certificates(getAttributeListValue(person, X509Certificate.class, "jansx509Certificate"));
    res.setPairwiseIdentifiers(person.getPpid());
    transferExtendedAttributesToResource(person, res);
}
Also used : ExtensionField(io.jans.scim.model.scim2.extensions.ExtensionField) StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) Path(javax.ws.rs.Path) Meta(io.jans.scim.model.scim2.Meta) PhoneNumber(io.jans.scim.model.scim2.user.PhoneNumber) GluuStatus(io.jans.model.GluuStatus) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) Map(java.util.Map) GluuGroup(io.jans.scim.model.GluuGroup) SortOrder(io.jans.orm.model.SortOrder) ScimCustomPerson(io.jans.scim.model.scim.ScimCustomPerson) UserResource(io.jans.scim.model.scim2.user.UserResource) Collection(java.util.Collection) IntrospectUtil(io.jans.scim.model.scim2.util.IntrospectUtil) AppConfiguration(io.jans.scim.model.conf.AppConfiguration) Address(io.jans.scim.model.scim2.user.Address) ScimFilterParserService(io.jans.scim.service.antlr.scimFilter.ScimFilterParserService) Collectors(java.util.stream.Collectors) PersistenceEntryManager(io.jans.orm.PersistenceEntryManager) X509Certificate(io.jans.scim.model.scim2.user.X509Certificate) Serializable(java.io.Serializable) List(java.util.List) Group(io.jans.scim.model.scim2.user.Group) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) GluuBoolean(io.jans.scim.model.GluuBoolean) GroupService(io.jans.scim.service.GroupService) Photo(io.jans.scim.model.scim2.user.Photo) Filter(io.jans.orm.search.filter.Filter) Extension(io.jans.scim.model.scim2.extensions.Extension) Email(io.jans.scim.model.scim2.user.Email) HashMap(java.util.HashMap) DateUtil(io.jans.scim.model.scim2.util.DateUtil) PairwiseIdentifier(io.jans.as.persistence.model.PairwiseIdentifier) ArrayList(java.util.ArrayList) ScimResourceUtil(io.jans.scim.model.scim2.util.ScimResourceUtil) Inject(javax.inject.Inject) InstantMessagingAddress(io.jans.scim.model.scim2.user.InstantMessagingAddress) PersonService(io.jans.scim.service.PersonService) Role(io.jans.scim.model.scim2.user.Role) Entitlement(io.jans.scim.model.scim2.user.Entitlement) GroupWebService(io.jans.scim.ws.rs.scim2.GroupWebService) ServiceUtil(io.jans.scim.util.ServiceUtil) Status(javax.ws.rs.core.Response.Status) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Name(io.jans.scim.model.scim2.user.Name) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) ExternalScimService(io.jans.scim.service.external.ExternalScimService) PagedResult(io.jans.orm.model.PagedResult) Meta(io.jans.scim.model.scim2.Meta) GluuGroup(io.jans.scim.model.GluuGroup) Group(io.jans.scim.model.scim2.user.Group) Email(io.jans.scim.model.scim2.user.Email) Address(io.jans.scim.model.scim2.user.Address) InstantMessagingAddress(io.jans.scim.model.scim2.user.InstantMessagingAddress) ArrayList(java.util.ArrayList) Photo(io.jans.scim.model.scim2.user.Photo) GluuGroup(io.jans.scim.model.GluuGroup) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) X509Certificate(io.jans.scim.model.scim2.user.X509Certificate) Name(io.jans.scim.model.scim2.user.Name) Role(io.jans.scim.model.scim2.user.Role) PhoneNumber(io.jans.scim.model.scim2.user.PhoneNumber) Entitlement(io.jans.scim.model.scim2.user.Entitlement) InstantMessagingAddress(io.jans.scim.model.scim2.user.InstantMessagingAddress)

Example 4 with UserResource

use of io.jans.scim.model.scim2.user.UserResource in project jans by JanssenProject.

the class Scim2UserService method updateUser.

public UserResource updateUser(ScimCustomPerson gluuPerson, UserResource user, String url) throws InvalidAttributeValueException {
    UserResource tmpUser = new UserResource();
    transferAttributesToUserResource(gluuPerson, tmpUser, url);
    tmpUser.getMeta().setLastModified(DateUtil.millisToISOString(System.currentTimeMillis()));
    tmpUser = (UserResource) ScimResourceUtil.transferToResourceReplace(user, tmpUser, extService.getResourceExtensions(user.getClass()));
    replacePersonInfo(gluuPerson, tmpUser, url);
    return tmpUser;
}
Also used : UserResource(io.jans.scim.model.scim2.user.UserResource)

Example 5 with UserResource

use of io.jans.scim.model.scim2.user.UserResource in project jans by JanssenProject.

the class Scim2UserService method searchUsers.

public PagedResult<BaseScimResource> searchUsers(String filter, String sortBy, SortOrder sortOrder, int startIndex, int count, String url, int maxCount) throws Exception {
    Filter ldapFilter = scimFilterParserService.createFilter(filter, Filter.createPresenceFilter("inum"), UserResource.class);
    log.info("Executing search for users using: ldapfilter '{}', sortBy '{}', sortOrder '{}', startIndex '{}', count '{}'", ldapFilter.toString(), sortBy, sortOrder.getValue(), startIndex, count);
    PagedResult<ScimCustomPerson> list = ldapEntryManager.findPagedEntries(personService.getDnForPerson(null), ScimCustomPerson.class, ldapFilter, null, sortBy, sortOrder, startIndex - 1, count, maxCount);
    List<BaseScimResource> resources = new ArrayList<>();
    if (externalScimService.isEnabled() && !externalScimService.executeScimPostSearchUsersMethods(list)) {
        throw new WebApplicationException("Failed to execute SCIM script successfully", Status.PRECONDITION_FAILED);
    }
    for (ScimCustomPerson person : list.getEntries()) {
        UserResource scimUsr = new UserResource();
        transferAttributesToUserResource(person, scimUsr, url);
        resources.add(scimUsr);
    }
    log.info("Found {} matching entries - returning {}", list.getTotalEntriesCount(), list.getEntries().size());
    PagedResult<BaseScimResource> result = new PagedResult<>();
    result.setEntries(resources);
    result.setTotalEntriesCount(list.getTotalEntriesCount());
    return result;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Filter(io.jans.orm.search.filter.Filter) ScimCustomPerson(io.jans.scim.model.scim.ScimCustomPerson) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) ArrayList(java.util.ArrayList) UserResource(io.jans.scim.model.scim2.user.UserResource) PagedResult(io.jans.orm.model.PagedResult)

Aggregations

UserResource (io.jans.scim.model.scim2.user.UserResource)49 Response (javax.ws.rs.core.Response)47 Test (org.testng.annotations.Test)37 UserBaseTest (io.jans.scim2.client.UserBaseTest)34 ListResponse (io.jans.scim.model.scim2.ListResponse)21 SearchRequest (io.jans.scim.model.scim2.SearchRequest)14 Parameters (org.testng.annotations.Parameters)11 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)7 Path (javax.ws.rs.Path)7 BaseScimResource (io.jans.scim.model.scim2.BaseScimResource)6 CustomAttributes (io.jans.scim.model.scim2.CustomAttributes)6 SkipTest (io.jans.scim2.listener.SkipTest)6 URI (java.net.URI)6 DefaultValue (javax.ws.rs.DefaultValue)6 HeaderParam (javax.ws.rs.HeaderParam)6 Status (javax.ws.rs.core.Response.Status)6 BeforeTest (org.testng.annotations.BeforeTest)6 ScimCustomPerson (io.jans.scim.model.scim.ScimCustomPerson)5 GroupResource (io.jans.scim.model.scim2.group.GroupResource)5 PatchRequest (io.jans.scim.model.scim2.patch.PatchRequest)5