|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +import logging |
| 4 | +import sys |
| 5 | +from pprint import pprint |
| 6 | + |
| 7 | +import click |
| 8 | +from six import text_type |
| 9 | + |
| 10 | +import ckan.logic as logic |
| 11 | +import ckan.plugins as plugin |
| 12 | +from ckan.cli import error_shout |
| 13 | + |
| 14 | +log = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +@click.group(name=u'user', short_help=u'Manage user commands') |
| 18 | +@click.help_option(u'-h', u'--help') |
| 19 | +def user(): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +@user.command(u'add', short_help=u'Add new user') |
| 24 | +@click.argument(u'username') |
| 25 | +@click.argument(u'args', nargs=-1) |
| 26 | +@click.pass_context |
| 27 | +def add_user(ctx, username, args): |
| 28 | + u'''Add new user if we use ckan sysadmin add |
| 29 | + or ckan user add |
| 30 | + ''' |
| 31 | + # parse args into data_dict |
| 32 | + data_dict = {u'name': username} |
| 33 | + for arg in args: |
| 34 | + try: |
| 35 | + field, value = arg.split(u'=', 1) |
| 36 | + data_dict[field] = value |
| 37 | + except ValueError: |
| 38 | + raise ValueError( |
| 39 | + u'Could not parse arg: %r (expected "<option>=<value>)"' % arg |
| 40 | + ) |
| 41 | + |
| 42 | + # Required |
| 43 | + if u'email' not in data_dict: |
| 44 | + data_dict['email'] = click.prompt(u'Email address ').strip() |
| 45 | + |
| 46 | + if u'password' not in data_dict: |
| 47 | + data_dict['password'] = click.prompt(u'Password ', hide_input=True, |
| 48 | + confirmation_prompt=True) |
| 49 | + |
| 50 | + # Optional |
| 51 | + if u'fullname' in data_dict: |
| 52 | + data_dict['fullname'] = data_dict['fullname'].decode( |
| 53 | + sys.getfilesystemencoding() |
| 54 | + ) |
| 55 | + |
| 56 | + # pprint(u'Creating user: %r' % username) |
| 57 | + |
| 58 | + try: |
| 59 | + import ckan.logic as logic |
| 60 | + import ckan.model as model |
| 61 | + site_user = logic.get_action(u'get_site_user')({ |
| 62 | + u'model': model, |
| 63 | + u'ignore_auth': True}, |
| 64 | + {} |
| 65 | + ) |
| 66 | + context = { |
| 67 | + u'model': model, |
| 68 | + u'session': model.Session, |
| 69 | + u'ignore_auth': True, |
| 70 | + u'user': site_user['name'], |
| 71 | + } |
| 72 | + flask_app = ctx.obj.app.apps[u'flask_app']._wsgi_app |
| 73 | + with flask_app.test_request_context(): |
| 74 | + user_dict = logic.get_action(u'user_create')(context, data_dict) |
| 75 | + click.secho(u"Successfully created user: %s" % user_dict['name'], |
| 76 | + fg=u'green', bold=True) |
| 77 | + except logic.ValidationError as e: |
| 78 | + error_shout(e) |
| 79 | + |
| 80 | + |
| 81 | +def get_user_str(user): |
| 82 | + user_str = u'name=%s' % user.name |
| 83 | + if user.name != user.display_name: |
| 84 | + user_str += u' display=%s' % user.display_name |
| 85 | + return user_str |
| 86 | + |
| 87 | + |
| 88 | +@user.command(u'list', short_help=u'List all users') |
| 89 | +def list_users(): |
| 90 | + import ckan.model as model |
| 91 | + click.secho(u'Users:') |
| 92 | + users = model.Session.query(model.User).filter_by(state=u'active') |
| 93 | + click.secho(u'count = %i' % users.count()) |
| 94 | + for user in users: |
| 95 | + click.secho(get_user_str(user)) |
| 96 | + |
| 97 | + |
| 98 | +@user.command(u'remove', short_help=u'Remove user') |
| 99 | +@click.argument(u'username') |
| 100 | +@click.pass_context |
| 101 | +def remove_user(ctx, username): |
| 102 | + import ckan.model as model |
| 103 | + if not username: |
| 104 | + error_shout(u'Please specify the username to be removed') |
| 105 | + return |
| 106 | + |
| 107 | + flask_app = ctx.obj.app.apps['flask_app']._wsgi_app |
| 108 | + site_user = logic.get_action(u'get_site_user')({u'ignore_auth': True}, {}) |
| 109 | + context = {u'user': site_user[u'name']} |
| 110 | + with flask_app.test_request_context(): |
| 111 | + plugin.toolkit.get_action(u'user_delete')(context, {u'id': username}) |
| 112 | + click.secho(u'Deleted user: %s' % username, fg=u'green', bold=True) |
| 113 | + |
| 114 | + |
| 115 | +@user.command(u'show', short_help=u'Show user') |
| 116 | +@click.argument(u'username') |
| 117 | +def show_user(username): |
| 118 | + import ckan.model as model |
| 119 | + if not username: |
| 120 | + error_shout(u'Please specify the username for the user') |
| 121 | + return |
| 122 | + user = model.User.get(text_type(username)) |
| 123 | + click.secho(u'User: %s' % user) |
| 124 | + |
| 125 | + |
| 126 | +@user.command(u'setpass', short_help=u'Set password for the user') |
| 127 | +@click.argument(u'username') |
| 128 | +def set_password(username): |
| 129 | + import ckan.model as model |
| 130 | + if not username: |
| 131 | + error_shout(u'Need name of the user.') |
| 132 | + return |
| 133 | + user = model.User.get(username) |
| 134 | + if not user: |
| 135 | + error_shout(u"User not found!") |
| 136 | + return |
| 137 | + click.secho(u'Editing user: %r' % user.name, fg=u'yellow') |
| 138 | + |
| 139 | + password = click.prompt(u'Password', hide_input=True, |
| 140 | + confirmation_prompt=True) |
| 141 | + user.password = password |
| 142 | + model.repo.commit_and_remove() |
| 143 | + click.secho(u'Password updated!', fg=u'green', bold=True) |
0 commit comments