|
@@ -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,
|