Search in sources :

Example 6 with UserResource

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

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(scopes = { "https://jans.io/scim/users.write" })
@RefAdjusted
public Response updateUser(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");
        // Check if the ids match in case the user coming has one
        if (user.getId() != null && !user.getId().equals(id))
            throw new SCIMException("Parameter id does not match with id attribute of User");
        ScimCustomPerson person = userPersistenceHelper.getPersonByInum(id);
        if (person == null)
            return notFoundResponse(id, userResourceType);
        response = externalConstraintsService.applyEntityCheck(person, user, httpHeaders, uriInfo, HttpMethod.PUT, userResourceType);
        if (response != null)
            return response;
        executeValidation(user, true);
        if (StringUtils.isNotEmpty(user.getUserName())) {
            checkUidExistence(user.getUserName(), id);
        }
        ScimResourceUtil.adjustPrimarySubAttributes(user);
        UserResource updatedResource = scim2UserService.updateUser(person, user, endpointUrl);
        String json = resourceSerializer.serialize(updatedResource, attrsList, excludedAttrsList);
        response = Response.ok(new URI(updatedResource.getMeta().getLocation())).entity(json).build();
    } catch (DuplicateEntryException e) {
        log.error(e.getMessage());
        response = getErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, e.getMessage());
    } catch (SCIMException e) {
        log.error("Validation check at updateUser returned: {}", e.getMessage());
        response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_VALUE, e.getMessage());
    } 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 : 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) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) 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) PUT(javax.ws.rs.PUT)

Example 7 with UserResource

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

the class UserWebService method getUserById.

@Path("{id}")
@GET
@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.read" })
@RefAdjusted
public Response getUserById(@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. getUserById");
        ScimCustomPerson person = userPersistenceHelper.getPersonByInum(id);
        if (person == null)
            return notFoundResponse(id, userResourceType);
        response = externalConstraintsService.applyEntityCheck(person, null, httpHeaders, uriInfo, HttpMethod.GET, userResourceType);
        if (response != null)
            return response;
        UserResource user = scim2UserService.buildUserResource(person, endpointUrl);
        String json = resourceSerializer.serialize(user, attrsList, excludedAttrsList);
        response = Response.ok(new URI(user.getMeta().getLocation())).entity(json).build();
    } catch (Exception e) {
        log.error("Failure at getUserById method", e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) ScimCustomPerson(io.jans.scim.model.scim.ScimCustomPerson) UserResource(io.jans.scim.model.scim2.user.UserResource) URI(java.net.URI) 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) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ProtectedApi(io.jans.scim.service.filter.ProtectedApi)

Example 8 with UserResource

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

the class SpecialCharsTest method containabilityAll.

@SkipTest(databases = { "spanner", "couchbase" })
@Test
public void containabilityAll() {
    // Builds a long "and" based clause
    String filter = specialFilterLdapChars.stream().reduce("", (partial, next) -> partial + String.format(" and userName co \"%s\"", next));
    SearchRequest sr = new SearchRequest();
    // Drop beginning (namely " and ")
    sr.setFilter(filter.substring(4));
    sr.setAttributes("userName");
    // Search users whose usernames contain ALL the chars
    Response response = client.searchUsersPost(sr);
    assertEquals(response.getStatus(), OK.getStatusCode());
    List<UserResource> resources = response.readEntity(ListResponse.class).getResources().stream().map(UserResource.class::cast).collect(Collectors.toList());
    String userName = resources.get(0).getUserName();
    assertEquals(resources.size(), 1);
    assertTrue(Stream.of(SPECIAL_CHARS).allMatch(userName::contains));
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) SearchRequest(io.jans.scim.model.scim2.SearchRequest) 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) SkipTest(io.jans.scim2.listener.SkipTest)

Example 9 with UserResource

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

the class GroupAssignUserTest method assignToGroup.

@Test(dependsOnMethods = "createUsers")
public void assignToGroup() {
    Set<Member> buddies = friends.stream().map(buddy -> {
        Member m = new Member();
        m.setValue(buddy.getId());
        m.setDisplay(buddy.getDisplayName());
        return m;
    }).collect(Collectors.toSet());
    GroupResource gr = new GroupResource();
    gr.setDisplayName("3 best demented buddies");
    gr.setMembers(buddies);
    logger.info("Assigning users to new group...");
    Response response = client.createGroup(gr, null, null);
    assertEquals(response.getStatus(), CREATED.getStatusCode());
    group = response.readEntity(GroupResource.class);
    // Verify the sanitarium is completely booked
    assertTrue(group.getMembers().stream().map(Member::getValue).collect(Collectors.toSet()).equals(friends.stream().map(UserResource::getId).collect(Collectors.toSet())));
}
Also used : 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) Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) 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)

Example 10 with UserResource

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

the class GroupAssignUserTest method verifyGroupsAttribute.

@Test(dependsOnMethods = "assignToSecondGroup")
public void verifyGroupsAttribute() {
    // Refresh the user instances so getGroups() can be called
    // builds a filter string
    StringBuilder filter = new StringBuilder();
    friends.forEach(buddy -> filter.append(String.format(" or id eq \"%s\"", buddy.getId())));
    // builds a search request
    SearchRequest sr = new SearchRequest();
    sr.setFilter(filter.substring(4));
    // Retrieve only the first 3
    sr.setCount(3);
    // Performs the query
    logger.info("Issuing query with filter: {}", sr.getFilter());
    Response response = client.searchUsersPost(sr);
    assertEquals(response.getStatus(), OK.getStatusCode());
    logger.info("Verifying groups and users consistency...");
    List<BaseScimResource> buddies = response.readEntity(ListResponse.class).getResources();
    assertEquals(buddies.size(), 3);
    // Verify all mad belong to group, and one of them, additionally to group2
    buddies.stream().map(usrClass::cast).forEach(buddy -> {
        Set<String> groupIds = buddy.getGroups().stream().map(Group::getValue).collect(Collectors.toSet());
        assertTrue(groupIds.contains(group.getId()));
    });
    Optional<UserResource> usrOpt = buddies.stream().map(usrClass::cast).filter(buddy -> buddy.getGroups().size() > 1).findFirst();
    assertTrue(usrOpt.isPresent());
    user = usrOpt.get();
    assertTrue(user.getGroups().stream().map(Group::getValue).collect(Collectors.toSet()).contains(group2.getId()));
}
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) SearchRequest(io.jans.scim.model.scim2.SearchRequest) ListResponse(io.jans.scim.model.scim2.ListResponse) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) UserResource(io.jans.scim.model.scim2.user.UserResource) 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