Update modules/auth.py
Browse files- modules/auth.py +19 -28
modules/auth.py
CHANGED
|
@@ -1,51 +1,42 @@
|
|
| 1 |
# /modules/auth.py
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
# Funci贸n para cargar usuarios
|
| 7 |
-
def load_users():
|
| 8 |
-
if not os.path.exists("users.csv"):
|
| 9 |
-
df = pd.DataFrame(columns=["username", "password", "role"])
|
| 10 |
-
df.to_csv("users.csv", index=False)
|
| 11 |
-
return pd.read_csv("users.csv")
|
| 12 |
-
|
| 13 |
-
# Funci贸n para guardar usuarios
|
| 14 |
-
def save_users(df):
|
| 15 |
-
df.to_csv("users.csv", index=False)
|
| 16 |
|
| 17 |
# Funci贸n para registrar un nuevo usuario
|
| 18 |
def register_user(username, password, role):
|
| 19 |
conn = get_db_connection()
|
| 20 |
cursor = conn.cursor()
|
| 21 |
try:
|
| 22 |
-
cursor.execute("INSERT INTO Users (Username, Role) VALUES (
|
| 23 |
-
(username, role))
|
| 24 |
conn.commit()
|
| 25 |
return True
|
| 26 |
-
except
|
| 27 |
return False
|
| 28 |
finally:
|
|
|
|
| 29 |
conn.close()
|
| 30 |
|
| 31 |
# Funci贸n para autenticar un usuario
|
| 32 |
def authenticate_user(username, password):
|
| 33 |
conn = get_db_connection()
|
| 34 |
cursor = conn.cursor()
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return False
|
| 43 |
|
| 44 |
# Funci贸n para obtener el rol del usuario
|
| 45 |
def get_user_role(username):
|
| 46 |
conn = get_db_connection()
|
| 47 |
cursor = conn.cursor()
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# /modules/auth.py
|
| 2 |
import streamlit as st
|
| 3 |
+
import pymssql
|
| 4 |
+
from app import get_db_connection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Funci贸n para registrar un nuevo usuario
|
| 7 |
def register_user(username, password, role):
|
| 8 |
conn = get_db_connection()
|
| 9 |
cursor = conn.cursor()
|
| 10 |
try:
|
| 11 |
+
cursor.execute("INSERT INTO Users (Username, Password, Role) VALUES (%s, %s, %s)", (username, password, role))
|
|
|
|
| 12 |
conn.commit()
|
| 13 |
return True
|
| 14 |
+
except pymssql.IntegrityError:
|
| 15 |
return False
|
| 16 |
finally:
|
| 17 |
+
cursor.close()
|
| 18 |
conn.close()
|
| 19 |
|
| 20 |
# Funci贸n para autenticar un usuario
|
| 21 |
def authenticate_user(username, password):
|
| 22 |
conn = get_db_connection()
|
| 23 |
cursor = conn.cursor()
|
| 24 |
+
try:
|
| 25 |
+
cursor.execute("SELECT UserID, Role FROM Users WHERE Username = %s AND Password = %s", (username, password))
|
| 26 |
+
user = cursor.fetchone()
|
| 27 |
+
return bool(user)
|
| 28 |
+
finally:
|
| 29 |
+
cursor.close()
|
| 30 |
+
conn.close()
|
|
|
|
| 31 |
|
| 32 |
# Funci贸n para obtener el rol del usuario
|
| 33 |
def get_user_role(username):
|
| 34 |
conn = get_db_connection()
|
| 35 |
cursor = conn.cursor()
|
| 36 |
+
try:
|
| 37 |
+
cursor.execute("SELECT Role FROM Users WHERE Username = %s", (username,))
|
| 38 |
+
role = cursor.fetchone()
|
| 39 |
+
return role[0] if role else None
|
| 40 |
+
finally:
|
| 41 |
+
cursor.close()
|
| 42 |
+
conn.close()
|