1
|
|
|
""" |
2
|
|
|
byceps.util.authorization |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2021 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from __future__ import annotations |
10
|
1 |
|
from importlib import import_module |
11
|
1 |
|
import pkgutil |
12
|
|
|
|
13
|
1 |
|
from flask import g |
14
|
|
|
from flask_babel import LazyString |
15
|
1 |
|
|
16
|
1 |
|
from ..services.authorization import service as authorization_service |
17
|
1 |
|
from ..services.authorization.transfer.models import PermissionID |
18
|
|
|
from ..typing import UserID |
19
|
|
|
|
20
|
1 |
|
|
21
|
|
|
def load_permissions() -> None: |
22
|
1 |
|
"""Load permissions from modules in the permissions package.""" |
23
|
|
|
pkg_name = f'byceps.permissions' |
24
|
1 |
|
pkg_module = import_module(pkg_name) |
25
|
1 |
|
mod_infos = pkgutil.iter_modules(pkg_module.__path__) |
26
|
1 |
|
mod_names = {mod_info.name for mod_info in mod_infos} |
27
|
|
|
for mod_name in mod_names: |
28
|
1 |
|
import_module(f'{pkg_name}.{mod_name}') |
29
|
|
|
|
30
|
1 |
|
|
31
|
|
|
def register_permissions( |
32
|
|
|
group: str, names_and_labels: list[tuple[str, LazyString]] |
33
|
1 |
|
) -> None: |
34
|
|
|
"""Register a permission.""" |
35
|
1 |
|
for name, label in names_and_labels: |
36
|
1 |
|
permission_id = f'{group}.{name}' |
37
|
|
|
permission_registry.register_permission(permission_id, label) |
38
|
1 |
|
|
39
|
|
|
|
40
|
|
|
def get_permissions_for_user(user_id: UserID) -> frozenset[str]: |
41
|
1 |
|
"""Return the permissions this user has been granted.""" |
42
|
|
|
permission_ids = authorization_service.get_permission_ids_for_user(user_id) |
43
|
1 |
|
|
44
|
1 |
|
return permission_registry.get_registered_permission_ids(permission_ids) |
45
|
|
|
|
46
|
|
|
|
47
|
1 |
|
class PermissionRegistry: |
48
|
|
|
"""A collection of valid permissions.""" |
49
|
1 |
|
|
50
|
1 |
|
def __init__(self) -> None: |
51
|
|
|
self.permissions: dict[str, LazyString] = {} |
52
|
1 |
|
|
53
|
|
|
def register_permission( |
54
|
1 |
|
self, permission_id: str, label: LazyString |
55
|
|
|
) -> None: |
56
|
1 |
|
"""Add permission to the registry.""" |
57
|
|
|
self.permissions[permission_id] = label |
58
|
1 |
|
|
59
|
|
|
def get_registered_permission_ids( |
60
|
1 |
|
self, permission_ids: set[PermissionID] |
61
|
|
|
) -> frozenset[str]: |
62
|
|
|
"""Return the permission IDs that are registered. |
63
|
|
|
|
64
|
1 |
|
If a given permission ID is not registered, it is silently |
65
|
|
|
ignored. |
66
|
1 |
|
""" |
67
|
1 |
|
return frozenset(p for p in self.permissions if p in permission_ids) |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
permission_registry = PermissionRegistry() |
71
|
1 |
|
|
72
|
|
|
|
73
|
1 |
|
def has_current_user_permission(permission: str) -> bool: |
74
|
1 |
|
"""Return `True` if the current user has this permission.""" |
75
|
1 |
|
return permission in g.user.permissions |
76
|
1 |
|
|
77
|
|
|
|
78
|
|
|
def has_current_user_any_permission(*permissions: str) -> bool: |
79
|
|
|
"""Return `True` if the current user has any of these permissions.""" |
80
|
|
|
return any(map(has_current_user_permission, permissions)) |
81
|
|
|
|