From f31117e940a1da5942ddd4773dfd196f39421c33 Mon Sep 17 00:00:00 2001 From: Darksider3 Date: Wed, 23 Oct 2019 17:50:13 +0200 Subject: [PATCH] System: aio_register, performs all neccessary steps to create user... BUT will NOT catch ANY exceptions. Therefore the caller script itself is REQUIRED to catch. --- private/Import.py | 5 +---- private/editUsers.py | 5 +---- private/lib/System.py | 28 ++++++++++++++++++++-------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/private/Import.py b/private/Import.py index 1010fb1..0859d27 100755 --- a/private/Import.py +++ b/private/Import.py @@ -44,10 +44,7 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo if row["status"] == "1": try: sys_ctl.setUser(row["username"]) - sys_ctl.register() - sys_ctl.lock_user_pw() - sys_ctl.add_to_usergroup() - sys_ctl.make_ssh_usable(row["pubkey"]) + 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 diff --git a/private/editUsers.py b/private/editUsers.py index 5075847..8754952 100755 --- a/private/editUsers.py +++ b/private/editUsers.py @@ -141,10 +141,7 @@ if __name__ == "__main__": print(f"Could not update Users status in database") exit(1) try: - sys_ctl.register() - sys_ctl.lock_user_pw() - sys_ctl.add_to_usergroup() - sys_ctl.make_ssh_usable(CurrentUser["pubkey"]) + 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) diff --git a/private/lib/System.py b/private/lib/System.py index aba22f2..467a02b 100644 --- a/private/lib/System.py +++ b/private/lib/System.py @@ -45,6 +45,26 @@ class System: self.home = home self.user = username + def aio_register(self, pubkey, group="tilde"): + """ Executes all neccessary steps to create a user from itself. Raises ALOT of possible exceptions + + :Note: CAREFULL! You MUST except the exceptions! + :param pubkey: Users public ssh key + :type pubkey: str + :param group: User-group. Defaults to tilde + :type group: str + :return: None + :raises: + lib.UserExceptions.UserExistsAlready: User Exists already on system + lib.UserExceptions.UnknownReturnCode: Unknown Return Code from useradd + lib.UserExceptions.SSHDirUncreatable: Users SSH Dir couldnt be created + lib.UserExceptions.ModifyFilesystem: Something with CHMOD failed + """ + self.register() + self.lock_user_pw() + self.add_to_usergroup(group) + self.make_ssh_usable(pubkey) + def register(self, cc: tuple = tuple(["useradd", "-m"])) -> bool: """Creates an local account for the given username @@ -226,14 +246,6 @@ class System: return True -def AIO(username, pubkey, group="tilde"): - sys_ctl = System(username, dryrun=False) - sys_ctl.register() - sys_ctl.lock_user_pw() - sys_ctl.add_to_usergroup(group) - sys_ctl.make_ssh_usable(pubkey) - - if __name__ == "__main__": try: S = System("dar", dryrun=True)