use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.
the class AuthenticationITCase method checkFailedLogins.
@Test
public void checkFailedLogins() {
UserTO userTO = UserITCase.getUniqueSampleTO("checkFailedLogin@syncope.apache.org");
userTO.getRoles().add("User manager");
userTO = createUser(userTO).getEntity();
assertNotNull(userTO);
String userKey = userTO.getKey();
UserService userService2 = clientFactory.create(userTO.getUsername(), "password123").getService(UserService.class);
assertEquals(0, getFailedLogins(userService2, userKey));
// authentications failed ...
try {
clientFactory.create(userTO.getUsername(), "wrongpwd1");
fail("This should not happen");
} catch (AccessControlException e) {
assertNotNull(e);
}
try {
clientFactory.create(userTO.getUsername(), "wrongpwd1");
fail("This should not happen");
} catch (AccessControlException e) {
assertNotNull(e);
}
assertEquals(2, getFailedLogins(userService, userKey));
UserService userService4 = clientFactory.create(userTO.getUsername(), "password123").getService(UserService.class);
assertEquals(0, getFailedLogins(userService4, userKey));
}
use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.
the class AuthenticationITCase method delegatedUserCRUD.
@Test
public void delegatedUserCRUD() {
String roleKey = null;
String delegatedAdminKey = null;
try {
// 1. create role for full user administration, under realm /even/two
RoleTO role = new RoleTO();
role.setKey("Delegated user admin");
role.getEntitlements().add(StandardEntitlement.USER_CREATE);
role.getEntitlements().add(StandardEntitlement.USER_UPDATE);
role.getEntitlements().add(StandardEntitlement.USER_DELETE);
role.getEntitlements().add(StandardEntitlement.USER_SEARCH);
role.getEntitlements().add(StandardEntitlement.USER_READ);
role.getRealms().add("/even/two");
roleKey = roleService.create(role).getHeaderString(RESTHeaders.RESOURCE_KEY);
assertNotNull(roleKey);
// 2. as admin, create delegated admin user, and assign the role just created
UserTO delegatedAdmin = UserITCase.getUniqueSampleTO("admin@syncope.apache.org");
delegatedAdmin.getRoles().add(roleKey);
delegatedAdmin = createUser(delegatedAdmin).getEntity();
delegatedAdminKey = delegatedAdmin.getKey();
// 3. instantiate a delegate user service client, for further operatins
UserService delegatedUserService = clientFactory.create(delegatedAdmin.getUsername(), "password123").getService(UserService.class);
// 4. as delegated, create user under realm / -> fail
UserTO user = UserITCase.getUniqueSampleTO("delegated@syncope.apache.org");
try {
delegatedUserService.create(user, true);
fail("This should not happen");
} catch (SyncopeClientException e) {
assertEquals(ClientExceptionType.DelegatedAdministration, e.getType());
}
// 5. set realm to /even/two -> succeed
user.setRealm("/even/two");
Response response = delegatedUserService.create(user, true);
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
user = response.readEntity(new GenericType<ProvisioningResult<UserTO>>() {
}).getEntity();
assertEquals("surname", user.getPlainAttr("surname").get().getValues().get(0));
// 5. as delegated, update user attempting to move under realm / -> fail
UserPatch userPatch = new UserPatch();
userPatch.setKey(user.getKey());
userPatch.setRealm(new StringReplacePatchItem.Builder().value("/odd").build());
userPatch.getPlainAttrs().add(attrAddReplacePatch("surname", "surname2"));
try {
delegatedUserService.update(userPatch);
fail("This should not happen");
} catch (SyncopeClientException e) {
assertEquals(ClientExceptionType.DelegatedAdministration, e.getType());
}
// 6. revert realm change -> succeed
userPatch.setRealm(null);
response = delegatedUserService.update(userPatch);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
user = response.readEntity(new GenericType<ProvisioningResult<UserTO>>() {
}).getEntity();
assertEquals("surname2", user.getPlainAttr("surname").get().getValues().get(0));
// 7. as delegated, delete user
delegatedUserService.delete(user.getKey());
try {
userService.read(user.getKey());
fail("This should not happen");
} catch (SyncopeClientException e) {
assertEquals(ClientExceptionType.NotFound, e.getType());
}
} finally {
if (roleKey != null) {
roleService.delete(roleKey);
}
if (delegatedAdminKey != null) {
userService.delete(delegatedAdminKey);
}
}
}
use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.
the class AuthenticationITCase method userRead.
@Test
public void userRead() {
UserTO userTO = UserITCase.getUniqueSampleTO("testuserread@test.org");
userTO.getRoles().add("User manager");
userTO = createUser(userTO).getEntity();
assertNotNull(userTO);
UserService userService2 = clientFactory.create(userTO.getUsername(), "password123").getService(UserService.class);
UserTO readUserTO = userService2.read("1417acbe-cbf6-4277-9372-e75e04f97000");
assertNotNull(readUserTO);
UserService userService3 = clientFactory.create("puccini", ADMIN_PWD).getService(UserService.class);
try {
userService3.read("b3cbc78d-32e6-4bd4-92e0-bbe07566a2ee");
fail("This should not happen");
} catch (SyncopeClientException e) {
assertNotNull(e);
assertEquals(ClientExceptionType.DelegatedAdministration, e.getType());
}
}
use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.
the class DynRealmITCase method delegatedAdmin.
@Test
public void delegatedAdmin() {
DynRealmTO dynRealm = null;
RoleTO role = null;
try {
// 1. create dynamic realm for all users and groups having resource-ldap assigned
dynRealm = new DynRealmTO();
dynRealm.setKey("LDAPLovers" + getUUIDString());
dynRealm.getDynMembershipConds().put(AnyTypeKind.USER.name(), "$resources==resource-ldap");
dynRealm.getDynMembershipConds().put(AnyTypeKind.GROUP.name(), "$resources==resource-ldap");
Response response = dynRealmService.create(dynRealm);
dynRealm = getObject(response.getLocation(), DynRealmService.class, DynRealmTO.class);
assertNotNull(dynRealm);
// 2. create role for such dynamic realm
role = new RoleTO();
role.setKey("Administer LDAP" + getUUIDString());
role.getEntitlements().add(StandardEntitlement.USER_SEARCH);
role.getEntitlements().add(StandardEntitlement.USER_READ);
role.getEntitlements().add(StandardEntitlement.USER_UPDATE);
role.getEntitlements().add(StandardEntitlement.GROUP_READ);
role.getEntitlements().add(StandardEntitlement.GROUP_UPDATE);
role.getDynRealms().add(dynRealm.getKey());
role = createRole(role);
assertNotNull(role);
// 3. create new user and assign the new role
UserTO dynRealmAdmin = UserITCase.getUniqueSampleTO("dynRealmAdmin@apache.org");
dynRealmAdmin.setPassword("password123");
dynRealmAdmin.getRoles().add(role.getKey());
dynRealmAdmin = createUser(dynRealmAdmin).getEntity();
assertNotNull(dynRealmAdmin);
// 4. create new user and group, assign resource-ldap
UserTO user = UserITCase.getUniqueSampleTO("dynRealmUser@apache.org");
user.setRealm("/even/two");
user.getResources().clear();
user.getResources().add(RESOURCE_NAME_LDAP);
user = createUser(user).getEntity();
assertNotNull(user);
final String userKey = user.getKey();
GroupTO group = GroupITCase.getSampleTO("dynRealmGroup");
group.setRealm("/odd");
group.getResources().clear();
group.getResources().add(RESOURCE_NAME_LDAP);
group = createGroup(group).getEntity();
assertNotNull(group);
final String groupKey = group.getKey();
if (ElasticsearchDetector.isElasticSearchEnabled(syncopeService)) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
// ignore
}
}
// 5. verify that the new user and group are found when searching by dynamic realm
PagedResult<UserTO> matchingUsers = userService.search(new AnyQuery.Builder().realm("/").fiql(SyncopeClient.getUserSearchConditionBuilder().inDynRealms(dynRealm.getKey()).query()).build());
assertTrue(matchingUsers.getResult().stream().anyMatch(object -> object.getKey().equals(userKey)));
PagedResult<GroupTO> matchingGroups = groupService.search(new AnyQuery.Builder().realm("/").fiql(SyncopeClient.getGroupSearchConditionBuilder().inDynRealms(dynRealm.getKey()).query()).build());
assertTrue(matchingGroups.getResult().stream().anyMatch(object -> object.getKey().equals(groupKey)));
// 6. prepare to act as delegated admin
SyncopeClient delegatedClient = clientFactory.create(dynRealmAdmin.getUsername(), "password123");
UserService delegatedUserService = delegatedClient.getService(UserService.class);
GroupService delegatedGroupService = delegatedClient.getService(GroupService.class);
// 7. verify delegated administration
// USER_READ
assertNotNull(delegatedUserService.read(userKey));
// GROUP_READ
assertNotNull(delegatedGroupService.read(groupKey));
// USER_SEARCH
matchingUsers = delegatedUserService.search(new AnyQuery.Builder().realm("/").build());
assertTrue(matchingUsers.getResult().stream().anyMatch(object -> object.getKey().equals(userKey)));
// USER_UPDATE
UserPatch userPatch = new UserPatch();
userPatch.setKey(userKey);
userPatch.getResources().add(new StringPatchItem.Builder().value(RESOURCE_NAME_LDAP).operation(PatchOperation.DELETE).build());
// this will fail because unassigning resource-ldap would result in removing the user from the dynamic realm
try {
delegatedUserService.update(userPatch);
fail("This should not happen");
} catch (SyncopeClientException e) {
assertEquals(ClientExceptionType.DelegatedAdministration, e.getType());
}
// this will succeed instead
userPatch.getResources().clear();
userPatch.getResources().add(new StringPatchItem.Builder().value(RESOURCE_NAME_NOPROPAGATION).build());
user = delegatedUserService.update(userPatch).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
}).getEntity();
assertNotNull(user);
assertTrue(user.getResources().contains(RESOURCE_NAME_NOPROPAGATION));
// GROUP_UPDATE
GroupPatch groupPatch = new GroupPatch();
groupPatch.setKey(groupKey);
groupPatch.getPlainAttrs().add(new AttrPatch.Builder().attrTO(attrTO("icon", "modified")).build());
group = delegatedGroupService.update(groupPatch).readEntity(new GenericType<ProvisioningResult<GroupTO>>() {
}).getEntity();
assertNotNull(group);
assertEquals("modified", group.getPlainAttr("icon").get().getValues().get(0));
} finally {
if (role != null) {
roleService.delete(role.getKey());
}
if (dynRealm != null) {
dynRealmService.delete(dynRealm.getKey());
}
}
}
use of org.apache.syncope.common.rest.api.service.UserService in project syncope by apache.
the class UserITCase method async.
@Test
public void async() {
SyncopeClient asyncClient = clientFactory.create(ADMIN_UNAME, ADMIN_PWD);
UserService asyncService = asyncClient.nullPriorityAsync(asyncClient.getService(UserService.class), true);
UserTO user = getUniqueSampleTO("async@syncope.apache.org");
user.getResources().add(RESOURCE_NAME_TESTDB);
user.getResources().add(RESOURCE_NAME_TESTDB2);
user.getResources().add(RESOURCE_NAME_LDAP);
ProvisioningResult<UserTO> result = asyncService.create(user, true).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
});
assertNotNull(result);
verifyAsyncResult(result.getPropagationStatuses());
UserPatch userPatch = new UserPatch();
userPatch.setKey(result.getEntity().getKey());
userPatch.setPassword(new PasswordPatch.Builder().onSyncope(true).resources(RESOURCE_NAME_LDAP, RESOURCE_NAME_TESTDB, RESOURCE_NAME_TESTDB2).value("password321").build());
result = asyncService.update(userPatch).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
});
assertNotNull(result);
verifyAsyncResult(result.getPropagationStatuses());
result = asyncService.delete(result.getEntity().getKey()).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
});
assertNotNull(result);
verifyAsyncResult(result.getPropagationStatuses());
}
Aggregations