Browse Source

refactor set_email, set password into userAPI

Guénaël Muller 6 years ago
parent
commit
765c3b3641
3 changed files with 79 additions and 17 deletions
  1. 4 0
      tracim/exceptions.py
  2. 68 0
      tracim/lib/core/user.py
  3. 7 17
      tracim/views/core_api/user_controller.py

+ 4 - 0
tracim/exceptions.py View File

171
 
171
 
172
 class UserNotActive(TracimException):
172
 class UserNotActive(TracimException):
173
     pass
173
     pass
174
+
175
+
176
+class NoUserSetted(TracimException):
177
+    pass

+ 68 - 0
tracim/lib/core/user.py View File

10
 from tracim.models.auth import User
10
 from tracim.models.auth import User
11
 from tracim.models.auth import Group
11
 from tracim.models.auth import Group
12
 from tracim.exceptions import WrongUserPassword
12
 from tracim.exceptions import WrongUserPassword
13
+from tracim.exceptions import NoUserSetted
14
+from tracim.exceptions import PasswordDoNotMatch
13
 from tracim.exceptions import UserDoesNotExist
15
 from tracim.exceptions import UserDoesNotExist
14
 from tracim.exceptions import AuthenticationFailed
16
 from tracim.exceptions import AuthenticationFailed
15
 from tracim.exceptions import NotificationNotSend
17
 from tracim.exceptions import NotificationNotSend
113
             raise AuthenticationFailed('User "{}" authentication failed'.format(email)) from exc  # nopep8
115
             raise AuthenticationFailed('User "{}" authentication failed'.format(email)) from exc  # nopep8
114
 
116
 
115
     # Actions
117
     # Actions
118
+    def set_password(
119
+            self,
120
+            user: User,
121
+            loggedin_user_password: str,
122
+            new_password: str,
123
+            new_password2: str,
124
+            do_save: bool=True
125
+    ):
126
+        """
127
+        Set User password if loggedin user password is correct
128
+        and both new_password are the same.
129
+        :param user: User who need password changed
130
+        :param loggedin_user_password: cleartext password of logged user (not
131
+        same as user)
132
+        :param new_password: new password for user
133
+        :param new_password2: should be same as new_password
134
+        :param do_save: should we save new user password ?
135
+        :return:
136
+        """
137
+        if not self._user:
138
+            raise NoUserSetted('Current User should be set in UserApi to use this method')  # nopep8
139
+        if not self._user.validate_password(loggedin_user_password):  # nopep8
140
+            raise WrongUserPassword(
141
+                'Wrong password for authenticated user {}'. format(self._user.user_id)  # nopep8
142
+            )
143
+        if new_password != new_password2:
144
+            raise PasswordDoNotMatch('Passwords given are different')
145
+
146
+        self.update(
147
+            user=user,
148
+            password=new_password,
149
+            do_save=do_save,
150
+        )
151
+        if do_save:
152
+            # TODO - G.M - 2018-07-24 - Check why commit is needed here
153
+            transaction.commit()
154
+        return user
155
+
156
+    def set_email(
157
+            self,
158
+            user: User,
159
+            loggedin_user_password: str,
160
+            email: str,
161
+            do_save: bool = True
162
+    ):
163
+        """
164
+        Set email address of user if loggedin user password is correct
165
+        :param user: User who need email changed
166
+        :param loggedin_user_password: cleartext password of logged user (not
167
+        same as user)
168
+        :param email:
169
+        :param do_save:
170
+        :return:
171
+        """
172
+        if not self._user:
173
+            raise NoUserSetted('Current User should be set in UserApi to use this method')  # nopep8
174
+        if not self._user.validate_password(loggedin_user_password):  # nopep8
175
+            raise WrongUserPassword(
176
+                'Wrong password for authenticated user {}'. format(self._user.user_id)  # nopep8
177
+            )
178
+        self.update(
179
+            user=user,
180
+            email=email,
181
+            do_save=do_save,
182
+        )
183
+        return user
116
 
184
 
117
     def update(
185
     def update(
118
             self,
186
             self,

+ 7 - 17
tracim/views/core_api/user_controller.py View File

79
         """
79
         """
80
         Set user Email
80
         Set user Email
81
         """
81
         """
82
-        if not request.current_user.validate_password(hapic_data.body.loggedin_user_password):  # nopep8
83
-            raise WrongUserPassword(
84
-                'Wrong password for authenticated user {}'. format(request.current_user.user_id)  # nopep8
85
-            )
86
         app_config = request.registry.settings['CFG']
82
         app_config = request.registry.settings['CFG']
87
         uapi = UserApi(
83
         uapi = UserApi(
88
             current_user=request.current_user,  # User
84
             current_user=request.current_user,  # User
89
             session=request.dbsession,
85
             session=request.dbsession,
90
             config=app_config,
86
             config=app_config,
91
         )
87
         )
92
-        user = uapi.update(
88
+        user = uapi.set_email(
93
             request.candidate_user,
89
             request.candidate_user,
94
-            email=hapic_data.body.email,
90
+            hapic_data.body.loggedin_user_password,
91
+            hapic_data.body.email,
95
             do_save=True
92
             do_save=True
96
         )
93
         )
97
         return uapi.get_user_with_context(user)
94
         return uapi.get_user_with_context(user)
107
         """
104
         """
108
         Set user password
105
         Set user password
109
         """
106
         """
110
-        if not request.current_user.validate_password(hapic_data.body.loggedin_user_password):  # nopep8
111
-            raise WrongUserPassword(
112
-                'Wrong password for authenticated user {}'. format(request.current_user.user_id)  # nopep8
113
-            )
114
-        if hapic_data.body.new_password != hapic_data.body.new_password2:
115
-            raise PasswordDoNotMatch('Passwords given are different')
116
         app_config = request.registry.settings['CFG']
107
         app_config = request.registry.settings['CFG']
117
         uapi = UserApi(
108
         uapi = UserApi(
118
             current_user=request.current_user,  # User
109
             current_user=request.current_user,  # User
119
             session=request.dbsession,
110
             session=request.dbsession,
120
             config=app_config,
111
             config=app_config,
121
         )
112
         )
122
-        uapi.update(
113
+        uapi.set_password(
123
             request.candidate_user,
114
             request.candidate_user,
124
-            password=hapic_data.body.new_password,
115
+            hapic_data.body.loggedin_user_password,
116
+            hapic_data.body.new_password,
117
+            hapic_data.body.new_password2,
125
             do_save=True
118
             do_save=True
126
         )
119
         )
127
-        uapi.save(request.candidate_user)
128
-        # TODO - G.M - 2018-07-24 - Check why commit is needed here
129
-        transaction.commit()
130
         return
120
         return
131
 
121
 
132
     @hapic.with_api_doc(tags=[USER_ENDPOINTS_TAG])
122
     @hapic.with_api_doc(tags=[USER_ENDPOINTS_TAG])