Search in sources :

Example 56 with UserResource

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

the class QueryParamCreateUpdateTest method update1.

@Test(dependsOnMethods = "create2")
public void update1() throws Exception {
    // Change some attributes existing in user object
    UserResource cheapClone = getDeepCloneUsr(user);
    cheapClone.getName().setGivenName("Bavara");
    cheapClone.setNickName("Cloned");
    String rndString = Double.toString(Math.random());
    // For help on usage of io.jans.scim.model.scim2.CustomAttributes class, read its api docs (oxtrust-scim maven project)
    CustomAttributes custAttrs = cheapClone.getCustomAttributes(USER_EXT_SCHEMA_ID);
    custAttrs.setAttribute("scimCustomFirst", rndString);
    String include = "userName, name.givenName, nickName, urn:ietf:params:scim:schemas:extension:gluu:2.0:User:scimCustomFirst";
    Response response = client.updateUser(cheapClone, cheapClone.getId(), include, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    user = response.readEntity(usrClass);
    assertNull(user.getDisplayName());
    assertEquals(user.getName().getGivenName(), cheapClone.getName().getGivenName());
    assertEquals(user.getNickName(), cheapClone.getNickName());
    custAttrs = user.getCustomAttributes(USER_EXT_SCHEMA_ID);
    assertNull(custAttrs.getValues("scimCustomSecond", Date.class));
    assertNull(custAttrs.getValue("scimCustomThird", Integer.class));
    assertEquals(custAttrs.getValue("scimCustomFirst", String.class), rndString);
}
Also used : Response(javax.ws.rs.core.Response) CustomAttributes(io.jans.scim.model.scim2.CustomAttributes) UserResource(io.jans.scim.model.scim2.user.UserResource) Date(java.util.Date) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Example 57 with UserResource

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

the class UserWebService method patchUser.

@Path("{id}")
@PATCH
@Consumes({ MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@Produces({ MEDIA_TYPE_SCIM_JSON + UTF8_CHARSET_FRAGMENT, MediaType.APPLICATION_JSON + UTF8_CHARSET_FRAGMENT })
@HeaderParam("Accept")
@DefaultValue(MEDIA_TYPE_SCIM_JSON)
@ProtectedApi(scopes = { "https://jans.io/scim/users.write" })
@RefAdjusted
public Response patchUser(PatchRequest request, @PathParam("id") String id, @QueryParam(QUERY_PARAM_ATTRIBUTES) String attrsList, @QueryParam(QUERY_PARAM_EXCLUDED_ATTRS) String excludedAttrsList) {
    Response response;
    try {
        log.debug("Executing web service method. patchUser");
        response = inspectPatchRequest(request, UserResource.class);
        if (response != null)
            return response;
        ScimCustomPerson person = userPersistenceHelper.getPersonByInum(id);
        if (person == null)
            return notFoundResponse(id, userResourceType);
        response = externalConstraintsService.applyEntityCheck(person, request, httpHeaders, uriInfo, HttpMethod.PATCH, userResourceType);
        if (response != null)
            return response;
        UserResource user = new UserResource();
        // Fill user instance with all info from person
        scim2UserService.transferAttributesToUserResource(person, user, endpointUrl);
        // Apply patches one by one in sequence
        for (PatchOperation po : request.getOperations()) {
            // Handle special case: https://github.com/GluuFederation/oxTrust/issues/800
            if (po.getType().equals(REMOVE) && po.getPath().equals("pairwiseIdentifiers")) {
                // If this block weren't here, the implementation will throw error because read-only attribute cannot be altered
                person.setPpid(null);
                user.setPairwiseIdentifiers(null);
                scim2UserService.removePPIDsBranch(person.getDn());
            } else {
                user = (UserResource) scim2PatchService.applyPatchOperation(user, po);
            }
        }
        // Throws exception if final representation does not pass overall validation
        log.debug("patchUser. Revising final resource representation still passes validations");
        executeValidation(user);
        ScimResourceUtil.adjustPrimarySubAttributes(user);
        // Update timestamp
        user.getMeta().setLastModified(DateUtil.millisToISOString(System.currentTimeMillis()));
        // Replaces the information found in person with the contents of user
        scim2UserService.replacePersonInfo(person, user, endpointUrl);
        String json = resourceSerializer.serialize(user, attrsList, excludedAttrsList);
        response = Response.ok(new URI(user.getMeta().getLocation())).entity(json).build();
    } catch (InvalidAttributeValueException e) {
        log.error(e.getMessage(), e);
        response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.MUTABILITY, e.getMessage());
    } catch (SCIMException e) {
        response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_SYNTAX, e.getMessage());
    } catch (Exception e) {
        log.error("Failure at patchUser method", e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) SCIMException(io.jans.scim.model.exception.SCIMException) ScimCustomPerson(io.jans.scim.model.scim.ScimCustomPerson) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) URI(java.net.URI) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) URISyntaxException(java.net.URISyntaxException) SCIMException(io.jans.scim.model.exception.SCIMException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Path(javax.ws.rs.Path) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) RefAdjusted(io.jans.scim.service.scim2.interceptor.RefAdjusted) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ProtectedApi(io.jans.scim.service.filter.ProtectedApi) PATCH(io.jans.scim.ws.rs.scim2.PATCH)

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