Test my validators

This commit is contained in:
Marek Wolan
2023-08-03 16:26:33 +01:00
parent 94617c57a4
commit 2a680c1e48
6 changed files with 235 additions and 55 deletions

View File

@@ -1,3 +0,0 @@
from primaite.simulator.domain.account import Account, AccountGroup, AccountType
__all__ = ["Account", "AccountGroup", "AccountType"]

View File

@@ -1,6 +1,6 @@
"""User account simulation."""
from enum import Enum
from typing import Callable, Dict, List, TypeAlias
from typing import Any, Callable, Dict, List
from primaite import getLogger
from primaite.simulator.core import SimComponent
@@ -8,9 +8,6 @@ from primaite.simulator.core import SimComponent
_LOGGER = getLogger(__name__)
__temp_node = TypeAlias() # placeholder while nodes don't exist
class AccountType(Enum):
"""Whether the account is intended for a user to log in or for a service to use."""
@@ -20,19 +17,6 @@ class AccountType(Enum):
"User accounts are used to allow agents to log in and perform actions"
class AccountGroup(Enum):
"""Permissions are set at group-level and accounts can belong to these groups."""
local_user = 1
"For performing basic actions on a node"
domain_user = 2
"For performing basic actions to the domain"
local_admin = 3
"For full access to actions on a node"
domain_admin = 4
"For full access"
class AccountStatus(Enum):
"""Whether the account is active."""

View File

@@ -1,29 +1,71 @@
from typing import Dict, Final, List, TypeAlias
from enum import Enum
from typing import Any, Dict, Final, List
from primaite.simulator.core import ActionPermissionValidator, SimComponent
from primaite.simulator.domain.account import Account, AccountType
from primaite.simulator.core import SimComponent
from primaite.simulator.domain import Account, AccountGroup, AccountType
# placeholder while these objects don't yet exist
__temp_node = TypeAlias()
__temp_application = TypeAlias()
__temp_folder = TypeAlias()
__temp_file = TypeAlias()
class temp_node:
pass
class temp_application:
pass
class temp_folder:
pass
class temp_file:
pass
class AccountGroup(Enum):
"""Permissions are set at group-level and accounts can belong to these groups."""
local_user = 1
"For performing basic actions on a node"
domain_user = 2
"For performing basic actions to the domain"
local_admin = 3
"For full access to actions on a node"
domain_admin = 4
"For full access"
class GroupMembershipValidator(ActionPermissionValidator):
"""Permit actions based on group membership."""
def __init__(self, allowed_groups: List[AccountGroup]) -> None:
"""TODO."""
self.allowed_groups = allowed_groups
def __call__(self, request: List[str], context: Dict) -> bool:
"""Permit the action if the request comes from an account which belongs to the right group."""
# if context request source is part of any groups mentioned in self.allow_groups, return true, otherwise false
requestor_groups: List[str] = context["request_source"]["groups"]
for allowed_group in self.allowed_groups:
if allowed_group.name in requestor_groups:
return True
return False
class DomainController(SimComponent):
"""Main object for controlling the domain."""
# owned objects
accounts: List(Account) = []
accounts: List[Account] = []
groups: Final[List[AccountGroup]] = list(AccountGroup)
group_membership: Dict[AccountGroup, List[Account]]
# references to non-owned objects
nodes: List(__temp_node) = []
applications: List(__temp_application) = []
folders: List(__temp_folder) = []
files: List(__temp_file) = []
nodes: List[temp_node] = []
applications: List[temp_application] = []
folders: List[temp_folder] = []
files: List[temp_file] = []
def _register_account(self, account: Account) -> None:
"""TODO."""