Procházet zdrojové kódy

fix NoAuthenticated exception renaming and other simple refactoring

Guénaël Muller před 6 roky
rodič
revize
4c96144b7a

+ 1 - 1
tracim/exceptions.py Zobrazit soubor

@@ -61,7 +61,7 @@ class SameValueError(ValueError):
61 61
     pass
62 62
 
63 63
 
64
-class NotAuthentificated(TracimException):
64
+class NotAuthenticated(TracimException):
65 65
     pass
66 66
 
67 67
 

+ 3 - 1
tracim/lib/core/userworkspace.py Zobrazit soubor

@@ -49,6 +49,7 @@ class RoleApi(object):
49 49
         """
50 50
         Return WorkspaceInContext object from Workspace
51 51
         """
52
+        assert self._config
52 53
         workspace = UserRoleWorkspaceInContext(
53 54
             user_role=user_role,
54 55
             dbsession=self._session,
@@ -138,7 +139,8 @@ class RoleApi(object):
138 139
         workspace:Workspace
139 140
     ) -> typing.List[UserRoleInWorkspace]:
140 141
         return self._session.query(UserRoleInWorkspace)\
141
-            .filter(UserRoleInWorkspace.workspace_id == workspace.workspace_id).all()  # nopep8
142
+            .filter(UserRoleInWorkspace.workspace_id==workspace.workspace_id)\
143
+            .all()
142 144
 
143 145
     def save(self, role: UserRoleInWorkspace) -> None:
144 146
         self._session.flush()

+ 1 - 1
tracim/lib/utils/authentification.py Zobrazit soubor

