Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36c8342

Browse files
committed
[#6956] Prevent non-sysadmin users to change their own state
1 parent 5734d89 commit 36c8342

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

changes/6956.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Non-sysadmin users are no longer able to change their own state

ckan/logic/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def default_user_schema(
404404
'reset_key': [ignore],
405405
'activity_streams_email_notifications': [ignore_missing,
406406
boolean_validator],
407-
'state': [ignore_missing],
407+
'state': [ignore_missing, ignore_not_sysadmin],
408408
'image_url': [ignore_missing, unicode_safe],
409409
'image_display_url': [ignore_missing, unicode_safe],
410410
'plugin_extras': [ignore_missing, json_object, ignore_not_sysadmin],

ckan/tests/logic/action/test_update.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,29 @@ def test_resource_reorder(self):
427427
"http://a.html",
428428
]
429429

430+
def test_normal_user_can_not_change_their_state(self):
431+
432+
user = factories.User(state='pending')
433+
434+
user['state'] = 'active'
435+
436+
updated_user = helpers.call_action("user_update", **user)
437+
438+
updated_user['state'] == 'pending'
439+
440+
def test_sysadmin_user_can_change_a_user_state(self):
441+
442+
user = factories.User(state='pending')
443+
sysadmin = factories.Sysadmin()
444+
445+
user['state'] = 'active'
446+
447+
context = {'user': sysadmin['name']}
448+
449+
updated_user = helpers.call_action("user_update", context=context, **user)
450+
451+
updated_user['state'] == 'active'
452+
430453
def test_update_dataset_cant_change_type(self):
431454
user = factories.User()
432455
dataset = factories.Dataset(

ckan/views/user.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,17 @@ def post(self, id):
732732
user_dict[u'name'] = username
733733
user_dict[u'reset_key'] = g.reset_key
734734
user_dict[u'state'] = model.State.ACTIVE
735-
logic.get_action(u'user_update')(context, user_dict)
736-
mailer.create_reset_key(context[u'user_obj'])
735+
updated_user = logic.get_action("user_update")(context, user_dict)
736+
# Users can not change their own state, so we need another edit
737+
if (updated_user["state"] == model.State.PENDING):
738+
patch_context = {
739+
'user': logic.get_action("get_site_user")(
740+
{"ignore_auth": True}, {})["name"]
741+
}
742+
logic.get_action("user_patch")(
743+
patch_context,
744+
{"id": user_dict['id'], "state": model.State.ACTIVE}
745+
)
737746

738747
h.flash_success(_(u'Your password has been reset.'))
739748
return h.redirect_to(u'home.index')

0 commit comments

Comments
 (0)