From 84cc83db3c4bd1e7afcc0f50c1518a77a7eb880e Mon Sep 17 00:00:00 2001 From: Darksider3 Date: Tue, 22 Oct 2019 15:47:17 +0200 Subject: [PATCH] ListUsers: UAP and APP renamed, function updates /home/users.txt On every register() call the System rewrites the /home/users.txt to reflect currently active users. Will fall apart when something unexpected happened, but that's @helix responsibility. --- private/Backup.py | 2 +- private/Import.py | 2 +- private/ListUsers.py | 21 ++++++++++----------- private/editUsers.py | 2 +- private/lib/System.py | 13 ++++++++++++- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/private/Backup.py b/private/Backup.py index bd324e2..97e10f6 100755 --- a/private/Backup.py +++ b/private/Backup.py @@ -53,7 +53,7 @@ class Backup: if __name__ == "__main__": try: - L = ListUsers.ListUsers(config['DEFAULT']['applications_db'], uap=args.unapproved, app=args.approved) + L = ListUsers.ListUsers(config['DEFAULT']['applications_db'], unapproved=args.unapproved, approved=args.approved) fetch = L.getFetch() B = Backup(args.file) B.BackupToFile(fetch) diff --git a/private/Import.py b/private/Import.py index 5e266ba..28f15c7 100755 --- a/private/Import.py +++ b/private/Import.py @@ -37,7 +37,7 @@ def ImportFromFile(fname: str, db: str, userids: tuple = tuple([])): exit(0) import lib.sqlitedb import lib.System - sysctl = lib.System.System() + sysctl = lib.System.System(db) reader = csv.DictReader(f) # @TODO csv.Sniffer to compare? When yes, give force-accept option for row in reader: if row["status"] == "1": diff --git a/private/ListUsers.py b/private/ListUsers.py index 2b96b7d..e7c5e7b 100755 --- a/private/ListUsers.py +++ b/private/ListUsers.py @@ -4,23 +4,16 @@ from lib.sqlitedb import SQLitedb import configparser import lib.uis.default as default_cmd # Follows -u, -a, -f flags -default_cmd.argparser.description += " - Lists Users from the Tilde database." -default_cmd.argparser.add_argument('--list', default=False, action="store_true", - help='Output a newline seperated list of users', required=False) -args = default_cmd.argparser.parse_args() -config = configparser.ConfigParser() -config.read(args.config) - class ListUsers: db = None usersFetch = None - def __init__(self, db: str, uap: bool = False, app: bool = True): + def __init__(self, db: str, unapproved: bool = False, approved: bool = True): self.db = SQLitedb(db) - if uap: # only unapproved users + if unapproved: # only unapproved users query = "SELECT * FROM `applications` WHERE `status` = '0'" - elif app: # Approved users + elif approved: # Approved users query = "SELECT * FROM `applications` WHERE `status` = '1'" else: # All users query = "SELECT * FROM `applications`" @@ -69,9 +62,15 @@ print(t.draw()) )) """ if __name__ == "__main__": + default_cmd.argparser.description += " - Lists Users from the Tilde database." + default_cmd.argparser.add_argument('--list', default=False, action="store_true", + help='Output a newline seperated list of users', required=False) + args = default_cmd.argparser.parse_args() + config = configparser.ConfigParser() + config.read(args.config) try: ret = "" - L = ListUsers(config['DEFAULT']['applications_db'], uap=args.unapproved, app=args.approved) + L = ListUsers(config['DEFAULT']['applications_db'], unapproved=args.unapproved, approved=args.approved) if args.list: ret = L.outputaslist() else: diff --git a/private/editUsers.py b/private/editUsers.py index 36bf62e..76e0d6f 100755 --- a/private/editUsers.py +++ b/private/editUsers.py @@ -47,7 +47,7 @@ if __name__ == "__main__": print(f"User {args.user} doesn't exist in the database.") exit(1) DB = lib.sqlitedb.SQLitedb(db) - Sysctl = lib.System.System() + Sysctl = lib.System.System(db) if not DB: print("Couldn't establish connection to database") exit(1) diff --git a/private/lib/System.py b/private/lib/System.py index 17de845..fa8dcd6 100644 --- a/private/lib/System.py +++ b/private/lib/System.py @@ -10,8 +10,9 @@ class System: dry = False create_command = [] home = "" + db = None - def __init__(self, dryrun: bool = False, home: str = "/home/"): + def __init__(self, db: str, dryrun: bool = False, home: str = "/home/"): """Creates an objects. Can set dry run. :param dryrun: Run all command in a dry-run? When enabled, doesn't make any changes to the system (defaults to @@ -21,6 +22,7 @@ class System: :type home: str """ + self.db = db self.dry = dryrun if not home.endswith("/"): home += "/" @@ -47,6 +49,7 @@ class System: rt = subprocess.call(cc) if rt != 0: raise lib.UserExceptions.UserExistsAlready(f"User {username} exists already") + self._createUserIndex() return True def unregister(self, username: str): @@ -184,8 +187,16 @@ class System: if ret != 0 and ret != 6: raise lib.UserExceptions.UnknownReturnCode( f"Could not delete user with command {cc}. Return code: {ret}") + self._createUserIndex() return True + def _createUserIndex(self, index_file: str = "/home/users.txt"): + import ListUsers + L = ListUsers.ListUsers(self.db, unapproved=False, approved=True) + user_list = L.outputaslist() + with open(index_file, "w") as f: + print(user_list, file=f) + def AIO(username, pubkey, group="tilde"): syst = System(dryrun=False)