diff --git a/exceptions/http_exceptions.py b/exceptions/http_exceptions.py index 13585ae..c6c1494 100644 --- a/exceptions/http_exceptions.py +++ b/exceptions/http_exceptions.py @@ -118,6 +118,15 @@ def __init__(self): ) +class CannotModifyDefaultRoleError(HTTPException): + """Raised when attempting to modify or delete a default system role.""" + def __init__(self, action: str = "modify"): + super().__init__( + status_code=403, + detail=f"Default system roles cannot be {action}d." + ) + + class DataIntegrityError(HTTPException): def __init__( self, diff --git a/routers/role.py b/routers/role.py index f6ecb2a..81770b1 100644 --- a/routers/role.py +++ b/routers/role.py @@ -9,7 +9,7 @@ from utils.db import get_session from utils.dependencies import get_authenticated_user from utils.models import Role, Permission, ValidPermissions, utc_time, User, DataIntegrityError -from exceptions.http_exceptions import InsufficientPermissionsError, InvalidPermissionError, RoleAlreadyExistsError, RoleNotFoundError, RoleHasUsersError +from exceptions.http_exceptions import InsufficientPermissionsError, InvalidPermissionError, RoleAlreadyExistsError, RoleNotFoundError, RoleHasUsersError, CannotModifyDefaultRoleError from routers.organization import router as organization_router logger = getLogger("uvicorn.error") @@ -84,6 +84,10 @@ def update_role( if not db_role: raise RoleNotFoundError() + # Prevent modification of default roles + if db_role.name in ["Owner", "Administrator", "Member"]: + raise CannotModifyDefaultRoleError(action="update") + # If any user-selected permissions are not valid, raise an error for permission in permissions: if permission not in ValidPermissions: @@ -148,6 +152,10 @@ def delete_role( if not db_role: raise RoleNotFoundError() + # Prevent deletion of default roles + if db_role.name in ["Owner", "Administrator", "Member"]: + raise CannotModifyDefaultRoleError(action="delete") + # Check that no users have the role if db_role.users: raise RoleHasUsersError() diff --git a/templates/organization/modals/roles_card.html b/templates/organization/modals/roles_card.html index af88772..c2e455d 100644 --- a/templates/organization/modals/roles_card.html +++ b/templates/organization/modals/roles_card.html @@ -17,7 +17,6 @@ {% endfor %} {% if organization.roles %} - {% if ns.custom_roles_exist %}
@@ -44,7 +43,7 @@ {% if ValidPermissions.EDIT_ROLE in user_permissions or ValidPermissions.DELETE_ROLE in user_permissions %}
- {% if ValidPermissions.EDIT_ROLE in user_permissions and role.name != "Owner" %} + {% if ValidPermissions.EDIT_ROLE in user_permissions and role.name not in ["Owner", "Administrator", "Member"] %} @@ -66,9 +65,6 @@
- {% else %} -

No custom roles defined

- {% endif %} {% else %}

No roles defined

{% endif %} @@ -120,7 +116,7 @@ {# Edit Role Modals #} {% if ValidPermissions.EDIT_ROLE in user_permissions %} {% for role in organization.roles %} - {% if role.name != "Owner" %} + {% if role.name not in ["Owner", "Administrator", "Member"] %}