Search in sources :

Example 1 with UserService

use of org.apache.syncope.common.rest.api.service.UserService in project testcases by coheigea.

the class SyncopeDeployer method deployUserData.

@SuppressWarnings("unchecked")
public void deployUserData() {
    WebClient client = WebClient.create(address);
    client = client.type("application/xml");
    String authorizationHeader = "Basic " + Base64Utility.encode(("admin" + ":" + "password").getBytes());
    client.header("Authorization", authorizationHeader);
    client.accept("application/xml");
    // Create the groups first
    client = client.path("groups");
    PagedResult<GroupTO> existingGroups = (PagedResult<GroupTO>) client.get(PagedResult.class);
    GroupTO bossGroup = findOrCreateGroup("boss", existingGroups, client);
    GroupTO employeeGroup = findOrCreateGroup("employee", existingGroups, client);
    // Now create the users
    client = client.replacePath("users");
    PagedResult<UserTO> existingUsers = (PagedResult<UserTO>) client.get(PagedResult.class);
    if (!doesUserAlreadyExist("alice", existingUsers.getResult())) {
        UserTO user = new UserTO();
        user.setUsername("alice");
        user.setPassword("security");
        user.setRealm("/");
        MembershipTO membership = new MembershipTO();
        membership.setGroupKey(employeeGroup.getKey());
        // membership.setGroupName(employeeGroup.getName());
        user.getMemberships().add(membership);
        membership = new MembershipTO();
        // membership.setGroupName(bossGroup.getName());
        membership.setGroupKey(bossGroup.getKey());
        user.getMemberships().add(membership);
        client.post(user, ProvisioningResult.class);
    }
    if (!doesUserAlreadyExist("bob", existingUsers.getResult())) {
        UserTO user = new UserTO();
        user.setUsername("bob");
        user.setPassword("security");
        user.setRealm("/");
        MembershipTO membership = new MembershipTO();
        membership.setGroupKey(employeeGroup.getKey());
        // membership.setGroupName(employeeGroup.getName());
        user.getMemberships().add(membership);
        client.post(user, ProvisioningResult.class);
    }
    client.close();
    // Check via the client API that the users were created correctly
    SyncopeClientFactoryBean clientFactory = new SyncopeClientFactoryBean().setAddress(address);
    SyncopeClient syncopeClient = clientFactory.create("admin", "password");
    UserService userService = syncopeClient.getService(UserService.class);
    int count = userService.search(new AnyQuery.Builder().build()).getTotalCount();
    Assert.assertEquals(2, count);
}
Also used : UserService(org.apache.syncope.common.rest.api.service.UserService) WebClient(org.apache.cxf.jaxrs.client.WebClient) SyncopeClient(org.apache.syncope.client.lib.SyncopeClient) GroupTO(org.apache.syncope.common.lib.to.GroupTO) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) SyncopeClientFactoryBean(org.apache.syncope.client.lib.SyncopeClientFactoryBean) AnyQuery(org.apache.syncope.common.rest.api.beans.AnyQuery) PagedResult(org.apache.syncope.common.lib.to.PagedResult)

Example 2 with UserService

use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.

the class UserSelfITCase method read.

@Test
public void read() {
    UserService userService2 = clientFactory.create("rossini", ADMIN_PWD).getService(UserService.class);
    try {
        userService2.read("1417acbe-cbf6-4277-9372-e75e04f97000");
        fail("This should not happen");
    } catch (ForbiddenException e) {
        assertNotNull(e);
    }
    Pair<Map<String, Set<String>>, UserTO> self = clientFactory.create("rossini", ADMIN_PWD).self();
    assertEquals("rossini", self.getRight().getUsername());
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) UserService(org.apache.syncope.common.rest.api.service.UserService) UserTO(org.apache.syncope.common.lib.to.UserTO) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with UserService

use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.

the class UserRestClient method suspend.

