Skip to content

Commit

Permalink
[#2647] user_show include_password_hash parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Sep 16, 2015
1 parent 70751c8 commit cd53881
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
8 changes: 5 additions & 3 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -556,15 +556,15 @@ def user_list_dictize(obj_list, context,
def member_dictize(member, context):
return d.table_dictize(member, context)

def user_dictize(user, context):
def user_dictize(user, context, include_password_hash=False):

if context.get('with_capacity'):
user, capacity = user
result_dict = d.table_dictize(user, context, capacity=capacity)
else:
result_dict = d.table_dictize(user, context)

del result_dict['password']
password_hash = result_dict.pop('password')
del result_dict['reset_key']

result_dict['display_name'] = user.display_name
Expand All @@ -590,11 +590,13 @@ def user_dictize(user, context):
result_dict['apikey'] = apikey
result_dict['email'] = email

## this should not really really be needed but tests need it
if authz.is_sysadmin(requester):
result_dict['apikey'] = apikey
result_dict['email'] = email

if include_password_hash:
result_dict['password_hash'] = password_hash

model = context['model']
session = model.Session

Expand Down
22 changes: 15 additions & 7 deletions ckan/logic/action/get.py
Expand Up @@ -1472,8 +1472,11 @@ def user_show(context, data_dict):
(optional, default:``False``, limit:50)
:type include_datasets: boolean
:param include_num_followers: Include the number of followers the user has
(optional, default:``False``)
(optional, default:``False``)
:type include_num_followers: boolean
:param include_password_hash: Include the stored password hash
(sysadmin only, optional, default:``False``)
:type include_password_hash: boolean
:returns: the details of the user. Includes email_hash, number_of_edits and
number_created_packages (which excludes draft or private datasets
Expand Down Expand Up @@ -1501,24 +1504,29 @@ def user_show(context, data_dict):

# include private and draft datasets?
requester = context.get('user')
sysadmin = False
if requester:
sysadmin = authz.is_sysadmin(requester)
requester_looking_at_own_account = requester == user_obj.name
include_private_and_draft_datasets = \
authz.is_sysadmin(requester) or \
requester_looking_at_own_account
include_private_and_draft_datasets = (
sysadmin or requester_looking_at_own_account)
else:
include_private_and_draft_datasets = False
context['count_private_and_draft_datasets'] = \
include_private_and_draft_datasets

user_dict = model_dictize.user_dictize(user_obj, context)
include_password_hash = sysadmin and asbool(
data_dict.get('include_password_hash', False))

user_dict = model_dictize.user_dictize(
user_obj, context, include_password_hash)

if context.get('return_minimal'):
log.warning('Use of the "return_minimal" in user_show is '
'deprecated.')
return user_dict

if data_dict.get('include_datasets', False):
if asbool(data_dict.get('include_datasets', False)):
user_dict['datasets'] = []

fq = "+creator_user_id:{0}".format(user_dict['id'])
Expand All @@ -1536,7 +1544,7 @@ def user_show(context, data_dict):
data_dict=search_dict) \
.get('results')

if data_dict.get('include_num_followers', False):
if asbool(data_dict.get('include_num_followers', False)):
user_dict['num_followers'] = logic.get_action('user_follower_count')(
{'model': model, 'session': model.Session},
{'id': user_dict['id']})
Expand Down
1 change: 1 addition & 0 deletions ckan/logic/schema.py
Expand Up @@ -464,6 +464,7 @@ def default_update_user_schema():

schema['name'] = [ignore_missing, name_validator, user_name_validator, unicode]
schema['password'] = [user_password_validator,ignore_missing, unicode]
schema['password_hash'] = [ignore_missing, unicode]

return schema

Expand Down

0 comments on commit cd53881

Please sign in to comment.