Search in sources :

Example 6 with EntityTO

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

the class ApplicationITCase method crud.

@Test
public void crud() {
    // 1. create application
    ApplicationTO application = new ApplicationTO();
    application.setKey(UUID.randomUUID().toString());
    PrivilegeTO privilegeTO = new PrivilegeTO();
    privilegeTO.setKey(UUID.randomUUID().toString());
    privilegeTO.setSpec("{ \"one\": true }");
    application.getPrivileges().add(privilegeTO);
    privilegeTO = new PrivilegeTO();
    privilegeTO.setKey(UUID.randomUUID().toString());
    privilegeTO.setSpec("{ \"two\": true }");
    application.getPrivileges().add(privilegeTO);
    privilegeTO = new PrivilegeTO();
    privilegeTO.setKey(UUID.randomUUID().toString());
    privilegeTO.setSpec("{ \"three\": true }");
    application.getPrivileges().add(privilegeTO);
    Response response = applicationService.create(application);
    assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
    application = getObject(response.getLocation(), ApplicationService.class, ApplicationTO.class);
    assertNotNull(application);
    assertNull(application.getDescription());
    assertEquals(3, application.getPrivileges().size());
    // 2. update application
    application.setDescription("A description");
    application.getPrivileges().remove(1);
    applicationService.update(application);
    application = applicationService.read(application.getKey());
    assertNotNull(application);
    assertNotNull(application.getDescription());
    assertEquals(2, application.getPrivileges().size());
    // 3. assign application's privileges to a new role
    RoleTO role = new RoleTO();
    role.setKey("privileged");
    role.getPrivileges().addAll(application.getPrivileges().stream().map(EntityTO::getKey).collect(Collectors.toList()));
    response = roleService.create(role);
    assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
    role = getObject(response.getLocation(), RoleService.class, RoleTO.class);
    assertNotNull(role);
    assertEquals(2, role.getPrivileges().size());
    // 4. delete application => delete privileges
    applicationService.delete(application.getKey());
    try {
        applicationService.read(application.getKey());
        fail("This should not happen");
    } catch (SyncopeClientException e) {
        assertEquals(ClientExceptionType.NotFound, e.getType());
    }
    role = roleService.read(role.getKey());
    assertNotNull(role);
    assertTrue(role.getPrivileges().isEmpty());
}
Also used : Response(javax.ws.rs.core.Response) EntityTO(org.apache.syncope.common.lib.to.EntityTO) RoleService(org.apache.syncope.common.rest.api.service.RoleService) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) ApplicationTO(org.apache.syncope.common.lib.to.ApplicationTO) PrivilegeTO(org.apache.syncope.common.lib.to.PrivilegeTO) RoleTO(org.apache.syncope.common.lib.to.RoleTO) ApplicationService(org.apache.syncope.common.rest.api.service.ApplicationService) Test(org.junit.jupiter.api.Test)

Example 7 with EntityTO

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

the class GroupServiceImpl method replace.

