Compare commits
6
Commits
falkordb
...
permissions
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bdf5fabd45 | ||
|
|
269c1a7a55 | ||
|
|
d120c62576 | ||
|
|
e1f58d6380 | ||
|
|
8261c0a52f | ||
|
|
e76bd217ac |
@@ -17,6 +17,7 @@ class AccountProfile(BaseModel):
|
||||
email: str
|
||||
name: str
|
||||
role: UserRole
|
||||
permissions: list[str] = Field(default_factory=list)
|
||||
organization_id: uuid.UUID
|
||||
organization_name: str
|
||||
deleted: bool
|
||||
|
||||
@@ -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, get_permission_values, 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:
|
||||
@@ -35,6 +40,7 @@ def _account_to_profile(account: Account, token: AccessTokenMetadata) -> Account
|
||||
email=account.email,
|
||||
name=account.name,
|
||||
role=account.role,
|
||||
permissions=get_permission_values(token.effective_role),
|
||||
organization_id=account.organization_id,
|
||||
organization_name=organization_name,
|
||||
deleted=account.deleted,
|
||||
@@ -109,11 +115,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 +162,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 +243,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 +316,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 +324,4 @@ def delete_account(
|
||||
account.deleted = True
|
||||
account.epoch += 1
|
||||
session.add(account)
|
||||
session.commit()
|
||||
session.commit()
|
||||
@@ -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 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()
|
||||
@@ -14,6 +14,7 @@ export interface AccountProfile {
|
||||
email: string;
|
||||
name: string;
|
||||
role: UserRole;
|
||||
permissions?: Array<string>;
|
||||
organization_id: string;
|
||||
organization_name: string;
|
||||
deleted: boolean;
|
||||
|
||||
@@ -22,12 +22,7 @@ export const accountViewerGuard: CanActivateFn = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
const mayViewAccounts =
|
||||
credentialService.isEffectiveSuperAdmin() ||
|
||||
credentialService.isEffectiveAdmin() ||
|
||||
credentialService.isEffectiveManager();
|
||||
|
||||
if (!mayViewAccounts) {
|
||||
if (!credentialService.canReadAccounts()) {
|
||||
void router.navigate(['/dashboard']);
|
||||
resolve(false);
|
||||
return;
|
||||
@@ -38,4 +33,4 @@ export const accountViewerGuard: CanActivateFn = () => {
|
||||
{ injector },
|
||||
);
|
||||
});
|
||||
};
|
||||
};
|
||||
@@ -94,6 +94,22 @@ export class CredentialService {
|
||||
() => 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(
|
||||
() => this.accessToken()?.real_account_id ?? '',
|
||||
);
|
||||
|
||||
@@ -44,15 +44,8 @@
|
||||
@for (itemGroup of navItemGroups; track $index) {
|
||||
<div>
|
||||
@if (
|
||||
(!itemGroup.adminOnly ||
|
||||
credentialService.isEffectiveSuperAdmin() ||
|
||||
credentialService.isEffectiveAdmin()) &&
|
||||
(!itemGroup.superAdminOnly ||
|
||||
credentialService.isEffectiveSuperAdmin()) &&
|
||||
(!itemGroup.managerOrAdminOnly ||
|
||||
credentialService.isEffectiveSuperAdmin() ||
|
||||
credentialService.isEffectiveAdmin() ||
|
||||
credentialService.isEffectiveManager())
|
||||
!itemGroup.requiredPermission ||
|
||||
credentialService.permissions().includes(itemGroup.requiredPermission)
|
||||
) {
|
||||
<mat-divider></mat-divider>
|
||||
@if (!collapsed()) {
|
||||
@@ -149,4 +142,4 @@
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</aside>
|
||||
</aside>
|
||||
@@ -17,17 +17,13 @@ type NavItem = {
|
||||
route: string;
|
||||
disabled: boolean;
|
||||
queryParams?: Record<string, string | number | boolean>;
|
||||
adminOnly?: boolean;
|
||||
superAdminOnly?: boolean;
|
||||
managerOrAdminOnly?: boolean;
|
||||
requiredPermission?: string;
|
||||
};
|
||||
|
||||
type NavItemGroup = {
|
||||
label: string;
|
||||
items: NavItem[];
|
||||
adminOnly?: boolean;
|
||||
superAdminOnly?: boolean;
|
||||
managerOrAdminOnly?: boolean;
|
||||
requiredPermission?: string;
|
||||
};
|
||||
|
||||
@Component({
|
||||
@@ -127,36 +123,35 @@ export class AppMenuComponent {
|
||||
icon: 'manage_accounts',
|
||||
route: '/accounts',
|
||||
disabled: false,
|
||||
managerOrAdminOnly: true,
|
||||
requiredPermission: 'READ_ACCOUNTS',
|
||||
},
|
||||
{
|
||||
label: 'Organisationen',
|
||||
icon: 'domain',
|
||||
route: '/organizations',
|
||||
disabled: false,
|
||||
superAdminOnly: true,
|
||||
requiredPermission: 'MANAGE_ORGANIZATIONS',
|
||||
},
|
||||
];
|
||||
|
||||
navItemGroups: NavItemGroup[] = [
|
||||
{
|
||||
label: 'Navigation',
|
||||
items: this.navItems.filter(
|
||||
(item) =>
|
||||
!item.adminOnly && !item.superAdminOnly && !item.managerOrAdminOnly,
|
||||
),
|
||||
items: this.navItems.filter((item) => !item.requiredPermission),
|
||||
},
|
||||
{
|
||||
label: 'Administration',
|
||||
items: this.navItems.filter(
|
||||
(item) =>
|
||||
(item.adminOnly || item.managerOrAdminOnly) && !item.superAdminOnly,
|
||||
(item) => item.requiredPermission === 'READ_ACCOUNTS',
|
||||
),
|
||||
managerOrAdminOnly: true,
|
||||
requiredPermission: 'READ_ACCOUNTS',
|
||||
},
|
||||
{
|
||||
label: 'Super Administration',
|
||||
items: this.navItems.filter((item) => item.superAdminOnly),
|
||||
superAdminOnly: true,
|
||||
items: this.navItems.filter(
|
||||
(item) => item.requiredPermission === 'MANAGE_ORGANIZATIONS',
|
||||
),
|
||||
requiredPermission: 'MANAGE_ORGANIZATIONS',
|
||||
},
|
||||
];
|
||||
|
||||
@@ -182,4 +177,4 @@ export class AppMenuComponent {
|
||||
void this.credentialService.logoutAccount();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user