Adds ability to delete users in db as well as on the system + type hints..

.... in function names for further documentation.
pull/4/head
Darksider3 5 years ago
parent a6d63fee42
commit d413662b36

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sqlite3
from lib.sqlitedb import SQLitedb from lib.sqlitedb import SQLitedb
import lib.CFG as CFG import lib.CFG as CFG
@ -18,10 +19,10 @@ class ListUsers:
query = "SELECT * FROM `applications`" query = "SELECT * FROM `applications`"
self.usersFetch = self.db.query(query) self.usersFetch = self.db.query(query)
def prettyPrint(self): def prettyPrint(self) -> None:
pass # see below why not implemented yet, texttable... pass # see below why not implemented yet, texttable...
def getFetch(self): def getFetch(self) -> sqlite3:
return self.usersFetch return self.usersFetch

@ -13,24 +13,25 @@ class System:
self.dry = dryrun self.dry = dryrun
self.home = home 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 create_command = cc
cc = create_command + tuple([username]) cc = create_command + tuple([username])
if self.dry: if self.dry:
self.printTuple(cc) self.printTuple(cc)
return 0 return True
elif not self.dry: elif not self.dry:
rt = subprocess.call(cc) rt = subprocess.call(cc)
if rt != 0: if rt != 0:
print(f"Could not create user {username}; '{cc}' returned '{rt}'", file=sys.stderr) print(f"Could not create user {username}; '{cc}' returned '{rt}'", file=sys.stderr)
# @TODO Logging/Exception # @TODO Logging/Exception
return False return False
return True
def unregister(self, username: str): def unregister(self, username: str):
pass pass
# @TODO errno # @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: if self.dry:
print("Nah, @TODO, but actually kinda too lazy for this lul. Just a lot happening here") print("Nah, @TODO, but actually kinda too lazy for this lul. Just a lot happening here")
return True return True
@ -64,38 +65,53 @@ class System:
return False # @TODO Exception in Log return False # @TODO Exception in Log
return True 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 lock_command = cc
cc = lock_command + tuple([username]) cc = lock_command + tuple([username])
if self.dry: if self.dry:
self.printTuple(cc) self.printTuple(cc)
return 0 return True
elif not self.dry: elif not self.dry:
rt = subprocess.call(cc) rt = subprocess.call(cc)
if rt != 0: if rt != 0:
print(f"Could not lock user '{username}'; '{cc}' returned '{rt}'", file=sys.stderr) print(f"Could not lock user '{username}'; '{cc}' returned '{rt}'", file=sys.stderr)
return False return False
# @TODO Exception in Log # @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 add_command = cc
cc = add_command + tuple([group, username]) cc = add_command + tuple([group, username])
if self.dry: if self.dry:
self.printTuple(cc) self.printTuple(cc)
return 0 return True
elif not self.dry: elif not self.dry:
rt = subprocess.call(cc) rt = subprocess.call(cc)
if rt != 0: if rt != 0:
print(f"Could not add user '{username}' to group '{group}' with command '{cc}', returned '{rt}'", print(f"Could not add user '{username}' to group '{group}' with command '{cc}', returned '{rt}'",
file=sys.stderr) # @TODO Exception in Log file=sys.stderr) # @TODO Exception in Log
return False return False
return True
def printTuple(self, tup: tuple): def printTuple(self, tup: tuple) -> None:
pp = "" pp = ""
for i in tup: for i in tup:
pp += i + " " pp += i + " "
print(pp) 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__": if __name__ == "__main__":
try: try:

@ -15,7 +15,7 @@ class SQLitedb:
db = "" db = ""
cursor = None cursor = None
connection = None connection = None
lastrow = None last_result = None
def __init__(self, dbpath: str): def __init__(self, dbpath: str):
db = dbpath db = dbpath
@ -29,37 +29,56 @@ class SQLitedb:
def __del__(self): def __del__(self):
try: try:
self.connection.commit()
self.connection.close() self.connection.close()
except sqlite3.Error as e: except sqlite3.Error as e:
print("Couldn't gracefully close db: %s" % e, file=STDERR) print("Couldn't gracefully close db: %s" % e, file=STDERR)
def query(self, qq: str): def query(self, qq: str) -> list:
try: try:
self.cursor.execute(qq) self.cursor.execute(qq)
self.lastrow = self.cursor.fetchall() self.last_result = self.cursor.fetchall()
except sqlite3.Error as e: except sqlite3.Error as e:
print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR) print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR)
self.lastrow = [] self.last_result = []
return self.lastrow return self.last_result
# sometimes we need the cursor for safety reasons, for example does sqlite3 all the security related # 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 # 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 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 # 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 # THE RIGHT TYPE
def safequery(self, qq: str, deliver: tuple): def safequery(self, qq: str, deliver: tuple) -> list:
try: try:
self.cursor.execute(qq, deliver) self.cursor.execute(qq, deliver)
self.lastrow = self.cursor.fetchall() self.last_result = self.cursor.fetchall()
except sqlite3.Error as e: except sqlite3.Error as e:
print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR) print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR)
self.lastrow = [] self.last_result = []
except TypeError as e: except TypeError as e:
print("Types in given tuple doesnt match to execute query \"%s\": %s" % (qq, e), file=STDERR) print("Types in given tuple doesnt match to execute query \"%s\": %s" % (qq, e), file=STDERR)
self.lastrow = [] self.last_result = []
return self.lastrow 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__": if __name__ == "__main__":

Loading…
Cancel
Save