Search in sources :

Example 1 with Role

use of com.enonic.xp.security.Role in project xp by enonic.

the class UpdateProjectRoleNamesCommand method doUpdateRoles.

private void doUpdateRoles() {
    for (ProjectRole projectRole : ProjectRole.values()) {
        final PrincipalKey roleKey = ProjectAccessHelper.createRoleKey(projectName, projectRole);
        final String newRoleDisplayName = createRoleDisplayName(projectRole);
        securityService.getRole(roleKey).filter(role -> !newRoleDisplayName.equals(role.getDisplayName())).ifPresent((roleValue) -> doUpdateRoleDisplayName(roleValue, newRoleDisplayName));
    }
}
Also used : UpdateRoleParams(com.enonic.xp.security.UpdateRoleParams) ProjectRole(com.enonic.xp.project.ProjectRole) PrincipalKey(com.enonic.xp.security.PrincipalKey) Preconditions(com.google.common.base.Preconditions) Role(com.enonic.xp.security.Role) ProjectRole(com.enonic.xp.project.ProjectRole) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Example 2 with Role

use of com.enonic.xp.security.Role in project xp by enonic.

the class SecurityServiceImplTest method testUpdateRole.

@Test
public void testUpdateRole() throws Exception {
    runAsAdmin(() -> {
        final CreateRoleParams createRole = CreateRoleParams.create().roleKey(PrincipalKey.ofRole("Role-a")).displayName("Role A").build();
        final Role role = securityService.createRole(createRole);
        final UpdateRoleParams roleUpdate = UpdateRoleParams.create(role).displayName("___Role B___").description("description").build();
        final Role updatedRoleResult = securityService.updateRole(roleUpdate);
        refresh();
        final Role updatedRole = securityService.getRole(role.getKey()).get();
        assertEquals("___Role B___", updatedRoleResult.getDisplayName());
        assertEquals("___Role B___", updatedRole.getDisplayName());
        assertEquals("description", updatedRoleResult.getDescription());
        assertEquals("description", updatedRole.getDescription());
    });
}
Also used : CreateRoleParams(com.enonic.xp.security.CreateRoleParams) Role(com.enonic.xp.security.Role) UpdateRoleParams(com.enonic.xp.security.UpdateRoleParams) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with Role

use of com.enonic.xp.security.Role in project xp by enonic.

the class SecurityServiceImpl method createRole.

@Override
public Role createRole(final CreateRoleParams createRole) {
    final Role role = Role.create().key(createRole.getKey()).displayName(createRole.getDisplayName()).modifiedTime(Instant.now(clock)).description(createRole.getDescription()).build();
    final CreateNodeParams createNodeParams = PrincipalNodeTranslator.toCreateNodeParams(role);
    try {
        final Node node = callWithContext(() -> {
            final Node createdNode = this.nodeService.create(createNodeParams);
            this.nodeService.refresh(RefreshMode.SEARCH);
            return createdNode;
        });
        return PrincipalNodeTranslator.roleFromNode(node);
    } catch (NodeIdExistsException | NodeAlreadyExistAtPathException e) {
        throw new PrincipalAlreadyExistsException(createRole.getKey());
    }
}
Also used : Role(com.enonic.xp.security.Role) PrincipalAlreadyExistsException(com.enonic.xp.security.PrincipalAlreadyExistsException) NodeIdExistsException(com.enonic.xp.node.NodeIdExistsException) Node(com.enonic.xp.node.Node) NodeAlreadyExistAtPathException(com.enonic.xp.node.NodeAlreadyExistAtPathException) CreateNodeParams(com.enonic.xp.node.CreateNodeParams)

Example 4 with Role

use of com.enonic.xp.security.Role in project xp by enonic.

the class SecurityServiceImplTest method testCreateRole.

@Test
public void testCreateRole() throws Exception {
    runAsAdmin(() -> {
        final PrincipalKey roleKey1 = PrincipalKey.ofRole("Role-a");
        final CreateRoleParams createRole = CreateRoleParams.create().roleKey(roleKey1).displayName("Role A").description("Group A Description").build();
        final PrincipalKey roleKey2 = PrincipalKey.ofRole("role-b");
        final CreateRoleParams createRole2 = CreateRoleParams.create().roleKey(roleKey2).displayName("Role B").build();
        final Role role1 = securityService.createRole(createRole);
        final Role role2 = securityService.createRole(createRole2);
        final Role createdRole1 = securityService.getRole(roleKey1).get();
        final Role createdRole2 = securityService.getRole(roleKey2).get();
        assertEquals("Role A", role1.getDisplayName());
        assertEquals("Role A", createdRole1.getDisplayName());
        assertEquals("Group A Description", role1.getDescription());
        assertEquals("Group A Description", createdRole1.getDescription());
        assertEquals("Role B", role2.getDisplayName());
        assertEquals("Role B", createdRole2.getDisplayName());
        assertNull(role2.getDescription());
        assertNull(createdRole2.getDescription());
    });
}
Also used : CreateRoleParams(com.enonic.xp.security.CreateRoleParams) Role(com.enonic.xp.security.Role) PrincipalKey(com.enonic.xp.security.PrincipalKey) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with Role

use of com.enonic.xp.security.Role in project xp by enonic.

the class SecurityServiceImpl method updateRole.

@Override
public Role updateRole(final UpdateRoleParams updateRoleParams) {
    return callWithContext(() -> {
        final Node node = this.nodeService.getByPath(updateRoleParams.getKey().toPath());
        if (node == null) {
            throw new PrincipalNotFoundException(updateRoleParams.getKey());
        }
        final Role existingRole = PrincipalNodeTranslator.roleFromNode(node);
        final Role roleToUpdate = updateRoleParams.update(existingRole);
        final UpdateNodeParams updateNodeParams = PrincipalNodeTranslator.toUpdateNodeParams(roleToUpdate);
        final Node updatedNode = nodeService.update(updateNodeParams);
        this.nodeService.refresh(RefreshMode.SEARCH);
        return PrincipalNodeTranslator.roleFromNode(updatedNode);
    });
}
Also used : Role(com.enonic.xp.security.Role) Node(com.enonic.xp.node.Node) PrincipalNotFoundException(com.enonic.xp.security.PrincipalNotFoundException) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams)

Aggregations

Role (com.enonic.xp.security.Role)8 Test (org.junit.jupiter.api.Test)4 Node (com.enonic.xp.node.Node)2 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)2 CreateRoleParams (com.enonic.xp.security.CreateRoleParams)2 Group (com.enonic.xp.security.Group)2 PrincipalKey (com.enonic.xp.security.PrincipalKey)2 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)2 UpdateRoleParams (com.enonic.xp.security.UpdateRoleParams)2 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)1 NodeAlreadyExistAtPathException (com.enonic.xp.node.NodeAlreadyExistAtPathException)1 NodeIdExistsException (com.enonic.xp.node.NodeIdExistsException)1 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)1 ProjectRole (com.enonic.xp.project.ProjectRole)1 EditableRole (com.enonic.xp.security.EditableRole)1 PrincipalAlreadyExistsException (com.enonic.xp.security.PrincipalAlreadyExistsException)1 PrincipalNotFoundException (com.enonic.xp.security.PrincipalNotFoundException)1 RoleEditor (com.enonic.xp.security.RoleEditor)1 Preconditions (com.google.common.base.Preconditions)1