43 lines
1.3 KiB
Python
Executable File
43 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sys
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
# Add project root to sys.path
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(BASE_DIR))
|
|
|
|
from app.database import init_db, get_db
|
|
from app.auth import create_user, hash_password
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Manage admin users for Notes App")
|
|
parser.add_argument("--username", "-u", required=True, help="Username")
|
|
parser.add_argument("--password", "-p", required=True, help="Password")
|
|
args = parser.parse_args()
|
|
|
|
init_db()
|
|
|
|
username = args.username.strip()
|
|
password = args.password
|
|
|
|
pwd_hash, salt = hash_password(password)
|
|
|
|
with get_db() as conn:
|
|
existing = conn.execute("SELECT id FROM users WHERE username = ?", (username,)).fetchone()
|
|
if existing:
|
|
conn.execute(
|
|
"UPDATE users SET password_hash = ?, salt = ? WHERE id = ?",
|
|
(pwd_hash, salt, existing["id"])
|
|
)
|
|
print(f"Password updated successfully for existing user '{username}'.")
|
|
else:
|
|
conn.execute(
|
|
"INSERT INTO users (username, password_hash, salt) VALUES (?, ?, ?)",
|
|
(username, pwd_hash, salt)
|
|
)
|
|
print(f"User '{username}' created successfully.")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|