Add script to update password for a user
This commit is contained in:
61
scripts/generate_hash.py
Normal file
61
scripts/generate_hash.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import os
|
||||
import sys
|
||||
import psycopg2
|
||||
from urllib.parse import urlparse
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 3:
|
||||
print("Usage: python scripts/generate_hash.py <person_id> <new_password>")
|
||||
sys.exit(1)
|
||||
|
||||
person_id = sys.argv[1]
|
||||
password = sys.argv[2]
|
||||
|
||||
db_url_str = os.environ.get('DATABASE_URL')
|
||||
if not db_url_str:
|
||||
print("Error: DATABASE_URL environment variable not set.")
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
# Generate hash
|
||||
hashed = generate_password_hash(password)
|
||||
|
||||
# Connect to DB
|
||||
db_url = urlparse(db_url_str)
|
||||
conn = psycopg2.connect(
|
||||
database=db_url.path[1:],
|
||||
user=db_url.username,
|
||||
password=db_url.password,
|
||||
host=db_url.hostname,
|
||||
port=db_url.port
|
||||
)
|
||||
cur = conn.cursor()
|
||||
|
||||
# Verify person exists
|
||||
cur.execute("SELECT name FROM person WHERE person_id = %s", (person_id,))
|
||||
person = cur.fetchone()
|
||||
if not person:
|
||||
print(f"Error: No person found with ID {person_id}")
|
||||
sys.exit(1)
|
||||
|
||||
person_name = person[0]
|
||||
|
||||
# Update password
|
||||
cur.execute(
|
||||
"UPDATE person SET password_hash = %s WHERE person_id = %s",
|
||||
(hashed, person_id)
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
print(f"\nSuccessfully updated password for {person_name} (ID: {person_id})")
|
||||
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user