diff --git a/private/ListUsers.py b/private/ListUsers.py index 5be3512..6deb520 100644 --- a/private/ListUsers.py +++ b/private/ListUsers.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import sqlite3 from lib.sqlitedb import SQLitedb import lib.CFG as CFG @@ -18,10 +19,10 @@ class ListUsers: query = "SELECT * FROM `applications`" self.usersFetch = self.db.query(query) - def prettyPrint(self): + def prettyPrint(self) -> None: pass # see below why not implemented yet, texttable... - def getFetch(self): + def getFetch(self) -> sqlite3: return self.usersFetch diff --git a/private/lib/System.py b/private/lib/System.py index ee11d78..354a4ff 100644 --- a/private/lib/System.py +++ b/private/lib/System.py @@ -13,24 +13,25 @@ class System: self.dry = dryrun self.home = home - def register(self, username: str, pubkey: str, cc: tuple = tuple(["useradd", "-m"])): + def register(self, username: str, pubkey: str, cc: tuple = tuple(["useradd", "-m"])) -> bool: create_command = cc cc = create_command + tuple([username]) if self.dry: self.printTuple(cc) - return 0 + return True elif not self.dry: rt = subprocess.call(cc) if rt != 0: print(f"Could not create user {username}; '{cc}' returned '{rt}'", file=sys.stderr) # @TODO Logging/Exception return False + return True def unregister(self, username: str): pass # @TODO errno - def make_ssh_usable(self, username: str, pubkey: str, sshdir: str = ".ssh/"): + def make_ssh_usable(self, username: str, pubkey: str, sshdir: str = ".ssh/") -> bool: if self.dry: print("Nah, @TODO, but actually kinda too lazy for this lul. Just a lot happening here") return True @@ -64,38 +65,53 @@ class System: return False # @TODO Exception in Log return True - def lock_user_pw(self, username: str, cc: tuple = tuple(["usermod", "--lock"])): + def lock_user_pw(self, username: str, cc: tuple = tuple(["usermod", "--lock"])) -> bool: lock_command = cc cc = lock_command + tuple([username]) if self.dry: self.printTuple(cc) - return 0 + return True elif not self.dry: rt = subprocess.call(cc) if rt != 0: print(f"Could not lock user '{username}'; '{cc}' returned '{rt}'", file=sys.stderr) return False # @TODO Exception in Log + return True - def add_to_usergroup(self, username: str, group: str = "tilde", cc: tuple = tuple(["usermod", "-a", "-G"])): + def add_to_usergroup(self, username: str, group: str = "tilde", cc: tuple = tuple(["usermod", "-a", "-G"])) -> bool: add_command = cc cc = add_command + tuple([group, username]) if self.dry: self.printTuple(cc) - return 0 + return True elif not self.dry: rt = subprocess.call(cc) if rt != 0: print(f"Could not add user '{username}' to group '{group}' with command '{cc}', returned '{rt}'", file=sys.stderr) # @TODO Exception in Log return False + return True - def printTuple(self, tup: tuple): + def printTuple(self, tup: tuple) -> None: pp = "" for i in tup: pp += i + " " print(pp) + def removeUser(self, username: str, cc: tuple = tuple(["userdel", "-r"])) -> bool: + remove_command = cc + cc = remove_command + tuple([username]) + if self.dry: + self.printTuple(cc) + return True + else: + ret = subprocess.call(cc) + if ret != 0: + print(f"Could not delete user with command {cc}. Return code: {ret}") + return False + return True + if __name__ == "__main__": try: diff --git a/private/lib/sqlitedb.py b/private/lib/sqlitedb.py index 6544cc5..fed0953 100644 --- a/private/lib/sqlitedb.py +++ b/private/lib/sqlitedb.py @@ -15,7 +15,7 @@ class SQLitedb: db = "" cursor = None connection = None - lastrow = None + last_result = None def __init__(self, dbpath: str): db = dbpath @@ -29,37 +29,56 @@ class SQLitedb: def __del__(self): try: + self.connection.commit() self.connection.close() except sqlite3.Error as e: print("Couldn't gracefully close db: %s" % e, file=STDERR) - def query(self, qq: str): + def query(self, qq: str) -> list: try: self.cursor.execute(qq) - self.lastrow = self.cursor.fetchall() + self.last_result = self.cursor.fetchall() except sqlite3.Error as e: print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR) - self.lastrow = [] - return self.lastrow + self.last_result = [] + return self.last_result # sometimes we need the cursor for safety reasons, for example does sqlite3 all the security related # escaoing in supplied strings for us, when we deliver it to con.execute in the second argument as a tuple - def getCursor(self): + def getCursor(self) -> sqlite3: return self.cursor # we could try to utilise that ourselfs in a function. Be c a r e f u l, these values in the tuple MUST HAVE # THE RIGHT TYPE - def safequery(self, qq: str, deliver: tuple): + def safequery(self, qq: str, deliver: tuple) -> list: try: self.cursor.execute(qq, deliver) - self.lastrow = self.cursor.fetchall() + self.last_result = self.cursor.fetchall() except sqlite3.Error as e: print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR) - self.lastrow = [] + self.last_result = [] except TypeError as e: print("Types in given tuple doesnt match to execute query \"%s\": %s" % (qq, e), file=STDERR) - self.lastrow = [] - return self.lastrow + self.last_result = [] + return self.last_result + + def removeApplicantFromDB(self, userid: int) -> bool: + try: + self.last_result = self.cursor.execute("DELETE FROM `applications` WHERE id = ? ", [userid]) + self.connection.commit() + except sqlite3.Error as e: + print(f"Could not delete user with id: {userid}, exception in DB: {e}") # @TODO LOGGING FFS + return False + return True + + def removeApplicantFromDBperUsername(self, username: str) -> bool: + try: + self.last_result = self.cursor.execute("DELETE FROM `applications` WHERE username = ?", [username]) + self.connection.commit() + except sqlite3.Error as e: + print(f"Could not delete user {username}, exception in DB: {e}") # @TODO LOGGING + return False + return True if __name__ == "__main__":