@Override
public Response replace(final String id, final SCIMGroup group) {
    if (!id.equals(group.getId())) {
        throw new BadRequestException(ErrorType.invalidPath, "Expected " + id + ", found " + group.getId());
    }
    ResponseBuilder builder = checkETag(Resource.Group, id);
    if (builder != null) {
        return builder.build();
    }
    // save current group members
    Set<String> beforeMembers = new HashSet<>();
    MembershipCond membCond = new MembershipCond();
    membCond.setGroup(id);
    SearchCond searchCond = SearchCond.getLeafCond(membCond);
    int count = userLogic().search(searchCond, 1, 1, Collections.<OrderByClause>emptyList(), SyncopeConstants.ROOT_REALM, false).getLeft();
    for (int page = 1; page <= (count / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
        beforeMembers.addAll(userLogic().search(searchCond, page, AnyDAO.DEFAULT_PAGE_SIZE, Collections.<OrderByClause>emptyList(), SyncopeConstants.ROOT_REALM, false).getRight().stream().map(EntityTO::getKey).collect(Collectors.toSet()));
    }
    // update group, don't change members
    ProvisioningResult<GroupTO> result = groupLogic().update(AnyOperations.diff(binder().toGroupTO(group), groupLogic().read(id), false), false);
    // assign new members
    Set<String> afterMembers = new HashSet<>();
    group.getMembers().forEach(member -> {
        afterMembers.add(member.getValue());
        if (!beforeMembers.contains(member.getValue())) {
            UserPatch patch = new UserPatch();
            patch.setKey(member.getValue());
            patch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(result.getEntity().getKey()).build());
            try {
                userLogic().update(patch, false);
            } catch (Exception e) {
                LOG.error("While setting membership of {} to {}", result.getEntity().getKey(), member.getValue(), e);
            }
        }
    });
    // remove unconfirmed members
    beforeMembers.stream().filter(member -> !afterMembers.contains(member)).forEach(user -> {
        UserPatch patch = new UserPatch();
        patch.setKey(user);
        patch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.DELETE).group(result.getEntity().getKey()).build());
        try {
            userLogic().update(patch, false);
        } catch (Exception e) {
            LOG.error("While removing membership of {} from {}", result.getEntity().getKey(), user, e);
        }
    });
    return updateResponse(result.getEntity().getKey(), binder().toSCIMGroup(result.getEntity(), uriInfo.getAbsolutePathBuilder().path(result.getEntity().getKey()).build().toASCIIString(), Collections.<String>emptyList(), Collections.<String>emptyList()));
}
Also used : Arrays(java.util.Arrays) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) ErrorType(org.apache.syncope.ext.scimv2.api.type.ErrorType) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) ArrayUtils(org.apache.commons.lang3.ArrayUtils) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) MembershipPatch(org.apache.syncope.common.lib.patch.MembershipPatch) EntityTO(org.apache.syncope.common.lib.to.EntityTO) SortOrder(org.apache.syncope.ext.scimv2.api.type.SortOrder) ListResponse(org.apache.syncope.ext.scimv2.api.data.ListResponse) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) SCIMGroup(org.apache.syncope.ext.scimv2.api.data.SCIMGroup) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Set(java.util.Set) GroupTO(org.apache.syncope.common.lib.to.GroupTO) Collectors(java.util.stream.Collectors) Resource(org.apache.syncope.ext.scimv2.api.type.Resource) Response(javax.ws.rs.core.Response) AnyDAO(org.apache.syncope.core.persistence.api.dao.AnyDAO) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) SCIMSearchRequest(org.apache.syncope.ext.scimv2.api.data.SCIMSearchRequest) Member(org.apache.syncope.ext.scimv2.api.data.Member) GroupService(org.apache.syncope.ext.scimv2.api.service.GroupService) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) Collections(java.util.Collections) AnyOperations(org.apache.syncope.common.lib.AnyOperations) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) GroupTO(org.apache.syncope.common.lib.to.GroupTO) EntityTO(org.apache.syncope.common.lib.to.EntityTO) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet)

Aggregations

EntityTO (org.apache.syncope.common.lib.to.EntityTO)7 ArrayList (java.util.ArrayList)3 Response (javax.ws.rs.core.Response)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2 AnyTypeTO (org.apache.syncope.common.lib.to.AnyTypeTO)2 AjaxBootstrapTabbedPanel (de.agilecoders.wicket.core.markup.html.bootstrap.tabs.AjaxBootstrapTabbedPanel)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ITabComponent (org.apache.syncope.client.console.commons.ITabComponent)1 GroupFormLayoutInfo (org.apache.syncope.client.console.layout.GroupFormLayoutInfo)1 UserFormLayoutInfo (org.apache.syncope.client.console.layout.UserFormLayoutInfo)1 ActionsPanel (org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel)1