Compare commits
6
Commits
dev
...
permissions
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bdf5fabd45 | ||
|
|
269c1a7a55 | ||
|
|
d120c62576 | ||
|
|
e1f58d6380 | ||
|
|
8261c0a52f | ||
|
|
e76bd217ac |
@@ -17,6 +17,7 @@ class AccountProfile(BaseModel):
|
|||||||
email: str
|
email: str
|
||||||
name: str
|
name: str
|
||||||
role: UserRole
|
role: UserRole
|
||||||
|
permissions: list[str] = Field(default_factory=list)
|
||||||
organization_id: uuid.UUID
|
organization_id: uuid.UUID
|
||||||
organization_name: str
|
organization_name: str
|
||||||
deleted: bool
|
deleted: bool
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from sqlmodel import Session, func, or_, select
|
|||||||
|
|
||||||
from inse.auth import service as auth_service
|
from inse.auth import service as auth_service
|
||||||
from inse.auth.model import AccessTokenMetadata, Account, Organization, UserRole
|
from inse.auth.model import AccessTokenMetadata, Account, Organization, UserRole
|
||||||
|
from inse.auth.permissions import Permission, get_permission_values, has_permission
|
||||||
|
|
||||||
from .model import (
|
from .model import (
|
||||||
AccountPage,
|
AccountPage,
|
||||||
@@ -16,8 +17,12 @@ from .model import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _is_admin(role: UserRole) -> bool:
|
def _can_read_accounts(role: UserRole) -> bool:
|
||||||
return role in {UserRole.SUPER_ADMIN, UserRole.ADMIN}
|
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:
|
def _role_allowed_for_editor(editor_role: UserRole, role: UserRole) -> bool:
|
||||||
@@ -35,6 +40,7 @@ def _account_to_profile(account: Account, token: AccessTokenMetadata) -> Account
|
|||||||
email=account.email,
|
email=account.email,
|
||||||
name=account.name,
|
name=account.name,
|
||||||
role=account.role,
|
role=account.role,
|
||||||
|
permissions=get_permission_values(token.effective_role),
|
||||||
organization_id=account.organization_id,
|
organization_id=account.organization_id,
|
||||||
organization_name=organization_name,
|
organization_name=organization_name,
|
||||||
deleted=account.deleted,
|
deleted=account.deleted,
|
||||||
@@ -109,11 +115,7 @@ def list_accounts(
|
|||||||
search: Optional[str] = None,
|
search: Optional[str] = None,
|
||||||
organization_id: Optional[uuid.UUID] = None,
|
organization_id: Optional[uuid.UUID] = None,
|
||||||
) -> AccountPage:
|
) -> AccountPage:
|
||||||
if access_token.effective_role not in {
|
if not _can_read_accounts(access_token.effective_role):
|
||||||
UserRole.SUPER_ADMIN,
|
|
||||||
UserRole.ADMIN,
|
|
||||||
UserRole.MANAGER,
|
|
||||||
}:
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_403_FORBIDDEN,
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
detail="Only managers and admins can list accounts.",
|
detail="Only managers and admins can list accounts.",
|
||||||
@@ -160,7 +162,7 @@ def create_account(
|
|||||||
access_token: AccessTokenMetadata,
|
access_token: AccessTokenMetadata,
|
||||||
payload: CreateAccountRequest,
|
payload: CreateAccountRequest,
|
||||||
) -> AccountProfile:
|
) -> AccountProfile:
|
||||||
if not _is_admin(access_token.effective_role):
|
if not _can_manage_accounts(access_token.effective_role):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_403_FORBIDDEN,
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
detail="Only admins can create accounts.",
|
detail="Only admins can create accounts.",
|
||||||
@@ -241,7 +243,7 @@ def update_account(
|
|||||||
_ensure_account_in_editor_scope(account, access_token)
|
_ensure_account_in_editor_scope(account, access_token)
|
||||||
|
|
||||||
is_self_update = account.id == access_token.effective_account_id
|
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:
|
if not can_admin_update and not is_self_update:
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||||
@@ -314,7 +316,7 @@ def delete_account(
|
|||||||
_ensure_account_in_editor_scope(account, access_token)
|
_ensure_account_in_editor_scope(account, access_token)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
not _is_admin(access_token.effective_role)
|
not _can_manage_accounts(access_token.effective_role)
|
||||||
or account.id == access_token.effective_account_id
|
or account.id == access_token.effective_account_id
|
||||||
):
|
):
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
from enum import StrEnum
|
||||||
|
|
||||||
|
from fastapi import HTTPException, status
|
||||||
|
|
||||||
|
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.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.",
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_permission_values(role: UserRole) -> list[str]:
|
||||||
|
permissions = ROLE_PERMISSIONS.get(role, set())
|
||||||
|
return [permission.value for permission in permissions]
|
||||||
@@ -4,7 +4,9 @@ from typing import Optional
|
|||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from sqlmodel import Session, func, select
|
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 (
|
from .model import (
|
||||||
CreateOrganizationRequest,
|
CreateOrganizationRequest,
|
||||||
@@ -14,12 +16,11 @@ from .model import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _require_super_admin(access_token: AccessTokenMetadata) -> None:
|
def _require_manage_organizations(access_token: AccessTokenMetadata) -> None:
|
||||||
if access_token.effective_role != UserRole.SUPER_ADMIN:
|
require_permission(
|
||||||
raise HTTPException(
|
access_token.effective_role,
|
||||||
status_code=status.HTTP_403_FORBIDDEN,
|
Permission.MANAGE_ORGANIZATIONS,
|
||||||
detail="Only superadmins can manage organizations.",
|
)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _normalize_name(name: str) -> str:
|
def _normalize_name(name: str) -> str:
|
||||||
@@ -117,7 +118,7 @@ def get_organizations(
|
|||||||
statement = select(Organization)
|
statement = select(Organization)
|
||||||
count_statement = select(func.count()).select_from(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
|
scope_filter = Organization.id == access_token.effective_organization_id
|
||||||
statement = statement.where(scope_filter)
|
statement = statement.where(scope_filter)
|
||||||
count_statement = count_statement.where(scope_filter)
|
count_statement = count_statement.where(scope_filter)
|
||||||
@@ -148,7 +149,7 @@ def create_organization(
|
|||||||
access_token: AccessTokenMetadata,
|
access_token: AccessTokenMetadata,
|
||||||
payload: CreateOrganizationRequest,
|
payload: CreateOrganizationRequest,
|
||||||
) -> OrganizationProfile:
|
) -> OrganizationProfile:
|
||||||
_require_super_admin(access_token)
|
_require_manage_organizations(access_token)
|
||||||
name = _normalize_name(payload.name)
|
name = _normalize_name(payload.name)
|
||||||
_ensure_name_available(session, name)
|
_ensure_name_available(session, name)
|
||||||
|
|
||||||
@@ -168,7 +169,7 @@ def update_organization(
|
|||||||
organization_id: uuid.UUID,
|
organization_id: uuid.UUID,
|
||||||
payload: UpdateOrganizationRequest,
|
payload: UpdateOrganizationRequest,
|
||||||
) -> OrganizationProfile:
|
) -> OrganizationProfile:
|
||||||
_require_super_admin(access_token)
|
_require_manage_organizations(access_token)
|
||||||
organization = _get_organization_or_404(session, organization_id)
|
organization = _get_organization_or_404(session, organization_id)
|
||||||
|
|
||||||
if payload.name is not None:
|
if payload.name is not None:
|
||||||
@@ -194,7 +195,7 @@ def delete_organization(
|
|||||||
access_token: AccessTokenMetadata,
|
access_token: AccessTokenMetadata,
|
||||||
organization_id: uuid.UUID,
|
organization_id: uuid.UUID,
|
||||||
) -> None:
|
) -> None:
|
||||||
_require_super_admin(access_token)
|
_require_manage_organizations(access_token)
|
||||||
organization = _get_organization_or_404(session, organization_id)
|
organization = _get_organization_or_404(session, organization_id)
|
||||||
if not organization.deleted:
|
if not organization.deleted:
|
||||||
_deactivate_organization_accounts(session, organization.id)
|
_deactivate_organization_accounts(session, organization.id)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export interface AccountProfile {
|
|||||||
email: string;
|
email: string;
|
||||||
name: string;
|
name: string;
|
||||||
role: UserRole;
|
role: UserRole;
|
||||||
|
permissions?: Array<string>;
|
||||||
organization_id: string;
|
organization_id: string;
|
||||||
organization_name: string;
|
organization_name: string;
|
||||||
deleted: boolean;
|
deleted: boolean;
|
||||||
|
|||||||
@@ -22,12 +22,7 @@ export const accountViewerGuard: CanActivateFn = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const mayViewAccounts =
|
if (!credentialService.canReadAccounts()) {
|
||||||
credentialService.isEffectiveSuperAdmin() ||
|
|
||||||
credentialService.isEffectiveAdmin() ||
|
|
||||||
credentialService.isEffectiveManager();
|
|
||||||
|
|
||||||
if (!mayViewAccounts) {
|
|
||||||
void router.navigate(['/dashboard']);
|
void router.navigate(['/dashboard']);
|
||||||
resolve(false);
|
resolve(false);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -94,6 +94,22 @@ export class CredentialService {
|
|||||||
() => this.accountProfile()?.organization_name ?? '',
|
() => this.accountProfile()?.organization_name ?? '',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public readonly permissions = computed(
|
||||||
|
() => this.accountProfile()?.permissions ?? [],
|
||||||
|
);
|
||||||
|
|
||||||
|
public readonly canReadAccounts = computed(() =>
|
||||||
|
this.permissions().includes('READ_ACCOUNTS'),
|
||||||
|
);
|
||||||
|
|
||||||
|
public readonly canManageAccounts = computed(() =>
|
||||||
|
this.permissions().includes('MANAGE_ACCOUNTS'),
|
||||||
|
);
|
||||||
|
|
||||||
|
public readonly canManageOrganizations = computed(() =>
|
||||||
|
this.permissions().includes('MANAGE_ORGANIZATIONS'),
|
||||||
|
);
|
||||||
|
|
||||||
public readonly realAccountId = computed(
|
public readonly realAccountId = computed(
|
||||||
() => this.accessToken()?.real_account_id ?? '',
|
() => this.accessToken()?.real_account_id ?? '',
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -44,15 +44,8 @@
|
|||||||
@for (itemGroup of navItemGroups; track $index) {
|
@for (itemGroup of navItemGroups; track $index) {
|
||||||
<div>
|
<div>
|
||||||
@if (
|
@if (
|
||||||
(!itemGroup.adminOnly ||
|
!itemGroup.requiredPermission ||
|
||||||
credentialService.isEffectiveSuperAdmin() ||
|
credentialService.permissions().includes(itemGroup.requiredPermission)
|
||||||
credentialService.isEffectiveAdmin()) &&
|
|
||||||
(!itemGroup.superAdminOnly ||
|
|
||||||
credentialService.isEffectiveSuperAdmin()) &&
|
|
||||||
(!itemGroup.managerOrAdminOnly ||
|
|
||||||
credentialService.isEffectiveSuperAdmin() ||
|
|
||||||
credentialService.isEffectiveAdmin() ||
|
|
||||||
credentialService.isEffectiveManager())
|
|
||||||
) {
|
) {
|
||||||
<mat-divider></mat-divider>
|
<mat-divider></mat-divider>
|
||||||
@if (!collapsed()) {
|
@if (!collapsed()) {
|
||||||
|
|||||||
@@ -17,17 +17,13 @@ type NavItem = {
|
|||||||
route: string;
|
route: string;
|
||||||
disabled: boolean;
|
disabled: boolean;
|
||||||
queryParams?: Record<string, string | number | boolean>;
|
queryParams?: Record<string, string | number | boolean>;
|
||||||
adminOnly?: boolean;
|
requiredPermission?: string;
|
||||||
superAdminOnly?: boolean;
|
|
||||||
managerOrAdminOnly?: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type NavItemGroup = {
|
type NavItemGroup = {
|
||||||
label: string;
|
label: string;
|
||||||
items: NavItem[];
|
items: NavItem[];
|
||||||
adminOnly?: boolean;
|
requiredPermission?: string;
|
||||||
superAdminOnly?: boolean;
|
|
||||||
managerOrAdminOnly?: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@@ -127,36 +123,35 @@ export class AppMenuComponent {
|
|||||||
icon: 'manage_accounts',
|
icon: 'manage_accounts',
|
||||||
route: '/accounts',
|
route: '/accounts',
|
||||||
disabled: false,
|
disabled: false,
|
||||||
managerOrAdminOnly: true,
|
requiredPermission: 'READ_ACCOUNTS',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Organisationen',
|
label: 'Organisationen',
|
||||||
icon: 'domain',
|
icon: 'domain',
|
||||||
route: '/organizations',
|
route: '/organizations',
|
||||||
disabled: false,
|
disabled: false,
|
||||||
superAdminOnly: true,
|
requiredPermission: 'MANAGE_ORGANIZATIONS',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
navItemGroups: NavItemGroup[] = [
|
navItemGroups: NavItemGroup[] = [
|
||||||
{
|
{
|
||||||
label: 'Navigation',
|
label: 'Navigation',
|
||||||
items: this.navItems.filter(
|
items: this.navItems.filter((item) => !item.requiredPermission),
|
||||||
(item) =>
|
|
||||||
!item.adminOnly && !item.superAdminOnly && !item.managerOrAdminOnly,
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Administration',
|
label: 'Administration',
|
||||||
items: this.navItems.filter(
|
items: this.navItems.filter(
|
||||||
(item) =>
|
(item) => item.requiredPermission === 'READ_ACCOUNTS',
|
||||||
(item.adminOnly || item.managerOrAdminOnly) && !item.superAdminOnly,
|
|
||||||
),
|
),
|
||||||
managerOrAdminOnly: true,
|
requiredPermission: 'READ_ACCOUNTS',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Super Administration',
|
label: 'Super Administration',
|
||||||
items: this.navItems.filter((item) => item.superAdminOnly),
|
items: this.navItems.filter(
|
||||||
superAdminOnly: true,
|
(item) => item.requiredPermission === 'MANAGE_ORGANIZATIONS',
|
||||||
|
),
|
||||||
|
requiredPermission: 'MANAGE_ORGANIZATIONS',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user