aboutsummaryrefslogtreecommitdiff
path: root/gn_auth
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-11-22 11:43:36 +0300
committerFrederick Muriuki Muriithi2023-11-22 11:43:36 +0300
commitf3bec4784af4715465fe63fd2cb9b8a0ca026d3e (patch)
tree04d258cd92acea12723d6eb9b233930ef964a81b /gn_auth
parentf680b53ab0c1d4e12b141662e2e26506efd06509 (diff)
downloadgn-auth-f3bec4784af4715465fe63fd2cb9b8a0ca026d3e.tar.gz
Move system admin creation
Make the system admin creation code part of the core system, and simply call it from the script(s). This will help with maintenance, since the changes are done in a single place only.
Diffstat (limited to 'gn_auth')
-rw-r--r--gn_auth/auth/authorisation/users/admin/models.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/gn_auth/auth/authorisation/users/admin/models.py b/gn_auth/auth/authorisation/users/admin/models.py
new file mode 100644
index 0000000..36f3c09
--- /dev/null
+++ b/gn_auth/auth/authorisation/users/admin/models.py
@@ -0,0 +1,23 @@
+"""Major function for handling admin users."""
+from gn_auth.auth.db import sqlite3 as db
+from gn_auth.auth.authentication.users import User
+
+def make_sys_admin(cursor: db.DbCursor, user: User) -> User:
+ """Make a given user into an system admin."""
+ cursor.execute(
+ "SELECT * FROM roles WHERE role_name='system-administrator'")
+ admin_role = cursor.fetchone()
+ cursor.execute(
+ "SELECT * FROM resources AS r "
+ "INNER JOIN resource_categories AS rc "
+ "ON r.resource_category_id=rc.resource_category_id "
+ "WHERE resource_category_key='system'")
+ the_system = cursor.fetchone()
+ cursor.execute(
+ "INSERT INTO user_roles VALUES (:user_id, :role_id, :resource_id)",
+ {
+ "user_id": str(user.user_id),
+ "role_id": admin_role["role_id"],
+ "resource_id": the_system["resource_id"]
+ })
+ return user