|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
import typing |
|
3
|
|
|
|
|
4
|
|
|
__author__ = 'damien' |
|
5
|
|
|
|
|
6
|
|
|
from sqlalchemy.orm import Session |
|
7
|
|
|
from tracim.models.auth import User |
|
8
|
|
|
from tracim.models.data import Workspace |
|
9
|
|
|
from tracim.models.data import UserRoleInWorkspace |
|
10
|
|
|
from tracim.models.data import RoleType |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class RoleApi(object): |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
ALL_ROLE_VALUES = UserRoleInWorkspace.get_all_role_values() |
|
16
|
|
|
# Dict containing readable members roles for given role |
|
17
|
|
|
members_read_rights = { |
|
18
|
|
|
UserRoleInWorkspace.NOT_APPLICABLE: [], |
|
19
|
|
|
UserRoleInWorkspace.READER: [ |
|
20
|
|
|
UserRoleInWorkspace.WORKSPACE_MANAGER, |
|
21
|
|
|
], |
|
22
|
|
|
UserRoleInWorkspace.CONTRIBUTOR: [ |
|
23
|
|
|
UserRoleInWorkspace.WORKSPACE_MANAGER, |
|
24
|
|
|
UserRoleInWorkspace.CONTENT_MANAGER, |
|
25
|
|
|
UserRoleInWorkspace.CONTRIBUTOR, |
|
26
|
|
|
], |
|
27
|
|
|
UserRoleInWorkspace.CONTENT_MANAGER: [ |
|
28
|
|
|
UserRoleInWorkspace.WORKSPACE_MANAGER, |
|
29
|
|
|
UserRoleInWorkspace.CONTENT_MANAGER, |
|
30
|
|
|
UserRoleInWorkspace.CONTRIBUTOR, |
|
31
|
|
|
UserRoleInWorkspace.READER, |
|
32
|
|
|
], |
|
33
|
|
|
UserRoleInWorkspace.WORKSPACE_MANAGER: [ |
|
34
|
|
|
UserRoleInWorkspace.WORKSPACE_MANAGER, |
|
35
|
|
|
UserRoleInWorkspace.CONTENT_MANAGER, |
|
36
|
|
|
UserRoleInWorkspace.CONTRIBUTOR, |
|
37
|
|
|
UserRoleInWorkspace.READER, |
|
38
|
|
|
], |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
@classmethod |
|
42
|
|
|
def role_can_read_member_role(cls, reader_role: int, tested_role: int) \ |
|
43
|
|
|
-> bool: |
|
44
|
|
|
""" |
|
45
|
|
|
:param reader_role: role as viewer |
|
46
|
|
|
:param tested_role: role as viwed |
|
47
|
|
|
:return: True if given role can view member role in workspace. |
|
48
|
|
|
""" |
|
49
|
|
|
if reader_role in cls.members_read_rights: |
|
50
|
|
|
return tested_role in cls.members_read_rights[reader_role] |
|
51
|
|
|
return False |
|
52
|
|
|
|
|
53
|
|
|
@classmethod |
|
54
|
|
|
def create_role(cls) -> UserRoleInWorkspace: |
|
|
|
|
|
|
55
|
|
|
role = UserRoleInWorkspace() |
|
56
|
|
|
|
|
57
|
|
|
return role |
|
58
|
|
|
|
|
59
|
|
|
def __init__(self, session: Session, current_user: typing.Optional[User]): |
|
|
|
|
|
|
60
|
|
|
self._session = session |
|
61
|
|
|
self._user = current_user |
|
62
|
|
|
|
|
63
|
|
|
def _get_one_rsc(self, user_id, workspace_id): |
|
64
|
|
|
""" |
|
65
|
|
|
:param user_id: |
|
66
|
|
|
:param workspace_id: |
|
67
|
|
|
:return: a Query object, filtered query but without fetching the object. |
|
68
|
|
|
""" |
|
69
|
|
|
return self._session.query(UserRoleInWorkspace).\ |
|
70
|
|
|
filter(UserRoleInWorkspace.workspace_id == workspace_id).\ |
|
71
|
|
|
filter(UserRoleInWorkspace.user_id == user_id) |
|
72
|
|
|
|
|
73
|
|
|
def get_one(self, user_id, workspace_id): |
|
|
|
|
|
|
74
|
|
|
return self._get_one_rsc(user_id, workspace_id).one() |
|
75
|
|
|
|
|
76
|
|
|
def create_one( |
|
|
|
|
|
|
77
|
|
|
self, |
|
78
|
|
|
user: User, |
|
79
|
|
|
workspace: Workspace, |
|
80
|
|
|
role_level: int, |
|
81
|
|
|
with_notif: bool, |
|
82
|
|
|
flush: bool=True |
|
|
|
|
|
|
83
|
|
|
) -> UserRoleInWorkspace: |
|
84
|
|
|
role = self.create_role() |
|
85
|
|
|
role.user_id = user.user_id |
|
86
|
|
|
role.workspace = workspace |
|
87
|
|
|
role.role = role_level |
|
88
|
|
|
role.do_notify = with_notif |
|
89
|
|
|
if flush: |
|
90
|
|
|
self._session.flush() |
|
91
|
|
|
return role |
|
92
|
|
|
|
|
93
|
|
|
def delete_one(self, user_id, workspace_id, flush=True): |
|
|
|
|
|
|
94
|
|
|
self._get_one_rsc(user_id, workspace_id).delete() |
|
95
|
|
|
if flush: |
|
96
|
|
|
self._session.flush() |
|
97
|
|
|
|
|
98
|
|
|
def _get_all_for_user(self, user_id): |
|
99
|
|
|
return self._session.query(UserRoleInWorkspace)\ |
|
100
|
|
|
.filter(UserRoleInWorkspace.user_id == user_id) |
|
101
|
|
|
|
|
102
|
|
|
def get_all_for_user(self, user_id): |
|
|
|
|
|
|
103
|
|
|
return self._get_all_for_user(user_id).all() |
|
104
|
|
|
|
|
105
|
|
|
def get_all_for_user_order_by_workspace( |
|
|
|
|
|
|
106
|
|
|
self, |
|
107
|
|
|
user_id: int |
|
108
|
|
|
) -> UserRoleInWorkspace: |
|
109
|
|
|
return self._get_all_for_user(user_id)\ |
|
110
|
|
|
.join(UserRoleInWorkspace.workspace).order_by(Workspace.label).all() |
|
111
|
|
|
|
|
112
|
|
|
def get_all_for_workspace(self, workspace_id): |
|
|
|
|
|
|
113
|
|
|
return self._session.query(UserRoleInWorkspace)\ |
|
114
|
|
|
.filter(UserRoleInWorkspace.workspace_id == workspace_id).all() |
|
115
|
|
|
|
|
116
|
|
|
def save(self, role: UserRoleInWorkspace): |
|
|
|
|
|
|
117
|
|
|
self._session.flush() |
|
118
|
|
|
|
|
119
|
|
|
@classmethod |
|
120
|
|
|
def get_roles_for_select_field(cls): |
|
121
|
|
|
""" |
|
122
|
|
|
|
|
123
|
|
|
:return: list of DictLikeClass instances representing available Roles |
|
124
|
|
|
(to be used in select fields) |
|
125
|
|
|
""" |
|
126
|
|
|
result = list() |
|
127
|
|
|
|
|
128
|
|
|
for role_id in UserRoleInWorkspace.get_all_role_values(): |
|
129
|
|
|
role = RoleType(role_id) |
|
130
|
|
|
result.append(role) |
|
131
|
|
|
|
|
132
|
|
|
return result |
|
133
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.