Browse Source

add simple authorization auth

Guénaël Muller 7 years ago
parent
commit
c55bf2def6
2 changed files with 44 additions and 14 deletions
  1. 28 13
      tracim/lib/utils/auth.py
  2. 16 1
      tracim/views/default/default_controller.py

+ 28 - 13
tracim/lib/utils/auth.py View File

@@ -1,22 +1,37 @@
1
+import typing
1 2
 from pyramid.security import ALL_PERMISSIONS
2 3
 from pyramid.security import Allow
3 4
 from pyramid.security import Authenticated
5
+from tracim.lib.core.user import UserApi
6
+from tracim.models.auth import Group
7
+from tracim.lib.core.workspace import WorkspaceApi
4 8
 
9
+# INFO - G.M - 06-04-2018 - Auth for pyramid
10
+# based on this tutorial : https://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/auth/basic.html  # nopep8
5 11
 
6
-def check_credentials(username, password, request):
7
-    if username == 'admin' and password == 'admin':
8
-        # an empty list is enough to indicate logged-in... watch how this
9
-        # affects the principals returned in the home view if you want to
10
-        # expand ACLs later
11
-        return ['g:admin']
12
-    if username == 'user' and password == 'user':
13
-        return []
12
+
13
+def check_credentials(username, password, request) -> typing.Optional[dict]:
14
+    permissions = None
15
+    app_config = request.registry.settings['CFG']
16
+    uapi = UserApi(None, session=request.dbsession, config=app_config)
17
+    try:
18
+        user = uapi.get_one_by_email(username)
19
+        if user.validate_password(password):
20
+            permissions = []
21
+            for group in user.groups:
22
+                permissions.append(group.group_name)
23
+            # TODO - G.M - 06-04-2018 - Add workspace specific permission ?
24
+    # TODO - G.M - 06-04-2018 - Better catch for exception of bad password, bad
25
+    # user
26
+    except:
27
+        pass
28
+    return permissions
14 29
 
15 30
 
16 31
 class Root:
17
-    # dead simple, give everyone who is logged in any permission
18
-    # (see the home_view for an example permission)
32
+    # root
19 33
     __acl__ = (
20
-        (Allow, 'g:admin', ALL_PERMISSIONS),
21
-        (Allow, Authenticated, 'user'),
22
-    )
34
+        (Allow, Group.TIM_ADMIN_GROUPNAME, ALL_PERMISSIONS),
35
+        (Allow, Group.TIM_MANAGER_GROUPNAME, 'manager'),
36
+        (Allow, Group.TIM_USER_GROUPNAME, 'user'),
37
+    )

+ 16 - 1
tracim/views/default/default_controller.py View File

@@ -45,6 +45,15 @@ class DefaultController(Controller):
45 45
         return {'project': project}
46 46
 
47 47
     @classmethod
48
+    def test_manager_page(cls, request):
49
+        try:
50
+            app_config = request.registry.settings['CFG']
51
+            project = 'manager'
52
+        except Exception as e:
53
+            return Response(e, content_type='text/plain', status=500)
54
+        return {'project': project}
55
+
56
+    @classmethod
48 57
     def test_user_page(cls, request):
49 58
         try:
50 59
             app_config = request.registry.settings['CFG']
@@ -76,7 +85,13 @@ class DefaultController(Controller):
76 85
             renderer='tracim:templates/mytemplate.jinja2',
77 86
             permission='admin',
78 87
         )
79
-
88
+        configurator.add_route('test_manager', '/test_manager')
89
+        configurator.add_view(
90
+            self.test_user_page,
91
+            route_name='test_manager',
92
+            renderer='tracim:templates/mytemplate.jinja2',
93
+            permission='manager',
94
+        )
80 95
         configurator.add_route('test_user', '/test_user')
81 96
         configurator.add_view(
82 97
             self.test_user_page,