Skip to content

Commit

Permalink
A controller action to cycle the API key
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelbabu committed Jan 1, 2014
1 parent 46752e5 commit 0952e46
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions ckan/config/routing.py
Expand Up @@ -349,6 +349,7 @@ def make_map():
m.connect('/user/edit', action='edit')
# Note: openid users have slashes in their ids, so need the wildcard
# in the route.
m.connect('user_cycle_apikey', '/user/cycle_key/{id}', action='cycle_apikey')
m.connect('/user/activity/{id}/{offset}', action='activity')
m.connect('user_activity_stream', '/user/activity/{id}',
action='activity', ckan_icon='time')
Expand Down
35 changes: 35 additions & 0 deletions ckan/controllers/user.py
Expand Up @@ -195,6 +195,41 @@ def delete(self, id):
msg = _('Unauthorized to delete user with id "{user_id}".')
abort(401, msg.format(user_id=id))

def cycle_apikey(self, id):
'''Cycle the API key of a user'''
context = {'model': model,
'session': model.Session,
'user': c.user,
'auth_user_obj': c.userobj,
}
if id is None:
if c.userobj:
id = c.userobj.id
else:
abort(400, _('No user specified'))
data_dict = {'id': id}

try:
check_access('user_update', context, data_dict)
except NotAuthorized:
abort(401, _('Unauthorized to edit a user.'))

try:
old_data = get_action('user_show')(context, data_dict)
old_data['apikey'] = model.types.make_uuid()
context['schema'] = schema.default_cycle_apikey_user_schema()
data_dict = old_data
get_action('user_update')(context, data_dict)

except NotAuthorized:
abort(401, _('Unauthorized to edit user %s') % '')
except NotFound:
abort(404, _('User not found'))

h.flash_success(_('Profile updated'))
h.redirect_to(controller='user', action='read', id=data_dict['name'])


def _save_new(self, context):
try:
data_dict = logic.clean_dict(unflatten(
Expand Down
6 changes: 6 additions & 0 deletions ckan/logic/schema.py
Expand Up @@ -438,6 +438,12 @@ def default_update_user_schema():

return schema

def default_cycle_apikey_user_schema():
schema = default_update_user_schema()

schema['apikey'] = [not_empty, unicode]
return schema

def default_user_invite_schema():
schema = {
'email': [not_empty, unicode],
Expand Down

0 comments on commit 0952e46

Please sign in to comment.