1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

Updated user part of the CLI and UserManager

This commit is contained in:
spl0k 2014-03-15 18:51:15 +01:00
parent 17e580e7b5
commit 3a06107fd6
2 changed files with 35 additions and 28 deletions

18
cli.py
View File

@ -22,7 +22,7 @@
import sys, cmd, argparse, getpass, time import sys, cmd, argparse, getpass, time
import config import config
from db import get_store, Folder from db import get_store, Folder, User
from managers.folder import FolderManager from managers.folder import FolderManager
from managers.user import UserManager from managers.user import UserManager
from scanner import Scanner from scanner import Scanner
@ -178,7 +178,7 @@ class CLI(cmd.Cmd):
def user_list(self): def user_list(self):
print 'Name\t\tAdmin\tEmail\n----\t\t-----\t-----' 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()) print '\n'.join('{0: <16}{1}\t{2}'.format(u.name, '*' if u.admin else '', u.mail) for u in self.__store.find(User))
def user_add(self, name, admin, password, email): def user_add(self, name, admin, password, email):
if not password: if not password:
@ -187,26 +187,26 @@ class CLI(cmd.Cmd):
if password != confirm: if password != confirm:
print >>sys.stderr, "Passwords don't match" print >>sys.stderr, "Passwords don't match"
return return
status = UserManager.add(name, password, email, admin) status = UserManager.add(self.__store, name, password, email, admin)
if status != UserManager.SUCCESS: if status != UserManager.SUCCESS:
print >>sys.stderr, UserManager.error_str(status) print >>sys.stderr, UserManager.error_str(status)
def user_delete(self, name): def user_delete(self, name):
user = db.User.query.filter(db.User.name == name).first() user = self.__store.find(User, User.name == name).one()
if not user: if not user:
print >>sys.stderr, 'No such user' print >>sys.stderr, 'No such user'
else: else:
db.session.delete(user) self.__store.remove(user)
db.session.commit() self.__store.commit()
print "User '{}' deleted".format(name) print "User '{}' deleted".format(name)
def user_setadmin(self, name, off): def user_setadmin(self, name, off):
user = db.User.query.filter(db.User.name == name).first() user = self.__store.find(User, User.name == name).one()
if not user: if not user:
print >>sys.stderr, 'No such user' print >>sys.stderr, 'No such user'
else: else:
user.admin = not off user.admin = not off
db.session.commit() self.__store.commit()
print "{0} '{1}' admin rights".format('Revoked' if off else 'Granted', name) print "{0} '{1}' admin rights".format('Revoked' if off else 'Granted', name)
def user_changepass(self, name, password): def user_changepass(self, name, password):
@ -216,7 +216,7 @@ class CLI(cmd.Cmd):
if password != confirm: if password != confirm:
print >>sys.stderr, "Passwords don't match" print >>sys.stderr, "Passwords don't match"
return return
status = UserManager.change_password2(name, password) status = UserManager.change_password2(self.__store, name, password)
if status != UserManager.SUCCESS: if status != UserManager.SUCCESS:
print >>sys.stderr, UserManager.error_str(status) print >>sys.stderr, UserManager.error_str(status)
else: else:

View File

@ -31,7 +31,7 @@ class UserManager:
WRONG_PASS = 4 WRONG_PASS = 4
@staticmethod @staticmethod
def get(uid): def get(stotre, uid):
if type(uid) in (str, unicode): if type(uid) in (str, unicode):
try: try:
uid = uuid.UUID(uid) uid = uuid.UUID(uid)
@ -42,40 +42,47 @@ class UserManager:
else: else:
return UserManager.INVALID_ID, None return UserManager.INVALID_ID, None
user = User.query.get(uid) user = store.get(User, uid)
if user is None: if user is None:
return UserManager.NO_SUCH_USER, None return UserManager.NO_SUCH_USER, None
return UserManager.SUCCESS, user return UserManager.SUCCESS, user
@staticmethod @staticmethod
def add(name, password, mail, admin): def add(store, name, password, mail, admin):
if User.query.filter(User.name == name).first(): if store.find(User, User.name == name).one():
return UserManager.NAME_EXISTS return UserManager.NAME_EXISTS
password = UserManager.__decode_password(password) password = UserManager.__decode_password(password)
crypt, salt = UserManager.__encrypt_password(password) crypt, salt = UserManager.__encrypt_password(password)
user = User(name = name, mail = mail, password = crypt, salt = salt, admin = admin)
session.add(user) user = User()
session.commit() user.name = name
user.mail = mail
user.password = crypt
user.salt = salt
user.admin = admin
store.add(user)
store.commit()
return UserManager.SUCCESS return UserManager.SUCCESS
@staticmethod @staticmethod
def delete(uid): def delete(store, uid):
status, user = UserManager.get(uid) status, user = UserManager.get(store, uid)
if status != UserManager.SUCCESS: if status != UserManager.SUCCESS:
return status return status
session.delete(user) store.remove(user)
session.commit() store.commit()
return UserManager.SUCCESS return UserManager.SUCCESS
@staticmethod @staticmethod
def try_auth(name, password): def try_auth(store, name, password):
password = UserManager.__decode_password(password) password = UserManager.__decode_password(password)
user = User.query.filter(User.name == name).first() user = store.find(User, User.name == name).one()
if not user: if not user:
return UserManager.NO_SUCH_USER, None return UserManager.NO_SUCH_USER, None
elif UserManager.__encrypt_password(password, user.salt)[0] != user.password: elif UserManager.__encrypt_password(password, user.salt)[0] != user.password:
@ -84,8 +91,8 @@ class UserManager:
return UserManager.SUCCESS, user return UserManager.SUCCESS, user
@staticmethod @staticmethod
def change_password(uid, old_pass, new_pass): def change_password(store, uid, old_pass, new_pass):
status, user = UserManager.get(uid) status, user = UserManager.get(store, uid)
if status != UserManager.SUCCESS: if status != UserManager.SUCCESS:
return status return status
@ -96,18 +103,18 @@ class UserManager:
return UserManager.WRONG_PASS return UserManager.WRONG_PASS
user.password = UserManager.__encrypt_password(new_pass, user.salt)[0] user.password = UserManager.__encrypt_password(new_pass, user.salt)[0]
session.commit() store.commit()
return UserManager.SUCCESS return UserManager.SUCCESS
@staticmethod @staticmethod
def change_password2(name, new_pass): def change_password2(store, name, new_pass):
user = User.query.filter(User.name == name).first() user = store.find(User, User.name == name).one()
if not user: if not user:
return UserManager.NO_SUCH_USER return UserManager.NO_SUCH_USER
new_pass = UserManager.__decode_password(new_pass) new_pass = UserManager.__decode_password(new_pass)
user.password = UserManager.__encrypt_password(new_pass, user.salt)[0] user.password = UserManager.__encrypt_password(new_pass, user.salt)[0]
session.commit() store.commit()
return UserManager.SUCCESS return UserManager.SUCCESS
@staticmethod @staticmethod