mirror of
https://github.com/wassname/flask-security.git
synced 2026-06-27 16:10:11 +08:00
147 lines
4.0 KiB
Python
147 lines
4.0 KiB
Python
|
|
try:
|
|
import simplejson as json
|
|
except ImportError:
|
|
import json
|
|
|
|
import inspect
|
|
import os
|
|
import re
|
|
|
|
from flask import current_app
|
|
from flask.ext.script import Command, Option, prompt_bool
|
|
from werkzeug.local import LocalProxy
|
|
|
|
from flask_security import views, utils
|
|
|
|
|
|
_datastore = LocalProxy(lambda: current_app.extensions['security'].datastore)
|
|
|
|
|
|
def pprint(obj):
|
|
print json.dumps(obj, sort_keys=True, indent=4)
|
|
|
|
|
|
class CreateUserCommand(Command):
|
|
"""Create a user"""
|
|
|
|
option_list = (
|
|
Option('-e', '--email', dest='email', default=None),
|
|
Option('-p', '--password', dest='password', default=None),
|
|
Option('-a', '--active', dest='active', default=''),
|
|
Option('-r', '--roles', dest='roles', default=''),
|
|
)
|
|
|
|
def run(self, **kwargs):
|
|
# sanitize active input
|
|
ai = re.sub(r'\s', '', str(kwargs['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(',')
|
|
kwargs['password'] = utils.encrypt_password(kwargs['password'])
|
|
|
|
_datastore.create_user(**kwargs)
|
|
|
|
print 'User created successfully.'
|
|
kwargs['password'] = '****'
|
|
pprint(kwargs)
|
|
|
|
|
|
class CreateRoleCommand(Command):
|
|
"""Create a role"""
|
|
|
|
option_list = (
|
|
Option('-n', '--name', dest='name', default=None),
|
|
Option('-d', '--desc', dest='description', default=None),
|
|
)
|
|
|
|
def run(self, **kwargs):
|
|
_datastore.create_role(**kwargs)
|
|
print 'Role "%(name)s" created successfully.' % kwargs
|
|
|
|
|
|
class _RoleCommand(Command):
|
|
option_list = (
|
|
Option('-u', '--user', dest='user_identifier'),
|
|
Option('-r', '--role', dest='role_name'),
|
|
)
|
|
|
|
|
|
class AddRoleCommand(_RoleCommand):
|
|
"""Add a role to a user"""
|
|
|
|
def run(self, user_identifier, role_name):
|
|
_datastore.add_role_to_user(user_identifier, role_name)
|
|
print "Role '%s' added to user '%s' successfully" % (role_name, user_identifier)
|
|
|
|
|
|
class RemoveRoleCommand(_RoleCommand):
|
|
"""Add a role to a user"""
|
|
|
|
def run(self, user_identifier, role_name):
|
|
_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):
|
|
_datastore.deactivate_user(user_identifier)
|
|
print "User '%s' has been deactivated" % user_identifier
|
|
|
|
|
|
class ActivateUserCommand(_ToggleActiveCommand):
|
|
"""Deactive a user"""
|
|
|
|
def run(self, user_identifier):
|
|
_datastore.activate_user(user_identifier)
|
|
print "User '%s' has been activated" % user_identifier
|
|
|
|
|
|
class GenerateBlueprintCommand(Command):
|
|
"""Generate a Flask-Security blueprint object"""
|
|
|
|
option_list = (
|
|
Option('--output', '-o', dest='output', default=None),
|
|
)
|
|
|
|
def run(self, output):
|
|
output = os.path.join(os.getcwd(), output) if output else 'security.py'
|
|
|
|
if os.path.exists(output):
|
|
msg = 'File %s exists. Do you want to overwrite it?' % output
|
|
if not prompt_bool(msg):
|
|
return
|
|
|
|
with open(output, 'w') as o:
|
|
source = inspect.getfile(views).replace('.pyc', '.py')
|
|
|
|
with open(source, 'r') as s:
|
|
to_remove = '"""' + views.__doc__ + '"""'
|
|
to_replace = """
|
|
\"""
|
|
Flask-Security
|
|
~~~~~~~~~~~~~~
|
|
|
|
This module was generated by Flask-Security to give developers greater
|
|
control over the various security mechanisms. For more information about
|
|
using this feature see:
|
|
|
|
TODO: Documentation URL
|
|
\"""
|
|
"""
|
|
contents = s.read().replace(to_remove, to_replace)
|
|
o.write(contents)
|
|
|
|
print 'File generated successfully.'
|
|
print output
|