Add account management permission
This commit is contained in:
@@ -6,6 +6,7 @@ from sqlmodel import Session, func, or_, select
|
||||
|
||||
from inse.auth import service as auth_service
|
||||
from inse.auth.model import AccessTokenMetadata, Account, Organization, UserRole
|
||||
from inse.auth.permissions import Permission, has_permission
|
||||
|
||||
from .model import (
|
||||
AccountPage,
|
||||
@@ -16,8 +17,12 @@ from .model import (
|
||||
)
|
||||
|
||||
|
||||
def _is_admin(role: UserRole) -> bool:
|
||||
return role in {UserRole.SUPER_ADMIN, UserRole.ADMIN}
|
||||
def _can_read_accounts(role: UserRole) -> bool:
|
||||
return has_permission(role, Permission.READ_ACCOUNTS)
|
||||
|
||||
|
||||
def _can_manage_accounts(role: UserRole) -> bool:
|
||||
return has_permission(role, Permission.MANAGE_ACCOUNTS)
|
||||
|
||||
|
||||
def _role_allowed_for_editor(editor_role: UserRole, role: UserRole) -> bool:
|
||||
@@ -109,11 +114,7 @@ def list_accounts(
|
||||
search: Optional[str] = None,
|
||||
organization_id: Optional[uuid.UUID] = None,
|
||||
) -> AccountPage:
|
||||
if access_token.effective_role not in {
|
||||
UserRole.SUPER_ADMIN,
|
||||
UserRole.ADMIN,
|
||||
UserRole.MANAGER,
|
||||
}:
|
||||
if not _can_read_accounts(access_token.effective_role):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only managers and admins can list accounts.",
|
||||
@@ -160,7 +161,7 @@ def create_account(
|
||||
access_token: AccessTokenMetadata,
|
||||
payload: CreateAccountRequest,
|
||||
) -> AccountProfile:
|
||||
if not _is_admin(access_token.effective_role):
|
||||
if not _can_manage_accounts(access_token.effective_role):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only admins can create accounts.",
|
||||
@@ -241,7 +242,7 @@ def update_account(
|
||||
_ensure_account_in_editor_scope(account, access_token)
|
||||
|
||||
is_self_update = account.id == access_token.effective_account_id
|
||||
can_admin_update = _is_admin(access_token.effective_role)
|
||||
can_admin_update = _can_manage_accounts(access_token.effective_role)
|
||||
|
||||
if not can_admin_update and not is_self_update:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
@@ -314,7 +315,7 @@ def delete_account(
|
||||
_ensure_account_in_editor_scope(account, access_token)
|
||||
|
||||
if (
|
||||
not _is_admin(access_token.effective_role)
|
||||
not _can_manage_accounts(access_token.effective_role)
|
||||
or account.id == access_token.effective_account_id
|
||||
):
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
@@ -322,4 +323,4 @@ def delete_account(
|
||||
account.deleted = True
|
||||
account.epoch += 1
|
||||
session.add(account)
|
||||
session.commit()
|
||||
session.commit()
|
||||
@@ -7,14 +7,23 @@ from inse.auth.model import UserRole
|
||||
|
||||
class Permission(StrEnum):
|
||||
MANAGE_ORGANIZATIONS = "MANAGE_ORGANIZATIONS"
|
||||
READ_ACCOUNTS = "READ_ACCOUNTS"
|
||||
MANAGE_ACCOUNTS = "MANAGE_ACCOUNTS"
|
||||
|
||||
|
||||
ROLE_PERMISSIONS: dict[UserRole, set[Permission]] = {
|
||||
UserRole.SUPER_ADMIN: {
|
||||
Permission.MANAGE_ORGANIZATIONS,
|
||||
Permission.READ_ACCOUNTS,
|
||||
Permission.MANAGE_ACCOUNTS,
|
||||
},
|
||||
UserRole.ADMIN: {
|
||||
Permission.READ_ACCOUNTS,
|
||||
Permission.MANAGE_ACCOUNTS,
|
||||
},
|
||||
UserRole.MANAGER: {
|
||||
Permission.READ_ACCOUNTS,
|
||||
},
|
||||
UserRole.ADMIN: set(),
|
||||
UserRole.MANAGER: set(),
|
||||
UserRole.USER: set(),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user