Skip to content

Commit 28718e3

Browse files
committed
Refactor: Greatly simplifies write checking conditions
1 parent f3b1ea5 commit 28718e3

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

kingdom/access/flow.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ def is_write_allowed(
175175

176176
def mask_pass(owned_permissions: int, requested_operation: int,) -> bool:
177177
"""
178-
When a subject tries to perform a write, this function calculates
179-
whether the requested operation is allowed on a set of owned
178+
Calculates whether the requested operation is allowed on a set of owned
180179
permissions.
181180
182181
>>> owned_perm = (
@@ -186,34 +185,24 @@ def mask_pass(owned_permissions: int, requested_operation: int,) -> bool:
186185
>>> is_allowed(owned_perm, requested_op)
187186
False
188187
"""
189-
return int(owned_permissions & requested_operation) > 0
188+
return bool(owned_permissions & requested_operation)
190189

191-
TOKEN_ALL = "*"
192190
assert access_request.operation & (
193191
Permission.CREATE | Permission.UPDATE | Permission.DELETE
194192
)
195193

196194
resource = access_request.resource
197195
operation = access_request.operation
198-
selector = access_request.selector
199196
if resource not in owned_policies:
200197
# Resource is unknown to subject.
201198
return False
202199

203-
if selector == TOKEN_ALL and selector not in owned_policies[resource]:
204-
# For e.g CREATE without CREATE ALL policy
205-
return False
200+
permissions = owned_policies[resource].get(access_request.selector, 0)
206201

207202
if TOKEN_ALL in owned_policies[resource]:
208203
# We might be asking for a specific instance but we have a "*" policy
209204
# that contemplates it.
210205
if mask_pass(owned_policies[resource][TOKEN_ALL], operation):
211206
return True
212207

213-
# Now that we've reached here, it means that subject has no "*" policy
214-
# to allow it for asked resource. Hence it must have a specific policy
215-
if selector not in owned_policies[resource]:
216-
return False
217-
218-
permissions = owned_policies[resource][selector]
219208
return mask_pass(permissions, operation)

0 commit comments

Comments
 (0)