@@ -51,6 +51,6 @@ def _get_basic_auth_unsafe_user(
51 51
         if not login:
52 52
             return None
53 53
         user = uapi.get_one_by_email(login)
54
-    except (NoResultFound, UserDoesNotExist):
54
+    except UserDoesNotExist:
55 55
         return None
56 56
     return user

+ 3 - 3
tracim/lib/utils/authorization.py Zobrazit soubor

@@ -44,7 +44,7 @@ class AcceptAllAuthorizationPolicy(object):
44 44
 # We prefer to use decorators
45 45
 
46 46
 
47
-def require_same_user_or_profile(group):
47
+def require_same_user_or_profile(group: int):
48 48
     """
49 49
     Decorator for view to restrict access of tracim request if candidate user
50 50
     is distinct from authenticated user and not with high enough profile.
@@ -64,7 +64,7 @@ def require_same_user_or_profile(group):
64 64
     return decorator
65 65
 
66 66
 
67
-def require_profile(group):
67
+def require_profile(group: int):
68 68
     """
69 69
     Decorator for view to restrict access of tracim request if profile is
70 70
     not high enough
@@ -82,7 +82,7 @@ def require_profile(group):
82 82
     return decorator
83 83
 
84 84
 
85
-def require_workspace_role(minimal_required_role):
85
+def require_workspace_role(minimal_required_role: int):
86 86
     """
87 87
     Decorator for view to restrict access of tracim request if role
88 88
     is not high enough

+ 6 - 3
tracim/lib/utils/request.py Zobrazit soubor

@@ -6,7 +6,7 @@ from pyramid.request import Request
6 6
 from sqlalchemy.orm.exc import NoResultFound
7 7
 
8 8
 
9
-from tracim.exceptions import NotAuthentificated
9
+from tracim.exceptions import NotAuthenticated
10 10
 from tracim.exceptions import UserNotFoundInTracimRequest
11 11
 from tracim.exceptions import UserDoesNotExist
12 12
 from tracim.exceptions import WorkspaceNotFound
@@ -40,11 +40,14 @@ class TracimRequest(Request):
40 40
         )
41 41
         # Current workspace, found by request headers or content
42 42
         self._current_workspace = None  # type: Workspace
43
+
43 44
         # Authenticated user
44 45
         self._current_user = None  # type: User
46
+
45 47
         # User found from request headers, content, distinct from authenticated
46 48
         # user
47 49
         self._user_candidate = None  # type: User
50
+
48 51
         # INFO - G.M - 18-05-2018 - Close db at the end of the request
49 52
         self.add_finished_callback(self._cleanup)
50 53
 
@@ -168,7 +171,7 @@ def get_auth_safe_user(
168 171
             raise UserNotFoundInTracimRequest('You request a current user but the context not permit to found one')  # nopep8
169 172
         user = uapi.get_one_by_email(login)
170 173
     except (UserDoesNotExist, UserNotFoundInTracimRequest) as exc:
171
-        raise NotAuthentificated('User {} not found'.format(login)) from exc
174
+        raise NotAuthenticated('User {} not found'.format(login)) from exc
172 175
     return user
173 176
 
174 177
 
@@ -187,7 +190,7 @@ def get_workspace(
187 190
         if 'workspace_id' in request.matchdict:
188 191
             workspace_id = request.matchdict['workspace_id']
189 192
         if not workspace_id:
190
-            raise WorkspaceNotFound('No workspace_id param')
193
+            raise WorkspaceNotFound('No workspace_id property found in request')
191 194
         wapi = WorkspaceApi(
192 195
             current_user=user,
193 196
             session=request.dbsession,

+ 2 - 2
tracim/models/applications.py Zobrazit soubor

@@ -47,7 +47,7 @@ thread = Application(
47 47
 
48 48
 )
49 49
 
50
-file = Application(
50
+_file = Application(
51 51
     label='Files',
52 52
     slug='contents/files',
53 53
     icon='paperclip',
@@ -81,7 +81,7 @@ htmlpage = Application(
81 81
 applications = [
82 82
     htmlpage,
83 83
     markdownpluspage,
84
-    file,
84
+    _file,
85 85
     thread,
86 86
     calendar,
87 87
 ]

+ 2 - 2
tracim/views/core_api/session_controller.py Zobrazit soubor

@@ -13,7 +13,7 @@ from tracim.views.core_api.schemas import UserSchema
13 13
 from tracim.views.core_api.schemas import NoContentSchema
14 14
 from tracim.views.core_api.schemas import LoginOutputHeaders
15 15
 from tracim.views.core_api.schemas import BasicAuthSchema
16
-from tracim.exceptions import NotAuthentificated
16
+from tracim.exceptions import NotAuthenticated
17 17
 from tracim.exceptions import AuthenticationFailed
18 18
 
19 19
 
@@ -52,7 +52,7 @@ class SessionController(Controller):
52 52
         return
53 53
 
54 54
     @hapic.with_api_doc()
55
-    @hapic.handle_exception(NotAuthentificated, HTTPStatus.UNAUTHORIZED)
55
+    @hapic.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
56 56
     @hapic.output_body(UserSchema(),)
57 57
     def whoami(self, context, request: TracimRequest, hapic_data=None):
58 58
         """

+ 2 - 2
tracim/views/core_api/system_controller.py Zobrazit soubor

@@ -1,7 +1,7 @@
1 1
 # coding=utf-8
2 2
 from pyramid.config import Configurator
3 3
 
4
-from tracim.exceptions import NotAuthentificated, InsufficientUserProfile
4
+from tracim.exceptions import NotAuthenticated, InsufficientUserProfile
5 5
 from tracim.lib.utils.authorization import require_profile
6 6
 from tracim.models import Group
7 7
 from tracim.models.applications import applications
@@ -20,7 +20,7 @@ from tracim.views.core_api.schemas import ApplicationSchema
20 20
 class SystemController(Controller):
21 21
 
22 22
     @hapic.with_api_doc()
23
-    @hapic.handle_exception(NotAuthentificated, HTTPStatus.UNAUTHORIZED)
23
+    @hapic.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
24 24
     @hapic.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
25 25
     @require_profile(Group.TIM_USER)
26 26
     @hapic.output_body(ApplicationSchema(many=True),)

+ 2 - 2
tracim/views/core_api/user_controller.py Zobrazit soubor

@@ -12,7 +12,7 @@ except ImportError:
12 12
 
13 13
 from tracim import hapic, TracimRequest
14 14
 
15
-from tracim.exceptions import NotAuthentificated
15
+from tracim.exceptions import NotAuthenticated
16 16
 from tracim.exceptions import InsufficientUserProfile
17 17
 from tracim.exceptions import UserDoesNotExist
18 18
 from tracim.lib.core.workspace import WorkspaceApi
@@ -24,7 +24,7 @@ from tracim.views.core_api.schemas import WorkspaceDigestSchema
24 24
 class UserController(Controller):
25 25
 
26 26
     @hapic.with_api_doc()
27
-    @hapic.handle_exception(NotAuthentificated, HTTPStatus.UNAUTHORIZED)
27
+    @hapic.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
28 28
     @hapic.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
29 29
     @hapic.handle_exception(UserDoesNotExist, HTTPStatus.NOT_FOUND)
30 30
     @require_same_user_or_profile(Group.TIM_ADMIN)

+ 3 - 3
tracim/views/core_api/workspace_controller.py Zobrazit soubor

@@ -15,7 +15,7 @@ except ImportError:
15 15
     from http import client as HTTPStatus
16 16
 
17 17
 from tracim import hapic, TracimRequest
18
-from tracim.exceptions import NotAuthentificated
18
+from tracim.exceptions import NotAuthenticated
19 19
 from tracim.exceptions import InsufficientUserProfile
20 20
 from tracim.exceptions import WorkspaceNotFound
21 21
 from tracim.lib.core.user import UserApi
@@ -29,7 +29,7 @@ from tracim.views.core_api.schemas import WorkspaceMemberSchema
29 29
 class WorkspaceController(Controller):
30 30
 
31 31
     @hapic.with_api_doc()
32
-    @hapic.handle_exception(NotAuthentificated, HTTPStatus.UNAUTHORIZED)
32
+    @hapic.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
33 33
     @hapic.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
34 34
     @hapic.handle_exception(WorkspaceNotFound, HTTPStatus.FORBIDDEN)
35 35
     @require_workspace_role(UserRoleInWorkspace.READER)
@@ -49,7 +49,7 @@ class WorkspaceController(Controller):
49 49
         return wapi.get_workspace_with_context(request.current_workspace)
50 50
 
51 51
     @hapic.with_api_doc()
52
-    @hapic.handle_exception(NotAuthentificated, HTTPStatus.UNAUTHORIZED)
52
+    @hapic.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
53 53
     @hapic.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
54 54
     @hapic.handle_exception(WorkspaceNotFound, HTTPStatus.FORBIDDEN)
55 55
     @require_workspace_role(UserRoleInWorkspace.READER)