public boolean add(CaseInsensitiveString roleName) { return add(new RoleConfig(roleName)); }
public static RoleConfig fromJSON(JsonReader jsonReader) { RoleConfig model = new RoleConfig(); if (jsonReader == null) { return model; } jsonReader.readArrayIfPresent("users", users -> { users.forEach(user -> model.addUser(new RoleUser(user.getAsString()))); }); return model; }
private static List<String> usersAsString(RoleConfig roleConfig) { return roleConfig.getUsers().stream().map(user -> user.getName().toString()).collect(Collectors.toList()); }
private void validateNullRoleName(Validator v) { RoleConfig role = new RoleConfig(); v.validate(role, ValidationContextMother.validationContext(new SecurityConfig())); assertThat(role.errors().size(), is(1)); assertThat(role.errors().get("name").get(0), is("Invalid role name name 'null'. This must be alphanumeric and can" + " contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); }
public RoleConfig(CaseInsensitiveString name, Users users) { this.name = name; for (RoleUser user : users) { addUser(user); } }
@Override public void update(CruiseConfig preprocessedConfig) throws Exception { RolesConfig rolesInConfig = preprocessedConfig.server().security().getRoles(); for (GoCDRolesBulkUpdateRequest.Operation operation : goCDRolesBulkUpdateRequest.getOperations()) { RoleConfig existingRole = rolesInConfig.findByNameAndType(new CaseInsensitiveString(operation.getRoleName()), RoleConfig.class); if (existingRole == null) { result.unprocessableEntity(resourceNotFound("Role", operation.getRoleName())); throw new NoSuchRoleException(operation.getRoleName()); } existingRole.addUsersWithName(operation.getUsersToAdd()); existingRole.removeUsersWithName(operation.getUsersToRemove()); } }
private void validatePresenceOfRoleName(Validator v) { RoleConfig role = new RoleConfig(new CaseInsensitiveString("")); v.validate(role, ValidationContextMother.validationContext(new SecurityConfig())); assertThat(role.errors().size(), is(1)); assertThat(role.errors().get("name").get(0), is("Invalid role name name ''. This must be alphanumeric and can" + " contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); }
public void setUsers(List<RoleUser> users) { this.users.clear(); for (RoleUser user : users) { addUser(user); } }
private Role role(String name, RoleUser... users) { return new RoleConfig(new CaseInsensitiveString(name), users); } }
public static RoleConfig fromJSON(JsonReader jsonReader) { RoleConfig model = new RoleConfig(); if (jsonReader == null) { return model; } jsonReader.readArrayIfPresent("users", users -> { users.forEach(user -> model.addUser(new RoleUser(user.getAsString()))); }); return model; }
public void validateUniquenessOfRoleName(Validator v) throws Exception { RoleConfig role = new RoleConfig(new CaseInsensitiveString("admin")); SecurityConfig securityConfig = new SecurityConfig(); ValidationContext validationContext = ValidationContextMother.validationContext(securityConfig); securityConfig.getRoles().add(new RoleConfig(new CaseInsensitiveString("admin"))); securityConfig.getRoles().add(role); v.validate(role, validationContext); assertThat(role.errors().size(), is(1)); assertThat(role.errors().get("name").get(0), is("Role names should be unique. Role with the same name exists.")); }
public void addUsersWithName(List<String> usersToAdd) { usersToAdd.forEach(user -> addUser(new RoleUser(user))); }
private static List<String> usersAsString(RoleConfig roleConfig) { return roleConfig.getUsers().stream().map(user -> user.getName().toString()).collect(Collectors.toList()); }
@Test public void isUniqueRoleName_shouldBeFalseWithMultipleRolesWithSameName() throws Exception { RolesConfig rolesConfig = new RolesConfig(new RoleConfig(new CaseInsensitiveString("admin")), new RoleConfig(new CaseInsensitiveString("view")), new RoleConfig(new CaseInsensitiveString("view"))); assertFalse(rolesConfig.isUniqueRoleName(new CaseInsensitiveString("view"))); } }
public static SecurityConfig securityConfigWithRole(SecurityConfig securityConfig, String roleName, String... users) { RoleConfig role = new RoleConfig(new CaseInsensitiveString(roleName)); for (String user : users) { role.addUser(new RoleUser(new CaseInsensitiveString(user))); } securityConfig.addRole(role); return securityConfig; } }
@Test public void shouldCareAboutRoleConfigChange() { SecurityConfigChangeListener securityConfigChangeListener = new SecurityConfigChangeListener() { @Override public void onEntityConfigChange(Object entity) { } }; assertThat(securityConfigChangeListener.shouldCareAbout(new RoleConfig()), is(true)); }
public Role createRole(String roleName, String... users) { return new RoleConfig(new CaseInsensitiveString(roleName), toRoleUsers(users)); }
@Test public void isUniqueRoleName_shouldBeTrueIfRolesAreUnique() throws Exception { RolesConfig rolesConfig = new RolesConfig(new RoleConfig(new CaseInsensitiveString("admin")), new RoleConfig(new CaseInsensitiveString("view"))); assertTrue(rolesConfig.isUniqueRoleName(new CaseInsensitiveString("admin"))); assertTrue(rolesConfig.isUniqueRoleName(new CaseInsensitiveString("operate"))); }
@Test public void shouldBombIfDeletingARoleWhichDoesNotExist() throws Exception { try { SecurityConfig securityConfig = security(passwordFileAuthConfig(), admins()); securityConfig.deleteRole(new RoleConfig(new CaseInsensitiveString("role99"))); fail("Should have blown up with an exception on the previous line as deleting role99 should blow up"); } catch (RuntimeException e) { assertTrue(Pattern.compile("does not exist").matcher(e.getMessage()).find()); } }
@Test public void getPluginRoleConfigsShouldReturnOnlyPluginRoles() { Role admin = new RoleConfig(new CaseInsensitiveString("admin")); Role view = new RoleConfig(new CaseInsensitiveString("view")); Role blackbird = new PluginRoleConfig("blackbird", "foo"); Role spacetiger = new PluginRoleConfig("spacetiger", "foo"); RolesConfig rolesConfig = new RolesConfig(admin, blackbird, view, spacetiger); List<PluginRoleConfig> roles = rolesConfig.getPluginRoleConfigs(); assertThat(roles, hasSize(2)); assertThat(roles, contains(blackbird, spacetiger)); }