intermittent commit
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -23,6 +24,13 @@ from govoplan_core.core.organizations import ORGANIZATIONS_MODULE_ID, Organizati
|
||||
from govoplan_core.security.time import utc_now
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class FunctionAuthorizationContext:
|
||||
assignment_ids: tuple[str, ...]
|
||||
delegation_ids: tuple[str, ...]
|
||||
roles: tuple[Role, ...]
|
||||
|
||||
|
||||
def primary_identity_for_account(session: Session, account_id: str) -> Identity | None:
|
||||
return (
|
||||
session.query(Identity)
|
||||
@@ -169,6 +177,56 @@ def collect_function_roles(session: Session, user: User) -> list[Role]:
|
||||
)
|
||||
|
||||
|
||||
def collect_function_authorization_context(session: Session, user: User) -> FunctionAuthorizationContext:
|
||||
assignments = active_function_assignments_for_account(session, user.account_id, tenant_id=user.tenant_id)
|
||||
delegations = active_function_delegations_for_account(
|
||||
session,
|
||||
user.account_id,
|
||||
tenant_id=user.tenant_id,
|
||||
modes=("delegate", "act_in_place"),
|
||||
)
|
||||
direct_assignment_ids = {assignment.id for assignment in assignments}
|
||||
delegated_role_assignment_ids = {
|
||||
delegation.function_assignment_id
|
||||
for delegation in delegations
|
||||
if delegation.mode == "delegate"
|
||||
}
|
||||
role_assignment_ids = direct_assignment_ids | delegated_role_assignment_ids
|
||||
function_ids = {assignment.function_id for assignment in assignments}
|
||||
|
||||
missing_role_assignment_ids = delegated_role_assignment_ids - direct_assignment_ids
|
||||
if missing_role_assignment_ids:
|
||||
function_ids.update(
|
||||
row[0]
|
||||
for row in session.query(FunctionAssignment.function_id)
|
||||
.filter(
|
||||
FunctionAssignment.tenant_id == user.tenant_id,
|
||||
FunctionAssignment.id.in_(sorted(missing_role_assignment_ids)),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
roles: tuple[Role, ...] = ()
|
||||
if function_ids:
|
||||
roles = tuple(
|
||||
session.query(Role)
|
||||
.join(FunctionRoleAssignment, FunctionRoleAssignment.role_id == Role.id)
|
||||
.filter(
|
||||
FunctionRoleAssignment.tenant_id == user.tenant_id,
|
||||
FunctionRoleAssignment.function_id.in_(sorted(function_ids)),
|
||||
Role.tenant_id == user.tenant_id,
|
||||
)
|
||||
.order_by(Role.name.asc())
|
||||
.all()
|
||||
)
|
||||
|
||||
return FunctionAuthorizationContext(
|
||||
assignment_ids=tuple(sorted(role_assignment_ids)),
|
||||
delegation_ids=tuple(sorted(delegation.id for delegation in delegations)),
|
||||
roles=roles,
|
||||
)
|
||||
|
||||
|
||||
def collect_external_function_roles(
|
||||
session: Session,
|
||||
user: User,
|
||||
|
||||
Reference in New Issue
Block a user