Skip to content

Commit 8a57863

Browse files
author
Mathias Bigaignon
committed
Rework of grants() function
1 parent 2714dac commit 8a57863

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

entitled/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import importlib.util
44
import pathlib
55
import types
6-
import typing
6+
from typing import Any, Type
77

88
from entitled import policies
99

@@ -12,7 +12,7 @@ class Client:
1212
"The Client class for decision-making centralization."
1313

1414
def __init__(self, base_path: str | None = None):
15-
self._policy_registrar: dict[typing.Type, policies.Policy] = {}
15+
self._policy_registrar: dict[Type, policies.Policy] = {}
1616
self._load_path = None
1717
if base_path:
1818
self._load_path = pathlib.Path(base_path)
@@ -26,7 +26,7 @@ def allows(self, action, actor, resource, context: dict | None = None) -> bool:
2626
policy = self._policy_lookup(resource)
2727
return policy.allows(action, actor, resource, context)
2828

29-
def grants(self, actor, resource, context: dict | None = None):
29+
def grants(self, actor, resource, context: dict | None = None) -> dict[Any, bool]:
3030
policy = self._policy_lookup(resource)
3131
return policy.grants(actor, resource, context)
3232

entitled/policies.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Grouping of authorization rules around a particular resource type"""
22

3-
from typing import Callable, Generic, TypeVar
3+
from typing import Any, Callable, Generic, TypeVar
44

55
from entitled import exceptions
66
from entitled.rules import Rule, RuleProtocol
@@ -45,12 +45,13 @@ def __register(self, action, *rules: Rule[T]):
4545
else:
4646
self._registry[action] = [*rules]
4747

48-
def grants(self, actor, resource: T, context: dict | None = None):
49-
50-
return filter(
51-
lambda action: self.allows(action, actor, resource, context),
52-
self._registry.keys(),
53-
)
48+
def grants(
49+
self, actor, resource: T, context: dict | None = None
50+
) -> dict[Any, bool]:
51+
return {
52+
action: self.allows(action, actor, resource, context)
53+
for action in self._registry
54+
}
5455

5556
def allows(
5657
self,

tests/test_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ def test_grants(self):
3939
user2 = User("user2", tenant2, set(["user"]))
4040
resource1 = Resource("R1", user1, tenant1)
4141

42-
u1_grants = [g for g in client.grants(user1, resource1)]
43-
u2_grants = [g for g in client.grants(user2, resource1)]
42+
u1_grants = client.grants(user1, resource1)
43+
u2_grants = client.grants(user2, resource1)
4444
assert "view" in u1_grants and "edit" in u1_grants
45-
assert "view" not in u2_grants and "edit" not in u2_grants
45+
assert u1_grants["view"] and u1_grants["edit"]
46+
assert "view" in u2_grants and "edit" in u2_grants
47+
assert not u2_grants["view"] and not u2_grants["edit"]
4648

4749
def test_allows(self):
4850
client = Client(base_path="tests/fixtures")

tests/test_policies.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from entitled import policies
12
from entitled.policies import Policy
23
from entitled.rules import Rule
34
from tests.fixtures.models import Tenant, User
@@ -76,7 +77,7 @@ def is_tenant_admin(
7677
user1 = User(name="user1", tenant=tenant1, roles=set([admin_role]))
7778
user2 = User(name="user2", tenant=tenant1, roles=set([guest_role]))
7879

79-
assert ["is_member", "has_admin_role", "is_tenant_admin"] == [
80-
item for item in policy.grants(user1, tenant1)
81-
]
82-
assert ["is_member"] == [item for item in policy.grants(user2, tenant1)]
80+
assert policy.grants(user1, tenant1)["is_member"]
81+
assert policy.grants(user2, tenant1)["is_member"]
82+
assert policy.grants(user1, tenant1)["is_tenant_admin"]
83+
assert not policy.grants(user2, tenant1)["is_tenant_admin"]

0 commit comments

Comments
 (0)