Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
[REVERTABLE]: Remove support for blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Aug 30, 2024
1 parent e18c8cb commit 9340434
Showing 1 changed file with 14 additions and 42 deletions.
56 changes: 14 additions & 42 deletions jaclang/runtimelib/architype.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,9 @@ class Access:
whitelist: bool = True # whitelist or blacklist
anchors: dict[str, AccessLevel] = field(default_factory=dict)

def check(
self, anchor: str
) -> tuple[bool, AccessLevel]: # whitelist or blacklist, has_read_access, level
def check(self, anchor: str) -> AccessLevel:
"""Validate access."""
if self.whitelist:
return self.whitelist, self.anchors.get(anchor, AccessLevel.NO_ACCESS)
else:
return self.whitelist, self.anchors.get(anchor, AccessLevel.WRITE)
return self.anchors.get(anchor, AccessLevel.NO_ACCESS)


@dataclass
Expand Down Expand Up @@ -86,34 +81,25 @@ class Anchor:
# ACCESS CONTROL #
##########################################################################

def whitelist_roots(self, whitelist: bool = True) -> None:
"""Toggle root whitelist/blacklist."""
if whitelist != self.access.roots.whitelist:
self.access.roots.whitelist = whitelist

def allow_root(
self, root_id: UUID, level: AccessLevel | int | str = AccessLevel.READ
) -> None:
"""Allow all access from target root graph to current Architype."""
level = AccessLevel.cast(level)
access = self.access.roots
if access.whitelist:
_root_id = str(root_id)
if level != access.anchors.get(_root_id, AccessLevel.NO_ACCESS):
access.anchors[_root_id] = level
else:
self.disallow_root(root_id, level)

_root_id = str(root_id)
if level != access.anchors.get(_root_id, AccessLevel.NO_ACCESS):
access.anchors[_root_id] = level

def disallow_root(
self, root_id: UUID, level: AccessLevel | int | str = AccessLevel.READ
) -> None:
"""Disallow all access from target root graph to current Architype."""
level = AccessLevel.cast(level)
access = self.access.roots
if access.whitelist:
access.anchors.pop(str(root_id), None)
else:
self.allow_root(root_id, level)

access.anchors.pop(str(root_id), None)

def unrestrict(self, level: AccessLevel | int | str = AccessLevel.READ) -> None:
"""Allow everyone to access current Architype."""
Expand Down Expand Up @@ -166,32 +152,18 @@ def access_level(self, to: Anchor) -> AccessLevel:
if to_root.access.all > to.current_access_level:
to.current_access_level = to_root.access.all

whitelist, level = to_root.access.roots.check(str(jroot.id))
if not whitelist:
if level < AccessLevel.READ:
to.current_access_level = AccessLevel.NO_ACCESS
return to.current_access_level
elif level < to.current_access_level:
level = to.current_access_level
elif (
whitelist
and level > AccessLevel.NO_ACCESS
level = to_root.access.roots.check(str(jroot.id))
if (
level > AccessLevel.NO_ACCESS
and to.current_access_level == AccessLevel.NO_ACCESS
):
to.current_access_level = level

# if target anchor have set allowed roots
# if current root is allowed to target anchor
whitelist, level = to_access.roots.check(str(jroot.id))
if not whitelist:
if level < AccessLevel.READ:
to.current_access_level = AccessLevel.NO_ACCESS
return to.current_access_level
elif level < to.current_access_level:
level = to.current_access_level
elif (
whitelist
and level > AccessLevel.NO_ACCESS
level = to_access.roots.check(str(jroot.id))
if (
level > AccessLevel.NO_ACCESS
and to.current_access_level == AccessLevel.NO_ACCESS
):
to.current_access_level = level
Expand Down

0 comments on commit 9340434

Please sign in to comment.