Skip to content

Commit

Permalink
Add remove user command
Browse files Browse the repository at this point in the history
  • Loading branch information
tino097 committed Jul 19, 2019
1 parent d21cedd commit 75a4899
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ckan/cli/cli.py
Expand Up @@ -12,6 +12,7 @@
translation,
dataset,
plugin_info,
user,
)

from ckan.config.middleware import make_app
Expand Down Expand Up @@ -46,3 +47,4 @@ def ckan(ctx, config, *args, **kwargs):
ckan.add_command(translation.translation)
ckan.add_command(dataset.dataset)
ckan.add_command(plugin_info.plugin_info)
ckan.add_command(user.user)
25 changes: 23 additions & 2 deletions ckan/cli/user.py
@@ -1,15 +1,20 @@
# encoding: utf-8

import logging
import sys
from pprint import pprint

import click

import ckan.logic as l
import ckan.plugins as p
from ckan.cli import error_shout

log = logging.getLogger(__name__)


@click.group(name=u'user', short_help=u'Manage user commands')
@click.help_option(u'-h', u'--help')
def user():
pass

Expand All @@ -19,8 +24,8 @@ def user():
@click.argument('args', nargs=-1)
@click.pass_context
def add_user(ctx, username, args):
'''Add new user if we use paster sysadmin add
or paster user add
'''Add new user if we use ckan sysadmin add
or ckan user add
'''
# parse args into data_dict
data_dict = {'name': username}
Expand Down Expand Up @@ -87,3 +92,19 @@ def list_users():
click.secho('count = %i' % users.count())
for user in users:
click.secho(get_user_str(user))


@user.command(u'remove', short_help=u'Remove user')
@click.argument('username')
@click.pass_context
def remove_user(ctx, username):
import ckan.model as model
if not username:
error_shout("Please specify the username to be removed")

flask_app = ctx.obj.app.apps['flask_app']._wsgi_app
site_user = l.get_action(u'get_site_user')({u'ignore_auth': True}, {})
context = {u'user': site_user[u'name']}
with flask_app.test_request_context():
p.toolkit.get_action('user_delete')(context, {'id': username})
click.secho('Deleted user: %s' % username, fg=u'green', bold=True)

0 comments on commit 75a4899

Please sign in to comment.