semantics

This commit is contained in:
2026-03-27 21:27:31 +03:00
parent 7c85552132
commit 2ed66bfebc
182 changed files with 21186 additions and 10254 deletions

View File

@@ -1,12 +1,12 @@
# [DEF:backend.src.scripts.create_admin:Module]
# [DEF:CreateAdminScript:Module]
#
# @COMPLEXITY: 3
# @SEMANTICS: admin, setup, user, auth, cli
# @PURPOSE: CLI tool for creating the initial admin user.
# @LAYER: Scripts
# @RELATION: USES -> backend.src.core.auth.security
# @RELATION: USES -> backend.src.core.database
# @RELATION: USES -> backend.src.models.auth
# @RELATION: USES -> [AuthSecurityModule]
# @RELATION: USES -> [DatabaseModule]
# @RELATION: USES -> [AuthModels]
#
# @INVARIANT: Admin user must have the "Admin" role.
@@ -24,6 +24,7 @@ from src.models.auth import User, Role
from src.core.logger import logger, belief_scope
# [/SECTION]
# [DEF:create_admin:Function]
# @PURPOSE: Creates an admin user and necessary roles/permissions.
# @PRE: username and password provided via CLI.
@@ -36,7 +37,9 @@ def create_admin(username, password, email=None):
with belief_scope("create_admin"):
db = AuthSessionLocal()
try:
normalized_email = email.strip() if isinstance(email, str) and email.strip() else None
normalized_email = (
email.strip() if isinstance(email, str) and email.strip() else None
)
# 1. Ensure Admin role exists
admin_role = db.query(Role).filter(Role.name == "Admin").first()
@@ -60,7 +63,7 @@ def create_admin(username, password, email=None):
email=normalized_email,
password_hash=get_password_hash(password),
auth_source="LOCAL",
is_active=True
is_active=True,
)
new_user.roles.append(admin_role)
db.add(new_user)
@@ -74,6 +77,8 @@ def create_admin(username, password, email=None):
raise
finally:
db.close()
# [/DEF:create_admin:Function]
if __name__ == "__main__":
@@ -91,4 +96,4 @@ if __name__ == "__main__":
except Exception:
sys.exit(1)
# [/DEF:backend.src.scripts.create_admin:Module]
# [/DEF:CreateAdminScript:Module]