editUsers: Refactor from System taken

pull/4/head
Darksider3 5 years ago
parent 05f27078f6
commit dd01acf801

@ -42,34 +42,38 @@ if __name__ == "__main__":
and not args.remove:
print(f"Well, SOMETHING must be done with {args.user} ;-)")
exit(1)
# --> --user
if not lib.validator.checkUserInDB(args.user, db):
print(f"User {args.user} doesn't exist in the database.")
print(f"User {args.user} does not exist in the database.")
exit(1)
DB = lib.sqlitedb.SQLitedb(db)
Sysctl = lib.System.System()
sys_ctl = lib.System.System(args.user)
if not DB:
print("Couldn't establish connection to database")
print("Could not establish connection to database")
exit(1)
CurrentUser = DB.safequery("SELECT * FROM `applications` WHERE `username`=?", tuple([args.user]))[0]
# --> --remove
if args.remove:
print(f"Removing {args.user} from the system and the database...")
try:
DB.removeApplicantFromDBperUsername(args.user)
print(f"Purged from the DB")
if CurrentUser["status"] == 1:
Sysctl.remove_user(args.user)
sys_ctl.remove_user()
print(f"Purged from the system")
else:
print(f"{args.user} wasn't approved before, therefore not deleting from system itself.")
print(f"'{args.user}' was not approved before, therefore not deleting from system itself.")
except lib.UserExceptions.General as e:
print(f"{e}")
exit(1)
print("Success.")
print(f"Successfully removed '{args.user}'.")
exit(0)
# --> --sshpubkey
if args.sshpubkey:
if not lib.validator.checkSSHKey(args.sshpubkey):
print(f"Pubkey {args.sshpubkey} isn't valid.")
print(f"Pubkey '{args.sshpubkey}' isn't valid.")
exit(1)
try:
DB.safequery("UPDATE `applications` SET `pubkey`=? WHERE `username`=?",
@ -80,58 +84,67 @@ if __name__ == "__main__":
fetch = DB.safequery("SELECT * FROM `applications` WHERE `username` = ? ", tuple([args.user]))
if int(fetch[0]["status"]) == 1:
try:
Sysctl.make_ssh_usable(args.user, args.sshpubkey)
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}")
print(f"{args.user} updated successfully.")
print(f"'{args.user}'s SSH-Key updated successfully.")
# --> --name
if args.name:
if not lib.validator.checkName(args.name):
print(f"{args.name} is not a valid Name.")
print(f"'{args.name}' is not a valid Name.")
exit(1)
try:
DB.safequery("UPDATE `applications` SET `name` =? WHERE `username` =?", tuple([args.name, args.user]))
except sqlite3.Error as e:
print(f"Couldn't write {args.name} to database: {e}")
print(f"Could not write '{args.name}' to database: {e}")
print(f"'{args.user}'s Name changed to '{args.name}'.")
# --> --email
if args.email:
if not lib.validator.checkEmail(args.email):
print(f"{args.email} is not a valid Mail address!")
print(f"'{args.email}' is not a valid Mail address!")
exit(1)
try:
DB.safequery("UPDATE `applications` SET `email` =? WHERE `username` =?", tuple([args.email]))
except sqlite3.Error as e:
print(f"Couldn't write {args.email} to the database. {e}")
print(f"Could not write '{args.email}' to the database. {e}")
print(f"'{args.user}' Mail changed to '{args.email}'.")
# --> --status
if args.status is not None:
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"Old and new Status matches, didn't change")
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"]]))
except sqlite3.Error as e:
print(f"Couldn't update database entry for {args.user}, didn't touch the system")
print(f"Could not update database entry for '{args.user}', did not touch the system")
exit(1)
try:
Sysctl.remove_user(args.user)
sys_ctl.remove_user()
except lib.UserExceptions.UnknownReturnCode as e:
print(f"Couldn't remove {args.user} from the system, unknown return code: {e}. DB is modified.")
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]))
except sqlite3.Error as e:
print(f"Couldn't update Users status in database")
print(f"Could not update Users status in database")
exit(1)
try:
Sysctl.register(args.user)
Sysctl.lock_user_pw(args.user)
Sysctl.add_to_usergroup(args.user)
Sysctl.make_ssh_usable(args.user, CurrentUser["pubkey"])
sys_ctl.register()
sys_ctl.lock_user_pw()
sys_ctl.add_to_usergroup()
sys_ctl.make_ssh_usable(CurrentUser["pubkey"])
except lib.UserExceptions.UserExistsAlready as UEA:
print(f"Somehow the user exists already on the system! {UEA}")
exit(1)
@ -139,11 +152,11 @@ if __name__ == "__main__":
print(f"Unknown return code: {URC}")
exit(1)
except lib.UserExceptions.SSHDirUncreatable as SDU:
print(f"Couldnt create ssh directory for {args.user}, exception: {SDU}")
print(f"Could not create ssh directory for {args.user}, exception: {SDU}")
exit(1)
except lib.UserExceptions.ModifyFilesystem as MFS:
pass
print(f"Success! {args.user}")
print(f"Successfully changed '{args.user}'s status to 1 and created on the system.")
exit(0)
except KeyboardInterrupt as e:
pass

Loading…
Cancel
Save