59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
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() |