-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add override_context global permission
- Loading branch information
Showing
6 changed files
with
153 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from infrahub.core.account import GlobalPermission | ||
from infrahub.core.constants import GLOBAL_BRANCH_NAME, GlobalPermissions, PermissionDecision | ||
from infrahub.core.registry import registry | ||
|
||
|
||
def define_global_permission_from_branch(permission: GlobalPermissions, branch_name: str) -> GlobalPermission: | ||
if branch_name in (GLOBAL_BRANCH_NAME, registry.default_branch): | ||
decision = PermissionDecision.ALLOW_DEFAULT | ||
else: | ||
decision = PermissionDecision.ALLOW_OTHER | ||
|
||
return GlobalPermission( | ||
action=permission.value, | ||
decision=decision.value, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from infrahub.core import registry | ||
from infrahub.core.constants import InfrahubKind | ||
from infrahub.core.node import Node | ||
from infrahub.core.protocols import CoreAccountGroup | ||
from infrahub.database import InfrahubDatabase | ||
from infrahub.permissions import LocalPermissionBackend | ||
|
||
if TYPE_CHECKING: | ||
from infrahub.core.account import GlobalPermission, ObjectPermission | ||
from infrahub.database import InfrahubDatabase | ||
|
||
|
||
async def define_permissions( | ||
account: Node, | ||
db: InfrahubDatabase, | ||
object_permissions: list[ObjectPermission] | None = None, | ||
global_permissions: list[GlobalPermission] | None = None, | ||
) -> None: | ||
registry.permission_backends = [LocalPermissionBackend()] | ||
|
||
object_permissions = object_permissions or [] | ||
global_permissions = global_permissions or [] | ||
permissions = [] | ||
for object_permission in object_permissions: | ||
obj = await Node.init(db=db, schema=InfrahubKind.OBJECTPERMISSION) | ||
await obj.new( | ||
db=db, | ||
namespace=object_permission.namespace, | ||
name=object_permission.name, | ||
action=object_permission.action, | ||
decision=object_permission.decision, | ||
) | ||
await obj.save(db=db) | ||
permissions.append(obj) | ||
|
||
for global_permission in global_permissions: | ||
obj = await Node.init(db=db, schema=InfrahubKind.GLOBALPERMISSION) | ||
await obj.new( | ||
db=db, | ||
action=global_permission.action, | ||
decision=global_permission.decision, | ||
) | ||
await obj.save(db=db) | ||
permissions.append(obj) | ||
|
||
role = await Node.init(db=db, schema=InfrahubKind.ACCOUNTROLE) | ||
await role.new(db=db, name="chief-people-officer", permissions=permissions) | ||
await role.save(db=db) | ||
|
||
group = await Node.init(db=db, schema=CoreAccountGroup) | ||
await group.new(db=db, name="hr", roles=[role]) | ||
await group.save(db=db) | ||
|
||
await group.members.add(db=db, data={"id": account.id}) | ||
await group.members.save(db=db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters