From 3a06107fd646537e120dedd624d2774f9ae78f13 Mon Sep 17 00:00:00 2001 From: spl0k Date: Sat, 15 Mar 2014 18:51:15 +0100 Subject: [PATCH] Updated user part of the CLI and UserManager --- cli.py | 18 +++++++++--------- managers/user.py | 45 ++++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/cli.py b/cli.py index 93eebc0..d9f8078 100755 --- a/cli.py +++ b/cli.py @@ -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: diff --git a/managers/user.py b/managers/user.py index b748a29..610035b 100644 --- a/managers/user.py +++ b/managers/user.py @@ -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