Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/org/ohdsi/webapi/security/authz/RoleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.Set;

import org.ohdsi.webapi.security.authc.UserOrigin;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.server.ResponseStatusException;

/**
* The RoleService is intentionaly left package-protected, as all interactions
Expand All @@ -21,6 +23,10 @@
@Service
class RoleService {

/** The roles the baseline migration creates; logins and permission checks rely on them by name. */
static final Set<String> BUILT_IN_ROLES = Set.of("anonymous", "public", "admin", "concept set creator",
"Tag Admin", "cohort creator", "cohort reader", "Read-restricted");

private final UserService userService;
private final PermissionService permissionService;
private final RoleRepository roleRepository;
Expand Down Expand Up @@ -67,9 +73,19 @@ public RoleEntity updateRole(RoleEntity roleEntity) {
}

public void removeRole(Long roleId) {
this.roleRepository.findById(roleId).ifPresent(role -> {
if (isBuiltInRole(role)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
String.format("The built-in role '%s' cannot be deleted", role.getName()));
}
});
this.roleRepository.deleteById(roleId);
}

static boolean isBuiltInRole(RoleEntity role) {
return Boolean.TRUE.equals(role.isSystemRole()) && BUILT_IN_ROLES.contains(role.getName());
}

// -------------------------
// Role Lists and Lookup
// -------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.ohdsi.webapi.security.authz;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Test;
import org.ohdsi.webapi.AbstractDatabaseTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

public class RemoveBuiltInRoleTest extends AbstractDatabaseTest {

private static final Long ROLE_ID = 53001L;

@Autowired
private AuthorizationService authorizationService;

@After
public void deleteRole() {
jdbcTemplate.update("DELETE FROM " + ohdsiSchema + ".sec_role WHERE id = ?", ROLE_ID);
}

private int countRoles(String name) {
return jdbcTemplate.queryForObject("SELECT count(*) FROM " + ohdsiSchema + ".sec_role WHERE name = ?",
Integer.class, name);
}

@Test
public void builtInRolesCannotBeDeleted() {
for (String name : RoleService.BUILT_IN_ROLES) {
Long id = jdbcTemplate.queryForObject("SELECT id FROM " + ohdsiSchema + ".sec_role WHERE name = ?",
Long.class, name);
try {
authorizationService.removeRole(id);
fail("deleted built-in role " + name);
} catch (ResponseStatusException e) {
assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
}
assertEquals(1, countRoles(name));
}
}

@Test
public void otherRolesCanBeDeleted() {
jdbcTemplate.update("INSERT INTO " + ohdsiSchema + ".sec_role (id, name, system_role) VALUES (?, ?, true)",
ROLE_ID, "RemoveRoleTestRole");

authorizationService.removeRole(ROLE_ID);

assertEquals(0, countRoles("RemoveRoleTestRole"));
}

@Test
public void personalRoleWithABuiltInNameCanBeDeleted() {
// a user with the login "admin" has a personal (non-system) role of that name
jdbcTemplate.update("INSERT INTO " + ohdsiSchema + ".sec_role (id, name, system_role) VALUES (?, 'admin', false)",
ROLE_ID);

authorizationService.removeRole(ROLE_ID);

assertEquals(1, countRoles("admin"));
}
}
Loading