use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.
the class UserPolicyManagementTest method testGenericConfig.
@Test
public void testGenericConfig() {
AuthorizationResource authorization = getClient().authorization();
UserPolicyRepresentation representation = new UserPolicyRepresentation();
representation.setName("Test Generic Config Permission");
representation.addUser("User A");
UserPoliciesResource policies = authorization.policies().user();
try (Response response = policies.create(representation)) {
UserPolicyRepresentation created = response.readEntity(UserPolicyRepresentation.class);
PolicyResource policy = authorization.policies().policy(created.getId());
PolicyRepresentation genericConfig = policy.toRepresentation();
assertNotNull(genericConfig.getConfig());
assertNotNull(genericConfig.getConfig().get("users"));
UserRepresentation user = getRealm().users().search("User A").get(0);
assertTrue(genericConfig.getConfig().get("users").contains(user.getId()));
}
}
use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.
the class UserPolicyManagementTest method testDelete.
@Test
public void testDelete() {
AuthorizationResource authorization = getClient().authorization();
UserPolicyRepresentation representation = new UserPolicyRepresentation();
representation.setName("Test Delete Permission");
representation.addUser("User A");
UserPoliciesResource policies = authorization.policies().user();
try (Response response = policies.create(representation)) {
UserPolicyRepresentation created = response.readEntity(UserPolicyRepresentation.class);
policies.findById(created.getId()).remove();
UserPolicyResource removed = policies.findById(created.getId());
try {
removed.toRepresentation();
fail("Permission not removed");
} catch (NotFoundException ignore) {
}
}
}
use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.
the class UserPolicyManagementTest method testDeleteUser.
@Test
public void testDeleteUser() {
AuthorizationResource authorization = getClient().authorization();
UserPolicyRepresentation representation = new UserPolicyRepresentation();
representation.setName("Realm User Policy");
representation.setDescription("description");
representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
representation.setLogic(Logic.NEGATIVE);
representation.addUser("User D");
representation.addUser("User E");
representation.addUser("User F");
assertCreated(authorization, representation);
UsersResource users = getRealm().users();
UserRepresentation user = users.search("User D").get(0);
users.get(user.getId()).remove();
representation = authorization.policies().user().findById(representation.getId()).toRepresentation();
Assert.assertEquals(2, representation.getUsers().size());
Assert.assertFalse(representation.getUsers().contains(user.getId()));
user = users.search("User E").get(0);
users.get(user.getId()).remove();
representation = authorization.policies().user().findById(representation.getId()).toRepresentation();
Assert.assertEquals(1, representation.getUsers().size());
Assert.assertFalse(representation.getUsers().contains(user.getId()));
user = users.search("User F").get(0);
users.get(user.getId()).remove();
try {
authorization.policies().user().findById(representation.getId()).toRepresentation();
fail("User policy should be removed");
} catch (NotFoundException nfe) {
// ignore
}
}
use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.
the class UserPolicyManagementTest method assertCreated.
private void assertCreated(AuthorizationResource authorization, UserPolicyRepresentation representation) {
UserPoliciesResource permissions = authorization.policies().user();
try (Response response = permissions.create(representation)) {
UserPolicyRepresentation created = response.readEntity(UserPolicyRepresentation.class);
UserPolicyResource permission = permissions.findById(created.getId());
assertRepresentation(representation, permission);
}
}
use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.
the class UserPolicyManagementTest method testUpdate.
@Test
public void testUpdate() {
AuthorizationResource authorization = getClient().authorization();
UserPolicyRepresentation representation = new UserPolicyRepresentation();
representation.setName("Update Test User Policy");
representation.setDescription("description");
representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
representation.setLogic(Logic.NEGATIVE);
representation.addUser("User A");
representation.addUser("User B");
representation.addUser("User C");
assertCreated(authorization, representation);
representation.setName("changed");
representation.setDescription("changed");
representation.setDecisionStrategy(DecisionStrategy.AFFIRMATIVE);
representation.setLogic(Logic.POSITIVE);
representation.setUsers(representation.getUsers().stream().filter(userName -> !userName.equals("User A")).collect(Collectors.toSet()));
UserPoliciesResource policies = authorization.policies().user();
UserPolicyResource permission = policies.findById(representation.getId());
permission.update(representation);
assertRepresentation(representation, permission);
representation.setUsers(representation.getUsers().stream().filter(userName -> !userName.equals("User C")).collect(Collectors.toSet()));
permission.update(representation);
assertRepresentation(representation, permission);
}
Aggregations