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,3 +171,7 @@ class EmptyCommentContentNotAllowed(EmptyValueNotAllowed):
171 171
 
172 172
 class UserNotActive(TracimException):
173 173
     pass
174
+
175
+
176
+class NoUserSetted(TracimException):
177
+    pass

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

@@ -10,6 +10,8 @@ from tracim import CFG
10 10
 from tracim.models.auth import User
11 11
 from tracim.models.auth import Group
12 12
 from tracim.exceptions import WrongUserPassword
13
+from tracim.exceptions import NoUserSetted
14
+from tracim.exceptions import PasswordDoNotMatch
13 15
 from tracim.exceptions import UserDoesNotExist
14 16
 from tracim.exceptions import AuthenticationFailed
15 17
 from tracim.exceptions import NotificationNotSend
@@ -113,6 +115,72 @@ class UserApi(object):
113 115
             raise AuthenticationFailed('User "{}" authentication failed'.format(email)) from exc  # nopep8
114 116
 
115 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 185
     def update(
118 186
             self,

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

@@ -79,19 +79,16 @@ class UserController(Controller):
79 79
         """
80 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 82
         app_config = request.registry.settings['CFG']
87 83
         uapi = UserApi(
88 84
             current_user=request.current_user,  # User
89 85
             session=request.dbsession,
90 86
             config=app_config,
91 87
         )
92
-        user = uapi.update(
88
+        user = uapi.set_email(
93 89
             request.candidate_user,
94
-            email=hapic_data.body.email,
90
+            hapic_data.body.loggedin_user_password,
91
+            hapic_data.body.email,
95 92
             do_save=True
96 93
         )
97 94
         return uapi.get_user_with_context(user)
@@ -107,26 +104,19 @@ class UserController(Controller):
107 104
         """
108 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 107
         app_config = request.registry.settings['CFG']
117 108
         uapi = UserApi(
118 109
             current_user=request.current_user,  # User
119 110
             session=request.dbsession,
120 111
             config=app_config,
121 112
         )
122
-        uapi.update(
113
+        uapi.set_password(
123 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 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 120
         return
131 121
 
132 122
     @hapic.with_api_doc(tags=[USER_ENDPOINTS_TAG])