diff options
author | Frederick Muriuki Muriithi | 2023-02-10 11:08:57 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-02-10 11:13:30 +0300 |
commit | 89f6b81a4d44448ca4f7b29bc6c09fd6205e11f4 (patch) | |
tree | 33648725fff70bcfbdb15a416370e41c127cb74a | |
parent | 4230afacfcee6c5e8a92a7133732adc9ee61026b (diff) | |
download | genenetwork3-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.
-rw-r--r-- | main.py | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -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__': |