This commit is contained in:
Matt Wright
2012-06-12 12:28:59 -04:00
parent edf41096d7
commit 0ddbcdca06
6 changed files with 187 additions and 163 deletions
+21 -17
View File
@@ -11,9 +11,11 @@
import json
import re
from flask.ext.script import Command, Option
from flask.ext.security import (UserCreationError, UserNotFoundError,
RoleNotFoundError, user_datastore)
from flask.ext.security import user_datastore
def pprint(obj):
print json.dumps(obj, sort_keys=True, indent=4)
@@ -21,7 +23,7 @@ def pprint(obj):
class CreateUserCommand(Command):
"""Create a user"""
option_list = (
Option('-u', '--username', dest='username', default=None),
Option('-e', '--email', dest='email', default=None),
@@ -33,14 +35,14 @@ class CreateUserCommand(Command):
def run(self, **kwargs):
# sanitize active input
ai = re.sub(r'\s', '', str(kwargs['active']))
kwargs['active'] = ai.lower() in ['', 'y','yes', '1', 'active']
kwargs['active'] = ai.lower() in ['', 'y', 'yes', '1', 'active']
# sanitize role input a bit
ri = re.sub(r'\s', '', kwargs['roles'])
kwargs['roles'] = [] if ri == '' else ri.split(',')
user_datastore.create_user(**kwargs)
print 'User created successfully.'
kwargs['password'] = '****'
pprint(kwargs)
@@ -55,11 +57,11 @@ class CreateRoleCommand(Command):
)
def run(self, **kwargs):
role = user_datastore.create_role(**kwargs)
user_datastore.create_role(**kwargs)
print 'Role "%(name)s" created successfully.' % kwargs
class _RoleCommand(Command):
class _RoleCommand(Command):
option_list = (
Option('-u', '--user', dest='user_identifier'),
Option('-r', '--role', dest='role_name'),
@@ -68,7 +70,7 @@ class _RoleCommand(Command):
class AddRoleCommand(_RoleCommand):
"""Add a role to a user"""
def run(self, user_identifier, role_name):
user_datastore.add_role_to_user(user_identifier, role_name)
print "Role '%s' added to user '%s' successfully" % (role_name, user_identifier)
@@ -76,27 +78,29 @@ class AddRoleCommand(_RoleCommand):
class RemoveRoleCommand(_RoleCommand):
"""Add a role to a user"""
def run(self, user_identifier, role_name):
user_datastore.remove_role_from_user(user_identifier, role_name)
print "Role '%s' removed from user '%s' successfully" % (role_name, user_identifier)
class _ToggleActiveCommand(Command):
option_list = (
Option('-u', '--user', dest='user_identifier'),
)
class DeactivateUserCommand(_ToggleActiveCommand):
"""Deactive a user"""
def run(self, user_identifier):
user_datastore.deactivate_user(user_identifier)
print "User '%s' has been deactivated" % user_identifier
class ActivateUserCommand(_ToggleActiveCommand):
"""Deactive a user"""
def run(self, user_identifier):
user_datastore.activate_user(user_identifier)
print "User '%s' has been activated" % user_identifier
print "User '%s' has been activated" % user_identifier