aboutsummaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-02-10 11:08:57 +0300
committerFrederick Muriuki Muriithi2023-02-10 11:13:30 +0300
commit89f6b81a4d44448ca4f7b29bc6c09fd6205e11f4 (patch)
tree33648725fff70bcfbdb15a416370e41c127cb74a /main.py
parent4230afacfcee6c5e8a92a7133732adc9ee61026b (diff)
downloadgenenetwork3-89f6b81a4d44448ca4f7b29bc6c09fd6205e11f4.tar.gz
auth: CLI: Provide a way to set an existing user as a system admin
Provide a way to set a user as a system administrator via CLI. This method was chosen rather that using the WebUI since most users will not have access to the CLI. It also means that assigning system administration privileges will not be an accident, since one has to actually run the command manually on the CLI.
Diffstat (limited to 'main.py')
-rw-r--r--main.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/main.py b/main.py
index 1c810a1..fc12755 100644
--- a/main.py
+++ b/main.py
@@ -1,5 +1,8 @@
"""Main entry point for project"""
+import sys
+import uuid
import json
+import click
from math import ceil
from datetime import datetime
@@ -84,6 +87,26 @@ def init_dev_clients():
with db.connection(app.config["AUTH_DB"]) as conn, db.cursor(conn) as cursor:
cursor.executemany(dev_clients_query, dev_clients)
+
+@app.cli.command()
+@click.argument("user_id", type=click.UUID)
+def assign_system_admin(user_id: uuid.UUID):
+ dburi = app.config["AUTH_DB"]
+ with db.connection(dburi) as conn, db.cursor(conn) as cursor:
+ cursor.execute("SELECT * FROM users WHERE user_id=?",
+ (str(user_id),))
+ row = cursor.fetchone()
+ if row:
+ cursor.execute(
+ "SELECT * FROM roles WHERE role_name='system-administrator'")
+ admin_role = cursor.fetchone()
+ cursor.execute("INSERT INTO user_roles VALUES (?,?)",
+ (str(user_id), admin_role["role_id"]))
+ return 0
+ print(f"ERROR: Could not find user with ID {user_id}",
+ file=sys.stderr)
+ sys.exit(1)
+
##### END: CLI Commands #####
if __name__ == '__main__':