Update modules/database/sql_db.py
Browse files- modules/database/sql_db.py +37 -15
modules/database/sql_db.py
CHANGED
@@ -10,17 +10,24 @@ logger = logging.getLogger(__name__)
|
|
10 |
|
11 |
#########################################
|
12 |
def get_user(username, role=None):
|
|
|
13 |
container = get_container("users")
|
14 |
try:
|
15 |
-
query = f"SELECT * FROM c WHERE c.id =
|
|
|
|
|
16 |
if role:
|
17 |
-
query +=
|
18 |
-
|
|
|
19 |
items = list(container.query_items(
|
20 |
query=query,
|
|
|
21 |
enable_cross_partition_query=True
|
22 |
))
|
|
|
23 |
return items[0] if items else None
|
|
|
24 |
except Exception as e:
|
25 |
logger.error(f"Error al obtener usuario {username}: {str(e)}")
|
26 |
return None
|
@@ -49,31 +56,46 @@ def create_user(username, password, role, additional_info=None):
|
|
49 |
return False
|
50 |
|
51 |
try:
|
52 |
-
# Verificar que
|
53 |
-
|
54 |
-
|
|
|
55 |
return False
|
56 |
-
|
|
|
57 |
user_data = {
|
58 |
'id': username,
|
59 |
-
'password': password,
|
60 |
'role': role,
|
61 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
62 |
-
'additional_info': additional_info or {}
|
63 |
-
'partitionKey': username # Agregar partition key
|
64 |
}
|
65 |
|
66 |
-
# Crear item sin
|
67 |
-
container.create_item(
|
68 |
-
|
69 |
-
|
|
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
except Exception as e:
|
72 |
-
logger.error(f"Error al crear usuario {role}: {str(e)}")
|
73 |
return False
|
74 |
|
75 |
#########################################
|
76 |
def create_student_user(username, password, additional_info=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
return create_user(username, password, 'Estudiante', additional_info)
|
78 |
|
79 |
#########################################
|
|
|
10 |
|
11 |
#########################################
|
12 |
def get_user(username, role=None):
|
13 |
+
"""Obtiene un usuario por su username"""
|
14 |
container = get_container("users")
|
15 |
try:
|
16 |
+
query = f"SELECT * FROM c WHERE c.id = @username"
|
17 |
+
params = [{"name": "@username", "value": username}]
|
18 |
+
|
19 |
if role:
|
20 |
+
query += " AND c.role = @role"
|
21 |
+
params.append({"name": "@role", "value": role})
|
22 |
+
|
23 |
items = list(container.query_items(
|
24 |
query=query,
|
25 |
+
parameters=params,
|
26 |
enable_cross_partition_query=True
|
27 |
))
|
28 |
+
|
29 |
return items[0] if items else None
|
30 |
+
|
31 |
except Exception as e:
|
32 |
logger.error(f"Error al obtener usuario {username}: {str(e)}")
|
33 |
return None
|
|
|
56 |
return False
|
57 |
|
58 |
try:
|
59 |
+
# Verificar que no exista el usuario
|
60 |
+
existing_user = get_user(username)
|
61 |
+
if existing_user:
|
62 |
+
logger.warning(f"Usuario {username} ya existe")
|
63 |
return False
|
64 |
+
|
65 |
+
# Crear documento del usuario
|
66 |
user_data = {
|
67 |
'id': username,
|
68 |
+
'password': password, # Ya viene hasheado de auth.py
|
69 |
'role': role,
|
70 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
71 |
+
'additional_info': additional_info or {}
|
|
|
72 |
}
|
73 |
|
74 |
+
# Crear el item sin referenciar partition key
|
75 |
+
result = container.create_item(
|
76 |
+
body=user_data,
|
77 |
+
enable_cross_partition_query=True
|
78 |
+
)
|
79 |
|
80 |
+
if result:
|
81 |
+
logger.info(f"Usuario {role} creado exitosamente: {username}")
|
82 |
+
return True
|
83 |
+
else:
|
84 |
+
logger.error(f"No se pudo crear el usuario {username}")
|
85 |
+
return False
|
86 |
+
|
87 |
except Exception as e:
|
88 |
+
logger.error(f"Error al crear usuario {role} {username}: {str(e)}")
|
89 |
return False
|
90 |
|
91 |
#########################################
|
92 |
def create_student_user(username, password, additional_info=None):
|
93 |
+
"""Crea un nuevo usuario estudiante"""
|
94 |
+
# Verificar que la contrase帽a est茅 hasheada
|
95 |
+
if not password.startswith('$2b$'):
|
96 |
+
logger.error("Intento de crear usuario con contrase帽a no hasheada")
|
97 |
+
return False
|
98 |
+
|
99 |
return create_user(username, password, 'Estudiante', additional_info)
|
100 |
|
101 |
#########################################
|