From fb4b577eb8eb25c899093e472cf4d1e2d257315d Mon Sep 17 00:00:00 2001 From: Darksider3 Date: Wed, 23 Oct 2019 18:01:13 +0200 Subject: [PATCH] minor adjustemenets --- private/Import.py | 18 ++------- private/editUsers.py | 44 +++++++++------------- private/lib/{validator.py => Validator.py} | 14 +++---- 3 files changed, 28 insertions(+), 48 deletions(-) rename private/lib/{validator.py => Validator.py} (94%) diff --git a/private/Import.py b/private/Import.py index 0859d27..a86ee8f 100755 --- a/private/Import.py +++ b/private/Import.py @@ -30,9 +30,9 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo # noinspection PyBroadException try: with open(file_path, 'r', newline='') as f: - import lib.validator + import lib.Validator sql = lib.sqlitedb.SQLitedb(db) - err = lib.validator.checkImportFile(file_path, db) + err = lib.Validator.checkImportFile(file_path, db) if err is not True: print(err) exit(0) @@ -46,18 +46,8 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo sys_ctl.setUser(row["username"]) sys_ctl.aio_register(row["pubkey"]) print(row['username'], "====> Registered.") - except lib.UserExceptions.UserExistsAlready as UEA: - pass # @TODO User was determined to exists already, shouldn't happen but is possible - except lib.UserExceptions.UnknownReturnCode as URC: - pass # @TODO Unknown Return Codes. Can happen in various function - except lib.UserExceptions.SSHDirUncreatable as SDU: - pass # @TODO SSH Directory doesn't exist AND couldn't be created. Inherently wrong design! - except lib.UserExceptions.ModifyFilesystem as MFS: - pass # @TODO Same as SSH Dir but more general, same problem: Wrong Permissions, - # Missing Dirs etc - except Exception as E: # @TODO well less broad is hard to achieve Kappa - print(E) - continue + except lib.UserExceptions.General as GeneralExcept: + print(f"Something didnt work out! {GeneralExcept}") elif row["status"] == "0": print(row['username'] + " not approved, therefore not registered.") try: diff --git a/private/editUsers.py b/private/editUsers.py index 8754952..c3e200d 100755 --- a/private/editUsers.py +++ b/private/editUsers.py @@ -2,13 +2,12 @@ import configparser import lib.uis.config_ui # only follow -c flag -import lib.validator +import lib.Validator import lib.sqlitedb import lib.System import lib.UserExceptions import sqlite3 - if __name__ == "__main__": lib.uis.config_ui.argparser.description += " - Edit Tilde Users" ArgParser = lib.uis.config_ui.argparser @@ -43,7 +42,7 @@ if __name__ == "__main__": print(f"Well, SOMETHING must be done with {args.user} ;-)") exit(1) # --> --user - if not lib.validator.checkUserInDB(args.user, db): + if not lib.Validator.checkUserInDB(args.user, db): print(f"User {args.user} does not exist in the database.") exit(1) DB = lib.sqlitedb.SQLitedb(db) @@ -72,26 +71,25 @@ if __name__ == "__main__": # --> --sshpubkey if args.sshpubkey: - if not lib.validator.checkSSHKey(args.sshpubkey): + if not lib.Validator.checkSSHKey(args.sshpubkey): print(f"Pubkey '{args.sshpubkey}' isn't valid.") exit(1) try: DB.safequery("UPDATE `applications` SET `pubkey`=? WHERE `username`=?", tuple([args.sshpubkey, args.user])) + CurrentUser = DB.safequery("SELECT * FROM `applications` WHERE `username` = ? ", tuple([args.user]))[0] + if int(CurrentUser["status"]) == 1: + sys_ctl.make_ssh_usable(args.sshpubkey) except sqlite3.Error as e: print(f"Something unexpected happened! {e}") exit(1) - fetch = DB.safequery("SELECT * FROM `applications` WHERE `username` = ? ", tuple([args.user])) - if int(fetch[0]["status"]) == 1: - try: - sys_ctl.make_ssh_usable(args.sshpubkey) - except lib.UserExceptions.ModifyFilesystem as e: - print(f"One action failed during writing the ssh key back to the authorization file. {e}") + except lib.UserExceptions.ModifyFilesystem as e: + print(f"One action failed during writing the ssh key back to the authorization file. {e}") print(f"'{args.user}'s SSH-Key updated successfully.") # --> --name if args.name: - if not lib.validator.checkName(args.name): + if not lib.Validator.checkName(args.name): print(f"'{args.name}' is not a valid Name.") exit(1) try: @@ -102,7 +100,7 @@ if __name__ == "__main__": # --> --email if args.email: - if not lib.validator.checkEmail(args.email): + if not lib.Validator.checkEmail(args.email): print(f"'{args.email}' is not a valid Mail address!") exit(1) try: @@ -116,43 +114,35 @@ if __name__ == "__main__": if args.status != 0 and args.status != 1: print("Only 0 and 1 are valid status, where 1 is activated and 0 is unapproved.") exit(0) + # just takes first result out of the dict if args.status == int(CurrentUser["status"]): print(f"New and old status are the same.") + if args.status == 0 and int(CurrentUser["status"]) == 1: try: DB.safequery("UPDATE `applications` SET `status` =? WHERE `id`=?", tuple([args.status, CurrentUser["id"]])) + sys_ctl.remove_user() except sqlite3.Error as e: print(f"Could not update database entry for '{args.user}', did not touch the system") exit(1) - try: - sys_ctl.remove_user() except lib.UserExceptions.UnknownReturnCode as e: print(f"Could not remove '{args.user}' from the system, unknown return code: {e}. DB is modified.") exit(1) - print(f"Successfully changed '{args.user}'s status to 0 and cleared from the system.") + if args.status == 1 and int(CurrentUser["status"]) == 0: try: DB.safequery("UPDATE `applications` SET `status`=? WHERE `username`=?", tuple([args.status, args.user])) + sys_ctl.aio_register(CurrentUser["pubkey"]) except sqlite3.Error as e: print(f"Could not update Users status in database") exit(1) - try: - sys_ctl.aio_register(CurrentUser["pubkey"]) - except lib.UserExceptions.UserExistsAlready as UEA: - print(f"Somehow the user exists already on the system! {UEA}") - exit(1) - except lib.UserExceptions.UnknownReturnCode as URC: - print(f"Unknown return code: {URC}") - exit(1) - except lib.UserExceptions.SSHDirUncreatable as SDU: - print(f"Could not create ssh directory for {args.user}, exception: {SDU}") + except lib.UserExceptions.General as ChangeUser: + print(f"Some chain in the cattle just slipped away, my lord! {ChangeUser}") exit(1) - except lib.UserExceptions.ModifyFilesystem as MFS: - pass print(f"Successfully changed '{args.user}'s status to 1 and created on the system.") exit(0) except KeyboardInterrupt as e: diff --git a/private/lib/validator.py b/private/lib/Validator.py similarity index 94% rename from private/lib/validator.py rename to private/lib/Validator.py index ede8c13..2ff39cc 100644 --- a/private/lib/validator.py +++ b/private/lib/Validator.py @@ -178,15 +178,15 @@ def checkImportFile(path: str, db: str): reader = csv.DictReader(f) for row in reader: # if any of this fails move on to the next user, just print a relatively helpful message lel - if not lib.validator.checkName(row["name"]): + if not lib.Validator.checkName(row["name"]): errstr += f"Line {ln}: Name: '{row['name']}' seems not legit. Character followed by character should" \ f" be correct.\n" valid = False - if not lib.validator.checkUsernameCharacters(row["username"]): + if not lib.Validator.checkUsernameCharacters(row["username"]): errstr += (f"Line {ln}: Username contains unsupported characters or starts with a number: '" f"{row['username']}'.\n") valid = False - if not lib.validator.checkUsernameLength(row["username"]): + if not lib.Validator.checkUsernameLength(row["username"]): errstr += f"Line {ln}: Username '{row['username']}' is either too long(>16) or short(<3)\n" valid = False # dup checking @@ -196,17 +196,17 @@ def checkImportFile(path: str, db: str): else: valid_names_list.append(row["username"]) # dup end - if not lib.validator.checkSSHKey(row["pubkey"]): + if not lib.Validator.checkSSHKey(row["pubkey"]): errstr += f"Line {ln}: Following SSH-Key of user '{row['username']}' isn't valid: " \ f"'{row['pubkey']}'.\n" valid = False - if not lib.validator.checkEmail(row["email"]): + if not lib.Validator.checkEmail(row["email"]): errstr += f"Line {ln}: E-Mail address of user '{row['username']}' '{row['email']}' is not valid.\n" valid = False - if not lib.validator.checkUserExists(row["username"]) or checkUserInDB(row["username"], db): + if not lib.Validator.checkUserExists(row["username"]) or checkUserInDB(row["username"], db): errstr += f"Line {ln}: User '{row['username']}' already exists.\n" valid = False - if not lib.validator.checkDatetimeFormat(row["timestamp"]): + if not lib.Validator.checkDatetimeFormat(row["timestamp"]): errstr += f"Line {ln}: Timestamp '{row['timestamp']}' from user '{row['username']}' is invalid.\n" valid = False if int(row["status"]) > 1 or int(row["status"]) < 0: