Java Code Examples for org.sonatype.nexus.security.role.Role#setName()

The following examples show how to use org.sonatype.nexus.security.role.Role#setName() . 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: KeycloakMapper.java    From nexus3-keycloak-plugin with Apache License 2.0 6 votes vote down vote up
public static Role toRole(String source, String sourceCode, RoleRepresentation representation) {
    if (representation == null) {
        return null;
    }

    Role role = new Role();
    String prefix = representation.getClientRole() ? CLIENT_ROLE_PREFIX : REALM_ROLE_PREFIX;
    String roleName = String.format("%s:%s%s",
                                    prefix,
                                    sourceCode != null ? sourceCode + ":" : "",
                                    representation.getName());

    // Use role name as role-id and role-name of Nexus3
    role.setRoleId(roleName);
    role.setName(roleName);
    if (representation.getDescription() != null && !representation.getDescription().isEmpty()) {
        role.setDescription(String.format("%s: %s", prefix, representation.getDescription()));
    }
    role.setReadOnly(true);
    role.setSource(source);

    return role;
}
 
Example 2
Source File: KeycloakMapper.java    From nexus3-keycloak-plugin with Apache License 2.0 6 votes vote down vote up
public static Role toRole(String source, String sourceCode, GroupRepresentation representation) {
    if (representation == null) {
        return null;
    }

    Role role = new Role();
    String roleName = String.format("%s:%s%s",
                                    REALM_GROUP_PREFIX,
                                    sourceCode != null ? sourceCode + ":" : "",
                                    representation.getPath());

    role.setRoleId(roleName);
    role.setName(roleName);
    role.setReadOnly(true);
    role.setSource(source);

    return role;
}
 
Example 3
Source File: RoleApiResourceTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testCreateRole() throws Exception {
  RoleXORequest roleXo = createApiRole("roleId", "roleName", "description", Collections.singleton("childRole"),
      Collections.singleton("priv"));

  Role createdRole = new Role();
  createdRole.setRoleId("roleId");
  createdRole.setSource("default");
  createdRole.setName("roleName");
  createdRole.setDescription("description");
  createdRole.setReadOnly(false);
  createdRole.setRoles(Collections.singleton("childRole"));
  createdRole.setPrivileges(Collections.singleton("priv"));

  when(authorizationManager.addRole(any())).thenReturn(createdRole);

  RoleXOResponse result = underTest.create(roleXo);

  assertApiRole(result, "default", "roleId", "roleName", "description", Collections.singleton("childRole"),
      Collections.singleton("priv"));
}
 
Example 4
Source File: RoleApiResourceTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Role createRole(final String source,
                        final String id,
                        final String name,
                        final String description,
                        final Collection<String> roles,
                        final Collection<String> privileges)
{
  Role role = new Role();
  role.setRoleId(id);
  role.setName(name);
  role.setDescription(description);
  role.setSource(source);
  roles.forEach(role::addRole);
  privileges.forEach(role::addPrivilege);

  return role;
}
 
Example 5
Source File: MockAuthorizationManagerB.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Set<Role> listRoles() {
  Set<Role> roles = new HashSet<Role>();

  Role role1 = new Role();
  role1.setSource(this.getSource());
  role1.setName("Role 1");
  role1.setRoleId("test-role1");
  role1.addPrivilege("from-role1:read");
  role1.addPrivilege("from-role1:delete");

  Role role2 = new Role();
  role2.setSource(this.getSource());
  role2.setName("Role 2");
  role2.setRoleId("test-role2");
  role2.addPrivilege("from-role2:read");
  role2.addPrivilege("from-role2:delete");

  roles.add(role1);
  roles.add(role2);

  return roles;
}
 
Example 6
Source File: AuthorizationManagerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testAddRole() throws Exception {
  AuthorizationManager authzManager = this.getAuthorizationManager();

  Role role = new Role();
  role.setRoleId("new-role");
  role.setName("new-name");
  role.setDescription("new-description");
  role.addPrivilege("2");
  role.addPrivilege("4");

  authzManager.addRole(role);

  CRole secRole = this.getConfigurationManager().readRole(role.getRoleId());

  Assert.assertEquals(role.getRoleId(), secRole.getId());
  Assert.assertEquals(role.getName(), secRole.getName());
  Assert.assertEquals(role.getDescription(), secRole.getDescription());
  Assert.assertTrue(secRole.getPrivileges().contains("2"));
  Assert.assertTrue(secRole.getPrivileges().contains("4"));
  Assert.assertEquals(2, secRole.getPrivileges().size());
}
 
Example 7
Source File: AuthorizationManagerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testUpdateRole() throws Exception {
  AuthorizationManager authzManager = this.getAuthorizationManager();

  Role role2 = authzManager.getRole("role2");
  role2.setDescription("new description");
  role2.setName("new name");

  Set<String> permissions = new HashSet<String>();
  permissions.add("2");
  role2.setPrivileges(permissions);

  authzManager.updateRole(role2);

  CRole secRole = this.getConfigurationManager().readRole(role2.getRoleId());

  Assert.assertEquals(role2.getRoleId(), secRole.getId());
  Assert.assertEquals(role2.getName(), secRole.getName());
  Assert.assertEquals(role2.getDescription(), secRole.getDescription());
  Assert.assertTrue(secRole.getPrivileges().contains("2"));
  Assert.assertEquals(1, secRole.getPrivileges().size());
}
 
Example 8
Source File: KeycloakMapper.java    From nexus3-keycloak-plugin with Apache License 2.0 5 votes vote down vote up
private static Role toCompatibleRole(String source, RoleRepresentation representation) {
    Role role = new Role();

    role.setRoleId(representation.getName());
    role.setName(representation.getName());
    role.setDescription(representation.getDescription());
    role.setReadOnly(true);
    role.setSource(source);
    return role;
}
 
Example 9
Source File: GenericRepositoryITSupport.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
protected static Role createRole(final String name, final String... privileges) {
  Role role = new Role();
  role.setRoleId(name);
  role.setSource(DEFAULT_SOURCE);
  role.setName(name);
  role.setDescription(name);
  role.setReadOnly(false);
  role.setPrivileges(Sets.newHashSet(privileges));
  return role;
}
 
Example 10
Source File: AuthorizationManagerImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Role convert(final CRole source) {
  Role target = new Role();
  target.setRoleId(source.getId());
  target.setVersion(source.getVersion());
  target.setName(source.getName());
  target.setSource(SOURCE);
  target.setDescription(source.getDescription());
  target.setReadOnly(source.isReadOnly());
  target.setPrivileges(Sets.newHashSet(source.getPrivileges()));
  target.setRoles(Sets.newHashSet(source.getRoles()));
  return target;
}