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.
pull/4/head
Darksider3 4 years ago
parent 452204a5e9
commit 84cc83db3c

@ -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)

@ -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":

@ -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:

@ -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)

@ -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)

Loading…
Cancel
Save