From 45f765f8027014ff51e38be1e17496617f3357f0 Mon Sep 17 00:00:00 2001 From: Peter Hoffmann <954078+p-hoffmann@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:39:32 +0800 Subject: [PATCH] Refuse to delete the built-in roles --- .../webapi/security/authz/RoleService.java | 16 +++++ .../security/authz/RemoveBuiltInRoleTest.java | 65 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/test/java/org/ohdsi/webapi/security/authz/RemoveBuiltInRoleTest.java diff --git a/src/main/java/org/ohdsi/webapi/security/authz/RoleService.java b/src/main/java/org/ohdsi/webapi/security/authz/RoleService.java index 42484e1305..da8a7d8552 100644 --- a/src/main/java/org/ohdsi/webapi/security/authz/RoleService.java +++ b/src/main/java/org/ohdsi/webapi/security/authz/RoleService.java @@ -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 @@ -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 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; @@ -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 // ------------------------- diff --git a/src/test/java/org/ohdsi/webapi/security/authz/RemoveBuiltInRoleTest.java b/src/test/java/org/ohdsi/webapi/security/authz/RemoveBuiltInRoleTest.java new file mode 100644 index 0000000000..bd8a638ccd --- /dev/null +++ b/src/test/java/org/ohdsi/webapi/security/authz/RemoveBuiltInRoleTest.java @@ -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")); + } +}