Changeset - 9f5493d379b7
[Not reviewed]
0 2 0
Lance Edgar (lance) - 11 years ago 2013-09-03 09:43:27
lance@edbob.org
Added the `adduser` command.
2 files changed with 50 insertions and 0 deletions:
0 comments (0 inline, 0 general)
rattail/commands.py
Show inline comments
 
@@ -54,6 +54,55 @@ See the file COPYING.txt for more information.
 
"""
 

	
 

	
 
class AddUser(commands.Subcommand):
 
    """
 
    Adds a user to the database.
 
    """
 

	
 
    name = 'adduser'
 
    description = "Add a user to the database."
 

	
 
    def add_parser_args(self, parser):
 
        parser.add_argument('url', metavar='URL',
 
                            help="Database engine URL")
 
        parser.add_argument('username',
 
                            help="Username for the new account.")
 
        parser.add_argument('-A', '--administrator',
 
                            action='store_true',
 
                            help="Add the new user to the Administrator role.")
 

	
 
    def run(self, args):
 
        from sqlalchemy import create_engine
 
        from .db import Session
 
        from .db.model import User
 
        from getpass import getpass
 
        from .db.auth import set_user_password, administrator_role
 

	
 
        engine = create_engine(args.url)
 
        session = Session(bind=engine)
 
        if session.query(User).filter_by(username=args.username).count():
 
            session.close()
 
            print("User '{0}' already exists.".format(args.username))
 
            return
 

	
 
        passwd = ''
 
        while not passwd:
 
            try:
 
                passwd = getpass("Enter a password for user '{0}': ".format(args.username))
 
            except KeyboardInterrupt:
 
                print("\nOperation was canceled.")
 
                return
 

	
 
        user = User(username=args.username)
 
        set_user_password(user, passwd)
 
        if args.administrator:
 
            user.roles.append(administrator_role(session))
 
        session.add(user)
 
        session.commit()
 
        session.close()
 
        print("Created user: {0}".format(args.username))
 

	
 

	
 
class DatabaseSyncCommand(commands.Subcommand):
 
    """
 
    Controls the database synchronization service.
setup.py
Show inline comments
 
@@ -170,6 +170,7 @@ rattailw = rattail.commands:main
 
rattail = rattail.db.extension:RattailExtension
 

	
 
[rattail.commands]
 
adduser = rattail.commands:AddUser
 
dbsync = rattail.commands:DatabaseSyncCommand
 
dump = rattail.commands:Dump
 
filemon = rattail.commands:FileMonitorCommand
0 comments (0 inline, 0 general)