Show More
@@ -260,24 +260,25 b' class ApiController(JSONRPCController):' | |||||
260 |
|
260 | |||
261 | user = get_user_or_error(userid) |
|
261 | user = get_user_or_error(userid) | |
262 |
|
262 | |||
263 | #return old attribute if Optional is passed. We don't change parameter |
|
263 | # call function and store only updated arguments | |
264 | # so user doesn't get updated parameters |
|
264 | updates = {} | |
265 | get = lambda attr, name: ( |
|
265 | ||
266 | getattr(user, name) if isinstance(attr, Optional) else attr |
|
266 | def store_update(attr, name): | |
267 | ) |
|
267 | if not isinstance(attr, Optional): | |
|
268 | updates[name] = attr | |||
268 |
|
269 | |||
269 | try: |
|
270 | try: | |
270 |
|
271 | |||
271 | user = UserModel().create_or_update( |
|
272 | store_update(username, 'username') | |
272 | username=get(username, 'username'), |
|
273 | store_update(password, 'password') | |
273 | password=get(password, 'password'), |
|
274 | store_update(email, 'email') | |
274 | email=get(email, 'email'), |
|
275 | store_update(firstname, 'name') | |
275 |
|
|
276 | store_update(lastname, 'lastname') | |
276 | lastname=get(lastname, 'lastname'), |
|
277 | store_update(active, 'active') | |
277 | active=get(active, 'active'), |
|
278 | store_update(admin, 'admin') | |
278 | admin=get(admin, 'admin'), |
|
279 | store_update(ldap_dn, 'ldap_dn') | |
279 | ldap_dn=get(ldap_dn, 'ldap_dn') |
|
280 | ||
280 | ) |
|
281 | user = UserModel().update_user(user, **updates) | |
281 | Session().commit() |
|
282 | Session().commit() | |
282 | return dict( |
|
283 | return dict( | |
283 | msg='updated user ID:%s %s' % (user.user_id, user.username), |
|
284 | msg='updated user ID:%s %s' % (user.user_id, user.username), |
@@ -278,6 +278,28 b' class UserModel(BaseModel):' | |||||
278 | log.error(traceback.format_exc()) |
|
278 | log.error(traceback.format_exc()) | |
279 | raise |
|
279 | raise | |
280 |
|
280 | |||
|
281 | def update_user(self, user, **kwargs): | |||
|
282 | from rhodecode.lib.auth import get_crypt_password | |||
|
283 | try: | |||
|
284 | user = self._get_user(user) | |||
|
285 | if user.username == 'default': | |||
|
286 | raise DefaultUserException( | |||
|
287 | _("You can't Edit this user since it's" | |||
|
288 | " crucial for entire application") | |||
|
289 | ) | |||
|
290 | ||||
|
291 | for k, v in kwargs.items(): | |||
|
292 | if k == 'password' and v: | |||
|
293 | v = get_crypt_password(v) | |||
|
294 | user.api_key = generate_api_key(user.username) | |||
|
295 | ||||
|
296 | setattr(user, k, v) | |||
|
297 | self.sa.add(user) | |||
|
298 | return user | |||
|
299 | except: | |||
|
300 | log.error(traceback.format_exc()) | |||
|
301 | raise | |||
|
302 | ||||
281 | def update_my_account(self, user_id, form_data): |
|
303 | def update_my_account(self, user_id, form_data): | |
282 | from rhodecode.lib.auth import get_crypt_password |
|
304 | from rhodecode.lib.auth import get_crypt_password | |
283 | try: |
|
305 | try: |
@@ -373,7 +373,7 b' class BaseTestApi(object):' | |||||
373 | expected = ret |
|
373 | expected = ret | |
374 | self._compare_ok(id_, expected, given=response.body) |
|
374 | self._compare_ok(id_, expected, given=response.body) | |
375 |
|
375 | |||
376 |
@mock.patch.object(UserModel, ' |
|
376 | @mock.patch.object(UserModel, 'update_user', crash) | |
377 | def test_api_update_user_when_exception_happens(self): |
|
377 | def test_api_update_user_when_exception_happens(self): | |
378 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
378 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | |
379 | ret = jsonify(usr.get_api_data()) |
|
379 | ret = jsonify(usr.get_api_data()) |
General Comments 0
You need to be logged in to leave comments.
Login now