Bladeren bron

Checks access in workspaces administration page

Adrien Panay 7 jaren geleden
bovenliggende
commit
362f31f6a6

+ 1 - 1
tracim/tracim/controllers/admin/workspace.py Bestand weergeven

@@ -161,7 +161,7 @@ class WorkspaceRestController(TIMRestController, BaseController):
161 161
         user = tmpl_context.current_user
162 162
         workspace_api_controller = WorkspaceApi(user)
163 163
 
164
-        workspaces = workspace_api_controller.get_all()
164
+        workspaces = workspace_api_controller.get_all_manageable_for_user(user)
165 165
 
166 166
         current_user_content = Context(CTX.CURRENT_USER).toDict(user)
167 167
         fake_api = Context(CTX.ADMIN_WORKSPACE).toDict({'current_user': current_user_content})

+ 16 - 0
tracim/tracim/lib/workspace.py Bestand weergeven

@@ -3,6 +3,7 @@ import transaction
3 3
 
4 4
 from sqlalchemy.orm import Query
5 5
 from tg.i18n import ugettext as _
6
+from typing import List
6 7
 
7 8
 from tracim.lib.userworkspace import RoleApi
8 9
 from tracim.model.auth import Group
@@ -101,6 +102,21 @@ class WorkspaceApi(object):
101 102
         workspaces.sort(key=lambda workspace: workspace.label.lower())
102 103
         return workspaces
103 104
 
105
+    def get_all_manageable_for_user(self, user: User) -> List[Workspace]:
106
+        """Get all workspaces the given user has manager rights on."""
107
+        workspaces = []
108
+        if user.profile.id == Group.TIM_ADMIN:
109
+            workspaces = self._base_query().order_by(Workspace.label).all()
110
+        elif user.profile.id == Group.TIM_MANAGER:
111
+            workspaces = self._base_query() \
112
+                .filter(
113
+                    UserRoleInWorkspace.role ==
114
+                    UserRoleInWorkspace.WORKSPACE_MANAGER
115
+                ) \
116
+                .order_by(Workspace.label) \
117
+                .all()
118
+        return workspaces
119
+
104 120
     def disable_notifications(self, user: User, workspace: Workspace):
105 121
         for role in user.roles:
106 122
             if role.workspace==workspace:

+ 32 - 0
tracim/tracim/tests/library/test_workspace.py Bestand weergeven

@@ -2,12 +2,14 @@
2 2
 from nose.tools import eq_
3 3
 
4 4
 from tracim.lib.content import ContentApi
5
+from tracim.lib.group import GroupApi
5 6
 from tracim.lib.user import UserApi
6 7
 from tracim.lib.userworkspace import RoleApi
7 8
 from tracim.lib.workspace import WorkspaceApi
8 9
 from tracim.model import Content
9 10
 from tracim.model import DBSession
10 11
 from tracim.model import User
12
+from tracim.model.auth import Group
11 13
 from tracim.model.data import UserRoleInWorkspace
12 14
 from tracim.model.data import Workspace
13 15
 from tracim.tests import BaseTestThread
@@ -41,3 +43,33 @@ class TestThread(BaseTestThread, TestStandard):
41 43
         eq_([r, ], wapi.get_notifiable_roles(workspace=w))
42 44
         u.is_active = False
43 45
         eq_([], wapi.get_notifiable_roles(workspace=w))
46
+
47
+    def test_unit__get_all_admin_for_user(self):
48
+        admin = DBSession.query(User) \
49
+            .filter(User.email == 'admin@admin.admin').one()
50
+        uapi = UserApi(admin)
51
+        # Checks a case without workspaces.
52
+        wapi = WorkspaceApi(current_user=admin)
53
+        eq_([], wapi.get_all_manageable_for_user(user=admin))
54
+        # Checks an admin gets all workspaces.
55
+        w4 = wapi.create_workspace(label='w4')
56
+        w3 = wapi.create_workspace(label='w3')
57
+        w2 = wapi.create_workspace(label='w2')
58
+        w1 = wapi.create_workspace(label='w1')
59
+        eq_([w1, w2, w3, w4], wapi.get_all_manageable_for_user(user=admin))
60
+        # Checks a regular user gets none workspace.
61
+        gapi = GroupApi(None)
62
+        u = uapi.create_user('u.s@e.r', [gapi.get_one(Group.TIM_USER)], True)
63
+        wapi = WorkspaceApi(current_user=u)
64
+        rapi = RoleApi(current_user=u)
65
+        off = 'off'
66
+        rapi.create_one(u, w4, UserRoleInWorkspace.READER, off)
67
+        rapi.create_one(u, w3, UserRoleInWorkspace.CONTRIBUTOR, off)
68
+        rapi.create_one(u, w2, UserRoleInWorkspace.CONTENT_MANAGER, off)
69
+        rapi.create_one(u, w1, UserRoleInWorkspace.WORKSPACE_MANAGER, off)
70
+        eq_([], wapi.get_all_manageable_for_user(user=u))
71
+        # Checks a manager gets only its own workspaces.
72
+        u.groups.append(gapi.get_one(Group.TIM_MANAGER))
73
+        rapi.delete_one(u.user_id, w2.workspace_id)
74
+        rapi.create_one(u, w2, UserRoleInWorkspace.WORKSPACE_MANAGER, off)
75
+        eq_([w1, w2], wapi.get_all_manageable_for_user(user=u))