public BulkActionResult suspend(final String etag, final String userKey, final List<StatusBean> statuses) {
    StatusPatch statusPatch = StatusUtils.buildStatusPatch(statuses, false);
    statusPatch.setKey(userKey);
    statusPatch.setType(StatusPatchType.SUSPEND);
    BulkActionResult result;
    synchronized (this) {
        result = new BulkActionResult();
        Map<String, BulkActionResult.Status> res = result.getResults();
        UserService service = getService(etag, UserService.class);
        @SuppressWarnings("unchecked") ProvisioningResult<UserTO> provisions = (ProvisioningResult<UserTO>) service.status(statusPatch).readEntity(ProvisioningResult.class);
        if (statusPatch.isOnSyncope()) {
            res.put(StringUtils.capitalize(Constants.SYNCOPE), "suspended".equalsIgnoreCase(provisions.getEntity().getStatus()) ? BulkActionResult.Status.SUCCESS : BulkActionResult.Status.FAILURE);
        }
        for (PropagationStatus status : provisions.getPropagationStatuses()) {
            res.put(status.getResource(), BulkActionResult.Status.valueOf(status.getStatus().name()));
        }
        resetClient(UserService.class);
    }
    return result;
}
Also used : PropagationStatus(org.apache.syncope.common.lib.to.PropagationStatus) UserService(org.apache.syncope.common.rest.api.service.UserService) UserTO(org.apache.syncope.common.lib.to.UserTO) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) StatusPatch(org.apache.syncope.common.lib.patch.StatusPatch) PropagationStatus(org.apache.syncope.common.lib.to.PropagationStatus) BulkActionResult(org.apache.syncope.common.lib.to.BulkActionResult)

Example 4 with UserService

use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.

the class RESTITCase method ifMatch.

@Test
public void ifMatch() {
    UserTO userTO = userService.create(UserITCase.getUniqueSampleTO("ifmatch@syncope.apache.org"), true).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
    }).getEntity();
    assertNotNull(userTO);
    assertNotNull(userTO.getKey());
    EntityTag etag = adminClient.getLatestEntityTag(userService);
    assertNotNull(etag);
    assertTrue(StringUtils.isNotBlank(etag.getValue()));
    UserPatch userPatch = new UserPatch();
    userPatch.setKey(userTO.getKey());
    userPatch.setUsername(new StringReplacePatchItem.Builder().value(userTO.getUsername() + "XX").build());
    userTO = userService.update(userPatch).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
    }).getEntity();
    assertTrue(userTO.getUsername().endsWith("XX"));
    EntityTag etag1 = adminClient.getLatestEntityTag(userService);
    assertFalse(etag.getValue().equals(etag1.getValue()));
    UserService ifMatchService = adminClient.ifMatch(adminClient.getService(UserService.class), etag);
    userPatch.setUsername(new StringReplacePatchItem.Builder().value(userTO.getUsername() + "YY").build());
    try {
        ifMatchService.update(userPatch);
        fail("This should not happen");
    } catch (SyncopeClientException e) {
        assertEquals(ClientExceptionType.ConcurrentModification, e.getType());
    }
    userTO = userService.read(userTO.getKey());
    assertTrue(userTO.getUsername().endsWith("XX"));
}
Also used : GenericType(javax.ws.rs.core.GenericType) StringReplacePatchItem(org.apache.syncope.common.lib.patch.StringReplacePatchItem) UserService(org.apache.syncope.common.rest.api.service.UserService) UserTO(org.apache.syncope.common.lib.to.UserTO) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) EntityTag(javax.ws.rs.core.EntityTag) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) Test(org.junit.jupiter.api.Test)

Example 5 with UserService

use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.

the class AuthenticationITCase method userSearch.

