Initial commit projet IoT Flask ESP32
This commit is contained in:
51
create_admin.py
Normal file
51
create_admin.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import psycopg2
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
DB_HOST = "localhost"
|
||||
DB_PORT = "5432"
|
||||
DB_NAME = "ecocharge"
|
||||
DB_USER = "ecocharge_user"
|
||||
DB_PASSWORD = "ecocharge_password"
|
||||
|
||||
USERNAME = "admin"
|
||||
PLAIN_PASSWORD = "admin123"
|
||||
|
||||
def main():
|
||||
password_hash = generate_password_hash(PLAIN_PASSWORD)
|
||||
|
||||
conn = psycopg2.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
database=DB_NAME,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD
|
||||
)
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute("SELECT id FROM users WHERE username = %s", (USERNAME,))
|
||||
user = cur.fetchone()
|
||||
|
||||
if user:
|
||||
cur.execute("""
|
||||
UPDATE users
|
||||
SET password = %s, is_admin = TRUE
|
||||
WHERE username = %s
|
||||
""", (password_hash, USERNAME))
|
||||
print(f"Utilisateur '{USERNAME}' mis à jour avec un mot de passe haché.")
|
||||
else:
|
||||
cur.execute("""
|
||||
INSERT INTO users (username, password, is_admin)
|
||||
VALUES (%s, %s, TRUE)
|
||||
""", (USERNAME, password_hash))
|
||||
print(f"Utilisateur '{USERNAME}' créé avec un mot de passe haché.")
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
print("Terminé.")
|
||||
print(f"Nom d'utilisateur : {USERNAME}")
|
||||
print(f"Mot de passe : {PLAIN_PASSWORD}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user