Deploy Docker: scripts\create_user.py
Browse files- scripts//create_user.py +53 -0
scripts//create_user.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
import argparse
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
# Configura o caminho para importar módulos da app
|
| 8 |
+
project_root = Path(__file__).resolve().parent.parent
|
| 9 |
+
os.chdir(project_root)
|
| 10 |
+
sys.path.append(str(project_root))
|
| 11 |
+
|
| 12 |
+
from app.db.database import SessionLocal
|
| 13 |
+
from app.db.models import User
|
| 14 |
+
from app.core.security import get_password_hash
|
| 15 |
+
|
| 16 |
+
def main():
|
| 17 |
+
parser = argparse.ArgumentParser(description="Criar usuário no banco de dados")
|
| 18 |
+
parser.add_argument("username", help="Nome do usuário")
|
| 19 |
+
parser.add_argument("password", help="Senha")
|
| 20 |
+
parser.add_argument("scopes", help="Escopos separados por vírgula (ex: sistemas,farmaceutico)")
|
| 21 |
+
|
| 22 |
+
args = parser.parse_args()
|
| 23 |
+
|
| 24 |
+
db = SessionLocal()
|
| 25 |
+
try:
|
| 26 |
+
# Verifica se usuário já existe
|
| 27 |
+
existing_user = db.query(User).filter(User.username == args.username).first()
|
| 28 |
+
if existing_user:
|
| 29 |
+
print(f"❌ Erro: Usuário '{args.username}' já existe.")
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
# Cria novo usuário
|
| 33 |
+
new_user = User(
|
| 34 |
+
username=args.username,
|
| 35 |
+
hashed_password=get_password_hash(args.password),
|
| 36 |
+
scopes=args.scopes,
|
| 37 |
+
is_active=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
db.add(new_user)
|
| 41 |
+
db.commit()
|
| 42 |
+
db.refresh(new_user)
|
| 43 |
+
|
| 44 |
+
print(f"✅ Usuário '{new_user.username}' criado com sucesso!")
|
| 45 |
+
print(f" Escopos: {new_user.scopes}")
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"❌ Erro ao criar usuário: {e}")
|
| 49 |
+
finally:
|
| 50 |
+
db.close()
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
main()
|