forked from tilde/ssh-reg
Compare commits
No commits in common. 'dev' and 'master' have entirely different histories.
@ -1,41 +1,120 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
|
import sqlite3 # sqlite3.Row-Object
|
||||||
|
from typing import List # Typing support!
|
||||||
|
|
||||||
import lib.uis.default as default_cmd # Follows -u, -a, -f flags
|
import lib.uis.default as default_cmd # Follows -u, -a, -f flags
|
||||||
from lib.ListUsers import ListUsers
|
from lib.sqlitedb import SQLiteDB
|
||||||
|
|
||||||
|
|
||||||
|
class ListUsers:
|
||||||
|
db = None
|
||||||
|
usersFetch = None
|
||||||
|
|
||||||
|
def __init__(self, db: str, unapproved: bool = False, approved: bool = True, single_user: str = None):
|
||||||
|
"""Constructs ListUsers
|
||||||
|
|
||||||
|
:param db: Database to access
|
||||||
|
:type db: str
|
||||||
|
:param unapproved: only List unapproved users
|
||||||
|
:type unapproved: bool
|
||||||
|
:param approved: only list approved users
|
||||||
|
:type approved: bool
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.db = SQLiteDB(db)
|
||||||
|
if unapproved: # only unapproved users
|
||||||
|
query = "SELECT * FROM `applications` WHERE `status` = '0'"
|
||||||
|
elif approved: # Approved users
|
||||||
|
query = "SELECT * FROM `applications` WHERE `status` = '1'"
|
||||||
|
else: # All users
|
||||||
|
query = "SELECT * FROM `applications`"
|
||||||
|
self.usersFetch = self.db.query(query)
|
||||||
|
if single_user is not None:
|
||||||
|
query = "SELECT * FROM `applications` WHERE `username` = ?"
|
||||||
|
self.usersFetch = self.db.safequery(query, tuple([single_user]))
|
||||||
|
|
||||||
|
def output_as_list(self) -> str:
|
||||||
|
"""Generates a string with one (approved) user per line and one newline at the end
|
||||||
|
|
||||||
|
:rtype: str
|
||||||
|
:return: String consisting with one(activated) user per line
|
||||||
|
"""
|
||||||
|
|
||||||
|
list_str: str = ""
|
||||||
|
query = "SELECT `username` FROM `applications` WHERE `status` = '1' ORDER BY timestamp ASC"
|
||||||
|
self.usersFetch = self.db.query(query)
|
||||||
|
for users in self.usersFetch:
|
||||||
|
list_str += users["username"] + "\n"
|
||||||
|
return list_str
|
||||||
|
|
||||||
|
def prettyPrint(self) -> None:
|
||||||
|
pass # see below why not implemented yet, texttable...
|
||||||
|
|
||||||
|
def get_fetch(self) -> List[sqlite3.Row]:
|
||||||
|
""" Returns a complete fetch done by the lib.sqlitedb-class
|
||||||
|
|
||||||
|
:return: Complete fetchall(). A List[sqlite3.Row] with dict-emulation objects.
|
||||||
|
:rtype: List[sqlite3.Row]
|
||||||
|
"""
|
||||||
|
|
||||||
|
return self.usersFetch
|
||||||
|
|
||||||
|
|
||||||
|
# @TODO MAYBE best solution: https://pypi.org/project/texttable/
|
||||||
|
# examle:
|
||||||
|
"""
|
||||||
|
from texttable import Texttable
|
||||||
|
t = Texttable()
|
||||||
|
t.add_rows([['Name', 'Age'], ['Alice', 24], ['Bob', 19]])
|
||||||
|
print(t.draw())
|
||||||
|
---------------> Results in:
|
||||||
|
|
||||||
|
+-------+-----+
|
||||||
|
| Name | Age |
|
||||||
|
+=======+=====+
|
||||||
|
| Alice | 24 |
|
||||||
|
+-------+-----+
|
||||||
|
| Bob | 19 |
|
||||||
|
+-------+-----+
|
||||||
|
|
||||||
|
for user in fetch:
|
||||||
|
print("ID: {}; Username: \"{}\"; Mail: {}; Name: \"{}\"; Registered: {}; Status: {}".format(
|
||||||
|
user["id"], user["username"], user["email"], user["name"], user["timestamp"], user["status"]
|
||||||
|
))
|
||||||
|
"""
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
default_cmd.argparser.description += " - Lists Users from the Tilde database."
|
default_cmd.argparser.description += " - Lists Users from the Tilde database."
|
||||||
default_cmd.argparser.add_argument('--list-asc', default=False, action="store_true",
|
default_cmd.argparser.add_argument('--list-asc', default=False, action="store_true",
|
||||||
help='Output a newline seperated list of users', required=False, dest="args_asc")
|
help='Output a newline seperated list of users', required=False, dest="args_asc")
|
||||||
default_cmd.argparser.add_argument('--single_user', default=None, type=str,
|
default_cmd.argparser.add_argument('--user', default=None, type=str,
|
||||||
help="Just show a specific single_user by it's name", required=False)
|
help="Just show a specific user by it's name", required=False)
|
||||||
args = default_cmd.argparser.parse_args()
|
args = default_cmd.argparser.parse_args()
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(args.config)
|
config.read(args.config)
|
||||||
|
|
||||||
OUTPUT = ""
|
ret = ""
|
||||||
if args.single_user is not None:
|
if args.user is not None:
|
||||||
L = ListUsers(config['DEFAULT']['applications_db'], unapproved=args.unapproved, approved=args.approved,
|
L = ListUsers(config['DEFAULT']['applications_db'], unapproved=args.unapproved, approved=args.approved,
|
||||||
single_user=args.single_user)
|
single_user=args.user)
|
||||||
else:
|
else:
|
||||||
L = ListUsers(config['DEFAULT']['applications_db'], unapproved=args.unapproved, approved=args.approved)
|
L = ListUsers(config['DEFAULT']['applications_db'], unapproved=args.unapproved, approved=args.approved)
|
||||||
if args.args_asc:
|
if args.args_asc:
|
||||||
OUTPUT = L.output_as_list()
|
ret = L.output_as_list()
|
||||||
else:
|
else:
|
||||||
users = L.get_fetch()
|
fetch = L.get_fetch()
|
||||||
OUTPUT += "ID %-1s| Username %-5s| Mail %-20s| Name %-17s| Registered %-8s | State |\n" % (
|
ret += "ID %-1s| Username %-5s| Mail %-20s| Name %-17s| Registered %-8s | State |\n" % (
|
||||||
" ", " ", " ", " ", " "
|
" ", " ", " ", " ", " "
|
||||||
)
|
)
|
||||||
OUTPUT += 102 * "-" + "\n"
|
ret += 102 * "-" + "\n"
|
||||||
for single_user in users:
|
for user in fetch:
|
||||||
OUTPUT += "%-4i| %-14s| %-25s| %-22s| %-8s | %-5i |\n" % (
|
ret += "%-4i| %-14s| %-25s| %-22s| %-8s | %-5i |\n" % (
|
||||||
single_user["id"], single_user["username"], single_user["email"],
|
user["id"], user["username"], user["email"], user["name"], user["timestamp"], user["status"]
|
||||||
single_user["name"], single_user["timestamp"], single_user["status"]
|
|
||||||
)
|
)
|
||||||
if args.file != "stdout":
|
if args.file != "stdout":
|
||||||
with open(args.file, 'w') as f:
|
with open(args.file, 'w') as f:
|
||||||
print(OUTPUT, file=f)
|
print(ret, file=f)
|
||||||
else:
|
else:
|
||||||
print(OUTPUT)
|
print(ret)
|
||||||
exit(0)
|
exit(0)
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
from lib.sqlitedb import SQLiteDB
|
|
||||||
import sqlite3 # sqlite3.Row-Object
|
|
||||||
from typing import List # Typing support!
|
|
||||||
|
|
||||||
|
|
||||||
class ListUsers:
|
|
||||||
"""
|
|
||||||
List tilde users
|
|
||||||
"""
|
|
||||||
|
|
||||||
db = None
|
|
||||||
users_fetch = None
|
|
||||||
|
|
||||||
def __init__(self, db: str, unapproved: bool = False, approved: bool = True, single_user: str = None):
|
|
||||||
"""Constructs list_users
|
|
||||||
|
|
||||||
:param db: Database to access
|
|
||||||
:type db: str
|
|
||||||
:param unapproved: only List unapproved users
|
|
||||||
:type unapproved: bool
|
|
||||||
:param approved: only list approved users
|
|
||||||
:type approved: bool
|
|
||||||
"""
|
|
||||||
|
|
||||||
self.db = SQLiteDB(db)
|
|
||||||
if unapproved: # only unapproved users
|
|
||||||
query = "SELECT * FROM `applications` WHERE `status` = '0'"
|
|
||||||
elif approved: # Approved users
|
|
||||||
query = "SELECT * FROM `applications` WHERE `status` = '1'"
|
|
||||||
else: # All users
|
|
||||||
query = "SELECT * FROM `applications`"
|
|
||||||
self.users_fetch = self.db.query(query)
|
|
||||||
if single_user is not None:
|
|
||||||
query = "SELECT * FROM `applications` WHERE `username` = ?"
|
|
||||||
self.users_fetch = self.db.safe_query(query, tuple([single_user]))
|
|
||||||
|
|
||||||
def output_as_list(self) -> str:
|
|
||||||
"""Generates a string with one (approved) single_user per line and one newline at the end
|
|
||||||
|
|
||||||
:rtype: str
|
|
||||||
:return: String consisting with one(activated) single_user per line
|
|
||||||
"""
|
|
||||||
|
|
||||||
list_str: str = ""
|
|
||||||
query = "SELECT `username` FROM `applications` WHERE `status` = '1' ORDER BY timestamp ASC"
|
|
||||||
self.users_fetch = self.db.query(query)
|
|
||||||
for user in self.users_fetch:
|
|
||||||
list_str += user["username"] + "\n"
|
|
||||||
return list_str
|
|
||||||
|
|
||||||
def pretty_print(self) -> None:
|
|
||||||
"""
|
|
||||||
pretty-print users
|
|
||||||
:return: None
|
|
||||||
"""
|
|
||||||
pass # see below why not implemented yet, texttable...
|
|
||||||
|
|
||||||
def get_fetch(self) -> List[sqlite3.Row]:
|
|
||||||
""" Returns a complete users done by the lib.sqlitedb-class
|
|
||||||
|
|
||||||
:return: Complete fetchall(). A List[sqlite3.Row] with dict-emulation objects.
|
|
||||||
:rtype: List[sqlite3.Row]
|
|
||||||
"""
|
|
||||||
|
|
||||||
return self.users_fetch
|
|
@ -1,9 +1,6 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import lib.cwd
|
import lib.cwd
|
||||||
|
|
||||||
argparser = argparse.ArgumentParser(description='Tilde administration tools ',
|
argparser = argparse.ArgumentParser(description='Tilde administration tools ', conflict_handler="resolve")
|
||||||
conflict_handler="resolve")
|
|
||||||
argparser.add_argument('-c', '--config', default=lib.cwd.cwd,
|
argparser.add_argument('-c', '--config', default=lib.cwd.cwd,
|
||||||
type=str,
|
type=str, help='Path to configuration file', required=False)
|
||||||
help='Path to configuration file. If not set, we look for it in $TILDE_CONF',
|
|
||||||
required=False)
|
|
||||||
|
Loading…
Reference in New Issue