Java Code Examples for org.keycloak.representations.idm.RoleRepresentation#setId()

The following examples show how to use org.keycloak.representations.idm.RoleRepresentation#setId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private List<RoleRepresentation> convertClientRoleNamesToRepresentation(String clientId, List<String> roles) {
    LinkedList<RoleRepresentation> result = new LinkedList<>();
    Map<String, String> roleIdMap = clientRoleIdMap.get(clientId);
    if (roleIdMap == null || roleIdMap.isEmpty()) {
        throw new RuntimeException("No client roles created for clientId: " + clientId);
    }

    for (String role: roles) {
        RoleRepresentation r = new RoleRepresentation();
        String id = roleIdMap.get(role);
        if (id == null) {
            throw new RuntimeException("No client role created on client '" + clientId + "' for name: " + role);
        }
        r.setId(id);
        r.setName(role);
        result.add(r);
    }
    return result;
}
 
Example 2
Source File: RealmRolesTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void getRolesWithFullRepresentation() {
    for(int i = 0; i<5; i++) {
        String roleName = "attributesrole"+i;
        RoleRepresentation role = makeRole(roleName);
        
        Map<String, List<String>> attributes = new HashMap<String, List<String>>();
        attributes.put("attribute1", Arrays.asList("value1","value2"));
        role.setAttributes(attributes);
                
        resource.create(role);
        assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.roleResourcePath(roleName), role, ResourceType.REALM_ROLE);   
        
        // we have to update the role to set the attributes because
        // the add role endpoint only care about name and description
        RoleResource roleToUpdate = resource.get(roleName);
        role.setId(roleToUpdate.toRepresentation().getId());
        
        roleToUpdate.update(role);
        assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, AdminEventPaths.roleResourcePath(roleName), role, ResourceType.REALM_ROLE);  
    }
    
    List<RoleRepresentation> roles = resource.list("attributesrole", false);
    assertTrue(roles.get(0).getAttributes().containsKey("attribute1"));
}
 
Example 3
Source File: RealmRolesTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void getRolesWithBriefRepresentation() {
    for(int i = 0; i<5; i++) {
        String roleName = "attributesrolebrief"+i;
        RoleRepresentation role = makeRole(roleName);
        
        Map<String, List<String>> attributes = new HashMap<String, List<String>>();
        attributes.put("attribute1", Arrays.asList("value1","value2"));
        role.setAttributes(attributes);
                
        resource.create(role);
        assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.roleResourcePath(roleName), role, ResourceType.REALM_ROLE);
        
        // we have to update the role to set the attributes because
        // the add role endpoint only care about name and description
        RoleResource roleToUpdate = resource.get(roleName);
        role.setId(roleToUpdate.toRepresentation().getId());
        
        roleToUpdate.update(role);
        assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, AdminEventPaths.roleResourcePath(roleName), role, ResourceType.REALM_ROLE);  
    }
    
    List<RoleRepresentation> roles = resource.list("attributesrolebrief", true);
    assertNull(roles.get(0).getAttributes());
}
 
Example 4
Source File: ClientRolesTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void getRolesWithFullRepresentation() {
    for(int i = 0; i<5; i++) {
        String roleName = "attributesrole"+i;
        RoleRepresentation role = makeRole(roleName);
        
        Map<String, List<String>> attributes = new HashMap<String, List<String>>();
        attributes.put("attribute1", Arrays.asList("value1","value2"));
        role.setAttributes(attributes);
                
        rolesRsc.create(role);
        assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.clientRoleResourcePath(clientDbId,roleName), role, ResourceType.CLIENT_ROLE);  
        
        // we have to update the role to set the attributes because
        // the add role endpoint only care about name and description
        RoleResource roleToUpdate = rolesRsc.get(roleName);
        role.setId(roleToUpdate.toRepresentation().getId());
        
        roleToUpdate.update(role);
        assertAdminEvents.assertEvent(getRealmId(), OperationType.UPDATE, AdminEventPaths.clientRoleResourcePath(clientDbId,roleName), role, ResourceType.CLIENT_ROLE);  
    }
    
    List<RoleRepresentation> roles = rolesRsc.list(false);
    assertTrue(roles.get(0).getAttributes().containsKey("attribute1"));
}
 
Example 5
Source File: ClientRolesTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void getRolesWithBriefRepresentation() {
    for(int i = 0; i<5; i++) {
        String roleName = "attributesrole"+i;
        RoleRepresentation role = makeRole(roleName);
        
        Map<String, List<String>> attributes = new HashMap<String, List<String>>();
        attributes.put("attribute1", Arrays.asList("value1","value2"));
        role.setAttributes(attributes);
                
        rolesRsc.create(role);
        assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.clientRoleResourcePath(clientDbId,roleName), role, ResourceType.CLIENT_ROLE);  
        
        // we have to update the role to set the attributes because
        // the add role endpoint only care about name and description
        RoleResource roleToUpdate = rolesRsc.get(roleName);
        role.setId(roleToUpdate.toRepresentation().getId());
        
        roleToUpdate.update(role);
        assertAdminEvents.assertEvent(getRealmId(), OperationType.UPDATE, AdminEventPaths.clientRoleResourcePath(clientDbId,roleName), role, ResourceType.CLIENT_ROLE);         
    }
    
    List<RoleRepresentation> roles = rolesRsc.list();
    assertNull(roles.get(0).getAttributes());
}
 
Example 6
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private List<RoleRepresentation> convertRealmRoleNamesToRepresentation(List<String> roles) {
    LinkedList<RoleRepresentation> result = new LinkedList<>();
    for (String role: roles) {
        RoleRepresentation r = new RoleRepresentation();
        String id = realmRoleIdMap.get(role);
        if (id == null) {
            throw new RuntimeException("No realm role created for name: " + role);
        }
        r.setId(id);
        r.setName(role);
        result.add(r);
    }
    return result;
}
 
Example 7
Source File: RoleContainerResource.java    From keycloak with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new role for the realm or client
 *
 * @param rep
 * @return
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createRole(final RoleRepresentation rep) {
    auth.roles().requireManage(roleContainer);

    if (rep.getName() == null) {
        throw new BadRequestException();
    }

    try {
        RoleModel role = roleContainer.addRole(rep.getName());
        role.setDescription(rep.getDescription());

        rep.setId(role.getId());

        if (role.isClientRole()) {
            adminEvent.resource(ResourceType.CLIENT_ROLE);
        } else {
            adminEvent.resource(ResourceType.REALM_ROLE);
        }

        adminEvent.operation(OperationType.CREATE).resourcePath(uriInfo, role.getName()).representation(rep).success();

        return Response.created(uriInfo.getAbsolutePathBuilder().path(role.getName()).build()).build();
    } catch (ModelDuplicateException e) {
        return ErrorResponse.exists("Role with name " + rep.getName() + " already exists");
    }
}
 
Example 8
Source File: RolesPartialImport.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private void setUniqueIds(Map<String, List<RoleRepresentation>> clientRoles) {
    for (String clientId : clientRoles.keySet()) {
        for (RoleRepresentation clientRole : clientRoles.get(clientId)) {
            clientRole.setId(KeycloakModelUtils.generateId());
        }
    }
}
 
Example 9
Source File: RolesPartialImport.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void setUniqueIds(List<RoleRepresentation> realmRoles) {
    for (RoleRepresentation realmRole : realmRoles) {
        realmRole.setId(KeycloakModelUtils.generateId());
    }
}