1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06: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 config
from db import get_store, Folder
from db import get_store, Folder, User
from managers.folder import FolderManager
from managers.user import UserManager
from scanner import Scanner
@ -178,7 +178,7 @@ class CLI(cmd.Cmd):
def user_list(self):
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):
if not password:
@ -187,26 +187,26 @@ class CLI(cmd.Cmd):
if password != confirm:
print >>sys.stderr, "Passwords don't match"
return
status = UserManager.add(name, password, email, admin)
status = UserManager.add(self.__store, name, password, email, admin)
if status != UserManager.SUCCESS:
print >>sys.stderr, UserManager.error_str(status)
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:
print >>sys.stderr, 'No such user'
else:
db.session.delete(user)
db.session.commit()
self.__store.remove(user)
self.__store.commit()
print "User '{}' deleted".format(name)
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:
print >>sys.stderr, 'No such user'
else:
user.admin = not off
db.session.commit()
self.__store.commit()
print "{0} '{1}' admin rights".format('Revoked' if off else 'Granted', name)
def user_changepass(self, name, password):
@ -216,7 +216,7 @@ class CLI(cmd.Cmd):
if password != confirm:
print >>sys.stderr, "Passwords don't match"
return
status = UserManager.change_password2(name, password)
status = UserManager.change_password2(self.__store, name, password)
if status != UserManager.SUCCESS:
print >>sys.stderr, UserManager.error_str(status)
else:

View File

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