Search in sources :

Example 31 with UserResource

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

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
@RefAdjusted
@ApiOperation(value = "PATCH operation", notes = "https://tools.ietf.org/html/rfc7644#section-3.5.2", response = UserResource.class)
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");
        UserResource user = new UserResource();
        // person is not null (check associated decorator method)
        GluuCustomPerson person = personService.getPersonByInum(id);
        // 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("pairwiseIdentitifers")) {
                // If this block weren't here, the implementation will throw error because read-only attribute cannot be altered
                // Note the path is intentionally mistyped, see class member in UserResource
                person.setOxPPID(null);
                user.setPairwiseIdentitifers(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");
        executeDefaultValidation(user);
        ScimResourceUtil.adjustPrimarySubAttributes(user);
        // Update timestamp
        String now = ISODateTimeFormat.dateTime().withZoneUTC().print(System.currentTimeMillis());
        user.getMeta().setLastModified(now);
        // 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 : ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) Response(javax.ws.rs.core.Response) ListViewResponse(org.gluu.persist.model.ListViewResponse) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) UserResource(org.gluu.oxtrust.model.scim2.user.UserResource) PatchOperation(org.gluu.oxtrust.model.scim2.patch.PatchOperation) URI(java.net.URI) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Path(javax.ws.rs.Path) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) RefAdjusted(org.gluu.oxtrust.service.scim2.interceptor.RefAdjusted) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ProtectedApi(org.gluu.oxtrust.service.filter.ProtectedApi)

Example 32 with UserResource

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

the class UserWebService method updateUser.

/**
 * This implementation differs from spec in the following aspects:
 * - Passing a null value for an attribute, does not modify the attribute in the destination, however passing an
 * empty array for a multivalued attribute does clear the attribute. Thus, to clear single-valued attribute, PATCH
 * operation should be used
 */
@Path("{id}")
@PUT
@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
@RefAdjusted
@ApiOperation(value = "Update user", notes = "Update user (https://tools.ietf.org/html/rfc7644#section-3.5.1)", response = UserResource.class)
public Response updateUser(@ApiParam(value = "User", required = true) UserResource user, @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. updateUser");
        UserResource updatedResource = scim2UserService.updateUser(id, user, endpointUrl);
        String json = resourceSerializer.serialize(updatedResource, attrsList, excludedAttrsList);
        response = Response.ok(new URI(updatedResource.getMeta().getLocation())).entity(json).build();
    } catch (InvalidAttributeValueException e) {
        log.error(e.getMessage());
        response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.MUTABILITY, e.getMessage());
    } catch (Exception e) {
        log.error("Failure at updateUser method", e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return response;
}
Also used : ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) Response(javax.ws.rs.core.Response) ListViewResponse(org.gluu.persist.model.ListViewResponse) UserResource(org.gluu.oxtrust.model.scim2.user.UserResource) URI(java.net.URI) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Path(javax.ws.rs.Path) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) RefAdjusted(org.gluu.oxtrust.service.scim2.interceptor.RefAdjusted) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ProtectedApi(org.gluu.oxtrust.service.filter.ProtectedApi) PUT(javax.ws.rs.PUT)

Example 33 with UserResource

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

the class SpecialCharsTest method equality.

@Test(dependsOnMethods = "containabilityAny")
public void equality() {
    // Based on usernames list built in test containabilityAny, n searches with EQ filter are issued
    for (String userName : userNames) {
        SearchRequest sr = new SearchRequest();
        sr.setFilter(String.format("userName eq \"%s\"", StringEscapeUtils.escapeJson(userName)));
        sr.setAttributes("userName, name.givenName");
        Response response = client.searchUsersPost(sr);
        UserResource user = (UserResource) response.readEntity(ListResponse.class).getResources().get(0);
        logger.info("User {} found with userName {}", user.getName().getGivenName(), user.getUserName());
        assertEquals(userName, user.getUserName());
    }
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) SearchRequest(io.jans.scim.model.scim2.SearchRequest) ListResponse(io.jans.scim.model.scim2.ListResponse) UserResource(io.jans.scim.model.scim2.user.UserResource) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test) SkipTest(io.jans.scim2.listener.SkipTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 34 with UserResource

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

the class GroupAssignUserTest method createUsers.

@Test
public void createUsers() {
    logger.info("Creating 3 users...");
    List<UserResource> mentals = new ArrayList<>();
    // Hugo, Paco, and Luis; or Curly, and the other 2 crazy men
    Arrays.asList(1, 2, 3).forEach(who -> mentals.add(getDummyPatient()));
    for (UserResource user : mentals) {
        Response response = client.createUser(user, null, null);
        assertEquals(response.getStatus(), CREATED.getStatusCode());
        friends.add(response.readEntity(usrClass));
    }
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) UserResource(io.jans.scim.model.scim2.user.UserResource) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Example 35 with UserResource

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

the class GroupAssignUserTest method alterMemberships.

@Test(dependsOnMethods = "modifyGroupFromUser")
public void alterMemberships() {
    // Effectively remove one member and add admin
    Member aMental = group.getMembers().stream().findAny().get();
    Member admin = new Member();
    admin.setValue(getAdminId());
    group.getMembers().remove(aMental);
    group.getMembers().add(admin);
    logger.info("Removing one and adding one member...");
    Response response = client.updateGroup(group, group.getId(), null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    group = response.readEntity(GroupResource.class);
    assertFalse(group.getMembers().contains(aMental));
    // Here we don't use contains because equality in Member object inspects all fields (not only value)
    assertTrue(group.getMembers().stream().anyMatch(m -> admin.getValue().equals(m.getValue())));
    logger.info("Group has correct members");
    // Verify groups attribute in users reflected changes
    response = client.getUserById(aMental.getValue(), "groups", null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    UserResource patient = response.readEntity(usrClass);
    assertTrue(patient.getGroups() == null || patient.getGroups().stream().noneMatch(gr -> gr.getValue().equals(group.getId())));
    response = client.getUserById(admin.getValue(), "groups", null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    patient = response.readEntity(usrClass);
    assertTrue(patient.getGroups().stream().anyMatch(gr -> gr.getValue().equals(group.getId())));
    logger.info("Users have correct memberships");
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) Member(io.jans.scim.model.scim2.group.Member) UserBaseTest(io.jans.scim2.client.UserBaseTest) java.util(java.util) UserResource(io.jans.scim.model.scim2.user.UserResource) Test(org.testng.annotations.Test) Collectors(java.util.stream.Collectors) GroupResource(io.jans.scim.model.scim2.group.GroupResource) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) SearchRequest(io.jans.scim.model.scim2.SearchRequest) Group(io.jans.scim.model.scim2.user.Group) Response(javax.ws.rs.core.Response) Assert(org.testng.Assert) ListResponse(io.jans.scim.model.scim2.ListResponse) Status(javax.ws.rs.core.Response.Status) UserResource(io.jans.scim.model.scim2.user.UserResource) Member(io.jans.scim.model.scim2.group.Member) GroupResource(io.jans.scim.model.scim2.group.GroupResource) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

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