mirror of
https://github.com/spl0k/supysonic.git
synced 2024-11-10 04:02:17 +00:00
CLI also provides user management
This commit is contained in:
parent
4f12fc25f0
commit
e8b3e7ca24
76
cli.py
76
cli.py
@ -1,6 +1,6 @@
|
||||
# coding: utf-8
|
||||
|
||||
import sys, cmd, argparse
|
||||
import sys, cmd, argparse, getpass
|
||||
import config
|
||||
|
||||
class CLIParser(argparse.ArgumentParser):
|
||||
@ -41,7 +41,7 @@ class CLI(cmd.Cmd):
|
||||
|
||||
if args.action == 'list':
|
||||
print 'Name\t\tPath\n----\t\t----'
|
||||
print '\n'.join('%s\t\t%s' % (f.name, f.path) for f in db.Folder.query.filter(db.Folder.root == True))
|
||||
print '\n'.join('{0: <16}{1}'.format(f.name, f.path) for f in db.Folder.query.filter(db.Folder.root == True))
|
||||
elif args.action == 'add':
|
||||
ret = FolderManager.add(args.name, args.path)
|
||||
if ret != FolderManager.SUCCESS:
|
||||
@ -78,6 +78,77 @@ class CLI(cmd.Cmd):
|
||||
#for cmd, parser in self.folder_subparsers.choices.iteritems():
|
||||
# parser.print_help()
|
||||
|
||||
user_parser = CLIParser(prog = 'user', add_help = False)
|
||||
user_subparsers = user_parser.add_subparsers(dest = 'action')
|
||||
user_subparsers.add_parser('list', help = 'List users', add_help = False)
|
||||
user_add_parser = user_subparsers.add_parser('add', help = 'Adds a user', add_help = False)
|
||||
user_add_parser.add_argument('name', help = 'Name/login of the user to add')
|
||||
user_add_parser.add_argument('-a', '--admin', action = 'store_true', help = 'Give admin rights to the new user')
|
||||
user_add_parser.add_argument('-p', '--password', help = "Specifies the user's password")
|
||||
user_add_parser.add_argument('-e', '--email', default = '', help = "Sets the user's email address")
|
||||
user_del_parser = user_subparsers.add_parser('delete', help = 'Deletes a user', add_help = False)
|
||||
user_del_parser.add_argument('name', help = 'Name/login of the user to delete')
|
||||
user_admin_parser = user_subparsers.add_parser('set-admin', help = 'Enable/disable admin rights for a user', add_help = False)
|
||||
user_admin_parser.add_argument('name', help = 'Name/login of the user to grant/revoke admin rights')
|
||||
user_admin_parser.add_argument('--off', action = 'store_true', help = 'Revoke admin rights if present, grant them otherwise')
|
||||
user_pass_parser = user_subparsers.add_parser('changepass', help = "Changes a user's password", add_help = False)
|
||||
user_pass_parser.add_argument('name', help = 'Name/login of the user to which change the password')
|
||||
user_pass_parser.add_argument('password', nargs = '?', help = 'New password')
|
||||
|
||||
def do_user(self, line):
|
||||
try:
|
||||
args = self.user_parser.parse_args(line.split())
|
||||
except RuntimeError, e:
|
||||
print >>sys.stderr, e.message
|
||||
return
|
||||
|
||||
if args.action == 'list':
|
||||
print 'Name\t\tAdmin\tEmail\n----\t\t-----\t-----'
|
||||
print '\n'.join('{0: <16}{1}\t{2}'.format(u.name, '*' if u.admin else '', u.mail) for u in db.User.query.all())
|
||||
elif args.action == 'add':
|
||||
if not args.password:
|
||||
password = getpass.getpass()
|
||||
confirm = getpass.getpass('Confirm password: ')
|
||||
if password != confirm:
|
||||
print >>sys.stderr, "Passwords don't match"
|
||||
return
|
||||
args.password = password
|
||||
status = UserManager.add(args.name, args.password, args.email, args.admin)
|
||||
if status != UserManager.SUCCESS:
|
||||
print >>sys.stderr, UserManager.error_str(status)
|
||||
elif args.action == 'delete':
|
||||
user = db.User.query.filter(db.User.name == args.name).first()
|
||||
if not user:
|
||||
print >>sys.stderr, 'No such user'
|
||||
else:
|
||||
db.session.delete(user)
|
||||
db.session.commit()
|
||||
print "User '{0.name}' deleted".format(args)
|
||||
elif args.action == 'set-admin':
|
||||
user = db.User.query.filter(db.User.name == args.name).first()
|
||||
if not user:
|
||||
print >>sys.stderr, 'No such user'
|
||||
else:
|
||||
user.admin = not args.off
|
||||
db.session.commit()
|
||||
print "{0} '{1}' admin rights".format('Revoked' if args.off else 'Granted', args.name)
|
||||
elif args.action == 'changepass':
|
||||
if not args.password:
|
||||
password = getpass.getpass()
|
||||
confirm = getpass.getpass('Confirm password: ')
|
||||
if password != confirm:
|
||||
print >>sys.stderr, "Passwords don't match"
|
||||
return
|
||||
args.password = password
|
||||
status = UserManager.change_password2(args.name, args.password)
|
||||
if status != UserManager.SUCCESS:
|
||||
print >>sys.stderr, UserManager.error_str(status)
|
||||
else:
|
||||
print "Successfully changed '{}' password".format(args.name)
|
||||
|
||||
def help_user(self):
|
||||
self.user_parser.print_help()
|
||||
|
||||
if __name__ == "__main__":
|
||||
if not config.check():
|
||||
sys.exit(1)
|
||||
@ -86,6 +157,7 @@ if __name__ == "__main__":
|
||||
db.init_db()
|
||||
|
||||
from managers.folder import FolderManager
|
||||
from managers.user import UserManager
|
||||
from scanner import Scanner
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
|
Loading…
Reference in New Issue
Block a user