X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=acl.py;h=2b347673af1b14dbb144ca9179d115a5eba642b0;hb=ed8fa2b10b0177b15b7423263bdd390efde2f0c8;hp=91550901e07df0a614027f90a6d15fa803fde877;hpb=a838c154135b2420d9047a101caf24a2c9f593c2;p=python_utils.git diff --git a/acl.py b/acl.py index 9155090..2b34767 100644 --- a/acl.py +++ b/acl.py @@ -7,6 +7,8 @@ import logging import re from typing import Any, Callable, List, Optional, Set, Sequence +from overrides import overrides + # This module is commonly used by others in here and should avoid # taking any unnecessary dependencies back on them. @@ -93,11 +95,13 @@ class SetBasedACL(SimpleACL): self.allow_set = allow_set self.deny_set = deny_set + @overrides def check_allowed(self, x: Any) -> bool: if self.allow_set is None: return False return x in self.allow_set + @overrides def check_denied(self, x: Any) -> bool: if self.deny_set is None: return False @@ -106,7 +110,7 @@ class SetBasedACL(SimpleACL): class AllowListACL(SetBasedACL): """Convenience subclass for a list that only allows known items. - i.e. a 'whitelist' + i.e. a 'allowlist' """ def __init__(self, *, @@ -119,7 +123,20 @@ class AllowListACL(SetBasedACL): class DenyListACL(SetBasedACL): """Convenience subclass for a list that only disallows known items. - i.e. a 'blacklist' + i.e. a 'blocklist' + """ + def __init__(self, + *, + deny_set: Optional[Set[Any]]) -> None: + super().__init__( + deny_set = deny_set, + order_to_check_allow_deny = Order.ALLOW_DENY, + default_answer = True) + + +class BlockListACL(SetBasedACL): + """Convenience subclass for a list that only disallows known items. + i.e. a 'blocklist' """ def __init__(self, *, @@ -145,11 +162,13 @@ class PredicateListBasedACL(SimpleACL): self.allow_predicate_list = allow_predicate_list self.deny_predicate_list = deny_predicate_list + @overrides def check_allowed(self, x: Any) -> bool: if self.allow_predicate_list is None: return False return any(predicate(x) for predicate in self.allow_predicate_list) + @overrides def check_denied(self, x: Any) -> bool: if self.deny_predicate_list is None: return False @@ -229,11 +248,13 @@ class AnyCompoundACL(SimpleACL): ) self.subacls = subacls + @overrides def check_allowed(self, x: Any) -> bool: if self.subacls is None: return False return any(acl(x) for acl in self.subacls) + @overrides def check_denied(self, x: Any) -> bool: if self.subacls is None: return False @@ -253,11 +274,13 @@ class AllCompoundACL(SimpleACL): ) self.subacls = subacls + @overrides def check_allowed(self, x: Any) -> bool: if self.subacls is None: return False return all(acl(x) for acl in self.subacls) + @overrides def check_denied(self, x: Any) -> bool: if self.subacls is None: return False