@Test
public void userSearch() {
    UserTO userTO = UserITCase.getUniqueSampleTO("testusersearch@test.org");
    userTO.getRoles().add("User reviewer");
    userTO = createUser(userTO).getEntity();
    assertNotNull(userTO);
    // 1. user assigned to role 1, with search entitlement on realms /odd and /even: won't find anything with
    // root realm
    UserService userService2 = clientFactory.create(userTO.getUsername(), "password123").getService(UserService.class);
    PagedResult<UserTO> matchingUsers = userService2.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).fiql(SyncopeClient.getUserSearchConditionBuilder().isNotNull("key").query()).build());
    assertNotNull(matchingUsers);
    assertFalse(matchingUsers.getResult().isEmpty());
    Set<String> matchingUserKeys = matchingUsers.getResult().stream().map(user -> user.getKey()).collect(Collectors.toSet());
    assertTrue(matchingUserKeys.contains("1417acbe-cbf6-4277-9372-e75e04f97000"));
    assertFalse(matchingUserKeys.contains("74cd8ece-715a-44a4-a736-e17b46c4e7e6"));
    assertFalse(matchingUserKeys.contains("823074dc-d280-436d-a7dd-07399fae48ec"));
    // 2. user assigned to role 4, with search entitlement on realm /even/two
    UserService userService3 = clientFactory.create("puccini", ADMIN_PWD).getService(UserService.class);
    matchingUsers = userService3.search(new AnyQuery.Builder().realm("/even/two").fiql(SyncopeClient.getUserSearchConditionBuilder().isNotNull("loginDate").query()).build());
    assertNotNull(matchingUsers);
    assertTrue(matchingUsers.getResult().stream().allMatch(matching -> "/even/two".equals(matching.getRealm())));
}
Also used : StringPatchItem(org.apache.syncope.common.lib.patch.StringPatchItem) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) Autowired(org.springframework.beans.factory.annotation.Autowired) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) ResourceDeassociationAction(org.apache.syncope.common.lib.types.ResourceDeassociationAction) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) SpringJUnitConfig(org.springframework.test.context.junit.jupiter.SpringJUnitConfig) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Pair(org.apache.commons.lang3.tuple.Pair) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) Map(java.util.Map) RESTHeaders(org.apache.syncope.common.rest.api.RESTHeaders) PagedResult(org.apache.syncope.common.lib.to.PagedResult) FlowableDetector(org.apache.syncope.fit.FlowableDetector) BasicAuthenticationHandler(org.apache.syncope.client.lib.BasicAuthenticationHandler) BulkActionResult(org.apache.syncope.common.lib.to.BulkActionResult) SchemaService(org.apache.syncope.common.rest.api.service.SchemaService) Set(java.util.Set) WorkflowFormTO(org.apache.syncope.common.lib.to.WorkflowFormTO) SchemaType(org.apache.syncope.common.lib.types.SchemaType) Collectors(java.util.stream.Collectors) StatusPatch(org.apache.syncope.common.lib.patch.StatusPatch) GenericType(javax.ws.rs.core.GenericType) Test(org.junit.jupiter.api.Test) Response(javax.ws.rs.core.Response) DeassociationPatch(org.apache.syncope.common.lib.patch.DeassociationPatch) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AccessControlException(java.security.AccessControlException) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) StandardEntitlement(org.apache.syncope.common.lib.types.StandardEntitlement) AnonymousAuthenticationHandler(org.apache.syncope.client.lib.AnonymousAuthenticationHandler) UserService(org.apache.syncope.common.rest.api.service.UserService) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) RoleTO(org.apache.syncope.common.lib.to.RoleTO) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) DataSource(javax.sql.DataSource) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AbstractITCase(org.apache.syncope.fit.AbstractITCase) AnyQuery(org.apache.syncope.common.rest.api.beans.AnyQuery) AnyObjectService(org.apache.syncope.common.rest.api.service.AnyObjectService) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) CipherAlgorithm(org.apache.syncope.common.lib.types.CipherAlgorithm) AnyTypeClassTO(org.apache.syncope.common.lib.to.AnyTypeClassTO) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) Encryptor(org.apache.syncope.core.spring.security.Encryptor) ForbiddenException(javax.ws.rs.ForbiddenException) StatusPatchType(org.apache.syncope.common.lib.types.StatusPatchType) AnyTypeTO(org.apache.syncope.common.lib.to.AnyTypeTO) PasswordPatch(org.apache.syncope.common.lib.patch.PasswordPatch) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) StringReplacePatchItem(org.apache.syncope.common.lib.patch.StringReplacePatchItem) SyncopeClient(org.apache.syncope.client.lib.SyncopeClient) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) UserService(org.apache.syncope.common.rest.api.service.UserService) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyQuery(org.apache.syncope.common.rest.api.beans.AnyQuery) Test(org.junit.jupiter.api.Test)

Aggregations

UserTO (org.apache.syncope.common.lib.to.UserTO)11 UserService (org.apache.syncope.common.rest.api.service.UserService)11 Test (org.junit.jupiter.api.Test)8 ProvisioningResult (org.apache.syncope.common.lib.to.ProvisioningResult)7 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)5 UserPatch (org.apache.syncope.common.lib.patch.UserPatch)5 GenericType (javax.ws.rs.core.GenericType)4 SyncopeClient (org.apache.syncope.client.lib.SyncopeClient)4 Response (javax.ws.rs.core.Response)3 StatusPatch (org.apache.syncope.common.lib.patch.StatusPatch)3 StringReplacePatchItem (org.apache.syncope.common.lib.patch.StringReplacePatchItem)3 BulkActionResult (org.apache.syncope.common.lib.to.BulkActionResult)3 PagedResult (org.apache.syncope.common.lib.to.PagedResult)3 RoleTO (org.apache.syncope.common.lib.to.RoleTO)3 AnyQuery (org.apache.syncope.common.rest.api.beans.AnyQuery)3 AccessControlException (java.security.AccessControlException)2 Map (java.util.Map)2 ForbiddenException (javax.ws.rs.ForbiddenException)2 PasswordPatch (org.apache.syncope.common.lib.patch.PasswordPatch)2 StringPatchItem (org.apache.syncope.common.lib.patch.StringPatchItem)2