|
@ -7,6 +7,7 @@ from typing import Optional, List, Set, Dict |
|
|
class Role(Enum): |
|
|
class Role(Enum): |
|
|
"""User role definitions.""" |
|
|
"""User role definitions.""" |
|
|
|
|
|
|
|
|
|
|
|
OWNER = 'OWNER' |
|
|
ADMIN = 'ADMIN' |
|
|
ADMIN = 'ADMIN' |
|
|
AUDITOR = 'AUDITOR' |
|
|
AUDITOR = 'AUDITOR' |
|
|
MODERATOR = 'MODERATOR' |
|
|
MODERATOR = 'MODERATOR' |
|
@ -14,39 +15,48 @@ class Role(Enum): |
|
|
ANONYMOUS = 'ANONYMOUS' |
|
|
ANONYMOUS = 'ANONYMOUS' |
|
|
NONE = 'NONE' |
|
|
NONE = 'NONE' |
|
|
|
|
|
|
|
|
|
|
|
def __str__(self): |
|
|
|
|
|
return self.value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RoleTree(defaultdict): |
|
|
class RoleTree(defaultdict): |
|
|
"""Simple tree structure to handle hierarchy.""" |
|
|
"""Simple tree structure to handle hierarchy.""" |
|
|
|
|
|
|
|
|
def __call__(self, data: Role) -> 'RoleTree': |
|
|
|
|
|
|
|
|
def __call__(self, data: Role, power: int) -> 'RoleTree': |
|
|
"""Handle direct calls to the tree.""" |
|
|
"""Handle direct calls to the tree.""" |
|
|
return RoleTree(self, data) |
|
|
|
|
|
|
|
|
return RoleTree(self, data, power) |
|
|
|
|
|
|
|
|
# def __hash__(self): |
|
|
|
|
|
|
|
|
parent: Optional['RoleTree'] |
|
|
|
|
|
data: Role |
|
|
|
|
|
power: int |
|
|
|
|
|
roles: Dict[Role, List['RoleTree']] |
|
|
|
|
|
|
|
|
def __init__( |
|
|
def __init__( |
|
|
self, |
|
|
self, |
|
|
parent: Optional['RoleTree'], |
|
|
parent: Optional['RoleTree'], |
|
|
data: Role, |
|
|
data: Role, |
|
|
|
|
|
power: int = None, |
|
|
**kwargs: dict) -> None: |
|
|
**kwargs: dict) -> None: |
|
|
"""Configure a RoleTree.""" |
|
|
"""Configure a RoleTree.""" |
|
|
super().__init__(**kwargs) |
|
|
super().__init__(**kwargs) |
|
|
self.parent: Optional[RoleTree] = parent |
|
|
self.parent: Optional[RoleTree] = parent |
|
|
self.data: Role = data |
|
|
self.data: Role = data |
|
|
|
|
|
self.power: int = power if power is not None else 1 |
|
|
self.default_factory = self # type: ignore |
|
|
self.default_factory = self # type: ignore |
|
|
self.roles: Dict[Role, List[RoleTree]] = {data: [self]} |
|
|
self.roles: Dict[Role, List[RoleTree]] = {data: [self]} |
|
|
|
|
|
|
|
|
def populate( |
|
|
def populate( |
|
|
self, children: Dict[Role, Optional[dict]]) -> List['RoleTree']: |
|
|
self, children: Dict[Role, Optional[dict]]) -> List['RoleTree']: |
|
|
"""Populate a RoleTree from a dictionary of a Role hierarchy.""" |
|
|
"""Populate a RoleTree from a dictionary of a Role hierarchy.""" |
|
|
role_list: List[RoleTree] = [] |
|
|
|
|
|
|
|
|
role_list: List[RoleTree] = [self] |
|
|
for child_role in children.keys(): |
|
|
for child_role in children.keys(): |
|
|
element = children[child_role] |
|
|
element = children[child_role] |
|
|
new_node = self(child_role) |
|
|
|
|
|
|
|
|
new_node = self(child_role, self.power + 1) |
|
|
if isinstance(element, dict) and element: |
|
|
if isinstance(element, dict) and element: |
|
|
role_list += new_node.populate(element) |
|
|
|
|
|
self[child_role] = new_node |
|
|
|
|
|
|
|
|
role_list.extend(new_node.populate(element)) |
|
|
|
|
|
else: |
|
|
role_list.append(new_node) |
|
|
role_list.append(new_node) |
|
|
|
|
|
self[child_role] = new_node |
|
|
for role_tree in role_list: |
|
|
for role_tree in role_list: |
|
|
if role_tree.data not in self.roles.keys(): |
|
|
if role_tree.data not in self.roles.keys(): |
|
|
self.roles[role_tree.data] = [] |
|
|
self.roles[role_tree.data] = [] |
|
@ -94,9 +104,14 @@ class RoleTree(defaultdict): |
|
|
roles.extend(role_tree.get_children_roles()) |
|
|
roles.extend(role_tree.get_children_roles()) |
|
|
return set(roles) |
|
|
return set(roles) |
|
|
|
|
|
|
|
|
|
|
|
def __str__(self): |
|
|
|
|
|
return str(self.data) |
|
|
|
|
|
|
|
|
ROLES = RoleTree(None, Role.ADMIN) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ROLES = RoleTree(None, Role.OWNER, 0) |
|
|
|
|
|
ROLE_LIST = sorted( |
|
|
ROLES.populate({ |
|
|
ROLES.populate({ |
|
|
|
|
|
Role.ADMIN: { |
|
|
Role.MODERATOR: { |
|
|
Role.MODERATOR: { |
|
|
Role.USER: { |
|
|
Role.USER: { |
|
|
Role.ANONYMOUS: None |
|
|
Role.ANONYMOUS: None |
|
@ -105,4 +120,6 @@ ROLES.populate({ |
|
|
Role.AUDITOR: { |
|
|
Role.AUDITOR: { |
|
|
Role.USER: None |
|
|
Role.USER: None |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
}), |
|
|
|
|
|
key=lambda rt: rt.power) |