Add role permissions
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
from enum import StrEnum
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from inse.auth.model import UserRole
|
||||
|
||||
|
||||
class Permission(StrEnum):
|
||||
MANAGE_ORGANIZATIONS = "MANAGE_ORGANIZATIONS"
|
||||
|
||||
|
||||
ROLE_PERMISSIONS: dict[UserRole, set[Permission]] = {
|
||||
UserRole.SUPER_ADMIN: {
|
||||
Permission.MANAGE_ORGANIZATIONS,
|
||||
},
|
||||
UserRole.ADMIN: set(),
|
||||
UserRole.MANAGER: set(),
|
||||
UserRole.USER: set(),
|
||||
}
|
||||
|
||||
|
||||
def has_permission(role: UserRole, permission: Permission) -> bool:
|
||||
return permission in ROLE_PERMISSIONS.get(role, set())
|
||||
|
||||
|
||||
def require_permission(role: UserRole, permission: Permission) -> None:
|
||||
if not has_permission(role, permission):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Missing permission.",
|
||||
)
|
||||
@@ -4,7 +4,9 @@ from typing import Optional
|
||||
from fastapi import HTTPException, status
|
||||
from sqlmodel import Session, func, select
|
||||
|
||||
from inse.auth.model import AccessTokenMetadata, Account, Organization, UserRole
|
||||
from inse.auth.model import AccessTokenMetadata, Account, Organization
|
||||
from inse.auth.permissions import Permission, has_permission, require_permission
|
||||
|
||||
|
||||
from .model import (
|
||||
CreateOrganizationRequest,
|
||||
@@ -14,12 +16,11 @@ from .model import (
|
||||
)
|
||||
|
||||
|
||||
def _require_super_admin(access_token: AccessTokenMetadata) -> None:
|
||||
if access_token.effective_role != UserRole.SUPER_ADMIN:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only superadmins can manage organizations.",
|
||||
)
|
||||
def _require_manage_organizations(access_token: AccessTokenMetadata) -> None:
|
||||
require_permission(
|
||||
access_token.effective_role,
|
||||
Permission.MANAGE_ORGANIZATIONS,
|
||||
)
|
||||
|
||||
|
||||
def _normalize_name(name: str) -> str:
|
||||
@@ -117,7 +118,7 @@ def get_organizations(
|
||||
statement = select(Organization)
|
||||
count_statement = select(func.count()).select_from(Organization)
|
||||
|
||||
if access_token.effective_role != UserRole.SUPER_ADMIN:
|
||||
if not has_permission(access_token.effective_role, Permission.MANAGE_ORGANIZATIONS):
|
||||
scope_filter = Organization.id == access_token.effective_organization_id
|
||||
statement = statement.where(scope_filter)
|
||||
count_statement = count_statement.where(scope_filter)
|
||||
@@ -148,7 +149,7 @@ def create_organization(
|
||||
access_token: AccessTokenMetadata,
|
||||
payload: CreateOrganizationRequest,
|
||||
) -> OrganizationProfile:
|
||||
_require_super_admin(access_token)
|
||||
_require_manage_organizations(access_token)
|
||||
name = _normalize_name(payload.name)
|
||||
_ensure_name_available(session, name)
|
||||
|
||||
@@ -168,7 +169,7 @@ def update_organization(
|
||||
organization_id: uuid.UUID,
|
||||
payload: UpdateOrganizationRequest,
|
||||
) -> OrganizationProfile:
|
||||
_require_super_admin(access_token)
|
||||
_require_manage_organizations(access_token)
|
||||
organization = _get_organization_or_404(session, organization_id)
|
||||
|
||||
if payload.name is not None:
|
||||
@@ -194,10 +195,10 @@ def delete_organization(
|
||||
access_token: AccessTokenMetadata,
|
||||
organization_id: uuid.UUID,
|
||||
) -> None:
|
||||
_require_super_admin(access_token)
|
||||
_require_manage_organizations(access_token)
|
||||
organization = _get_organization_or_404(session, organization_id)
|
||||
if not organization.deleted:
|
||||
_deactivate_organization_accounts(session, organization.id)
|
||||
organization.deleted = True
|
||||
session.add(organization)
|
||||
session.commit()
|
||||
session.commit()
|
||||
Reference in New Issue
Block a user