first commit
This commit is contained in:
BIN
scripts/__pycache__/create_admin.cpython-312.pyc
Normal file
BIN
scripts/__pycache__/create_admin.cpython-312.pyc
Normal file
Binary file not shown.
BIN
scripts/__pycache__/create_admin.cpython-313.pyc
Normal file
BIN
scripts/__pycache__/create_admin.cpython-313.pyc
Normal file
Binary file not shown.
59
scripts/create_admin.py
Normal file
59
scripts/create_admin.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import sys
|
||||
from getpass import getpass
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
|
||||
|
||||
import psycopg2
|
||||
from services.auth_service import hash_password
|
||||
|
||||
|
||||
def get_connection():
|
||||
return psycopg2.connect(
|
||||
host=os.getenv("DB_HOST", "localhost"),
|
||||
port=os.getenv("DB_PORT", 5432),
|
||||
dbname=os.getenv("DB_NAME", "wfl_db"),
|
||||
user=os.getenv("DB_USER", "postgres"),
|
||||
password=os.getenv("DB_PASSWORD", "159753"),
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
username = input("Username: ").strip()
|
||||
password = getpass("Password: ").strip()
|
||||
|
||||
if not username:
|
||||
print("Username is required")
|
||||
return
|
||||
|
||||
if not password:
|
||||
print("Password is required")
|
||||
return
|
||||
|
||||
password_hash = hash_password(password)
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO admin_users (username, password_hash, is_active)
|
||||
VALUES (%s, %s, TRUE)
|
||||
ON CONFLICT (username) DO NOTHING
|
||||
RETURNING id;
|
||||
""",
|
||||
(username, password_hash),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
|
||||
if row:
|
||||
print(f"User created: {username} (id={row[0]})")
|
||||
else:
|
||||
print(f"User '{username}' already exists")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user