aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-11-22 12:00:39 +0300
committerFrederick Muriuki Muriithi2023-11-22 12:00:39 +0300
commit410d797535fc9a9f432c44a7d6c786fd508066c5 (patch)
treec207abff8300b48ce049343719fa00198aa80341
parentf3bec4784af4715465fe63fd2cb9b8a0ca026d3e (diff)
downloadgn-auth-410d797535fc9a9f432c44a7d6c786fd508066c5.tar.gz
Make existing user admin using core system functions
Use the core system functions to both fetch the user and make them into a system admin, rather than fetching with raw queries. This way, if the way the users are fetched, or made into an admin, changes, we do not need to update the scripts for most part.
-rw-r--r--main.py24
1 files changed, 10 insertions, 14 deletions
diff --git a/main.py b/main.py
index 7f3b256..ae4b4a1 100644
--- a/main.py
+++ b/main.py
@@ -14,7 +14,10 @@ from gn_auth import migrations
from gn_auth import create_app
from gn_auth.auth.db import sqlite3 as db
-from gn_auth.auth.authentication.users import hash_password
+from gn_auth.auth.authentication.users import user_by_id, hash_password
+
+from gn_auth.auth.authorisation.errors import NotFoundError
+from gn_auth.auth.authorisation.users.admin.models import make_sys_admin
from scripts import register_sys_admin as rsysadm# type: ignore[import]
from scripts import migrate_existing_data as med# type: ignore[import]
@@ -96,20 +99,13 @@ def init_dev_clients():
@click.argument("user_id", type=click.UUID)
def assign_system_admin(user_id: uuid.UUID):
"""Assign user with ID `user_id` administrator role."""
- 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"]))
+ try:
+ dburi = app.config["AUTH_DB"]
+ with db.connection(dburi) as conn, db.cursor(conn) as cursor:
+ make_sys_admin(cursor, user_by_id(conn, user_id))
return 0
- print(f"ERROR: Could not find user with ID {user_id}",
- file=sys.stderr)
+ except NotFoundError as nfe:
+ print(nfe, file=sys.stderr)
sys.exit(1)
@app.cli.command()