Skip to content

Commit

Permalink
Fix perm lookup with several perm backends
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazoyer committed Nov 25, 2024
1 parent 625ad4a commit e93c8a7
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions backend/infrahub/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,25 @@ async def load_schema(
account_session: AccountSession = Depends(get_current_user),
) -> SchemaUpdate:
has_permission = False
has_branch_permission = branch.name not in (GLOBAL_BRANCH_NAME, registry.default_branch)
for permission_backend in registry.permission_backends:
has_permission = await permission_backend.has_permission(
db=db,
account_session=account_session,
permission=GlobalPermission(
action=GlobalPermissions.MANAGE_SCHEMA.value,
decision=(
PermissionDecision.ALLOW_DEFAULT
if branch.name in (GLOBAL_BRANCH_NAME, registry.default_branch)
else PermissionDecision.ALLOW_OTHER
).value,
),
branch=branch,
)
if branch.name in (GLOBAL_BRANCH_NAME, registry.default_branch):
has_permission &= await permission_backend.has_permission(
if not has_permission:
has_permission = await permission_backend.has_permission(
db=db,
account_session=account_session,
permission=GlobalPermission(
action=GlobalPermissions.MANAGE_SCHEMA.value,
decision=(
PermissionDecision.ALLOW_DEFAULT
if branch.name in (GLOBAL_BRANCH_NAME, registry.default_branch)
else PermissionDecision.ALLOW_OTHER
).value,
),
branch=branch,
)

if not has_branch_permission:
has_branch_permission = permission_backend.has_permission(
db=db,
account_session=account_session,
permission=GlobalPermission(
Expand All @@ -269,10 +272,10 @@ async def load_schema(
branch=branch,
)

if has_permission:
if has_permission and has_branch_permission:
break

if not has_permission:
if not has_permission or not has_branch_permission:
raise PermissionDeniedError("You are not allowed to manage the schema")

service: InfrahubServices = request.app.state.service
Expand Down

0 comments on commit e93c8a7

Please sign in to comment.