test2 / modules /auth.py
AIdeaText's picture
Update modules/auth.py
2b79f7a verified
raw
history blame
1.26 kB
# /modules/auth.py
import streamlit as st
import pymssql
from app import get_db_connection
# Funci贸n para registrar un nuevo usuario
def register_user(username, password, role):
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO Users (Username, Password, Role) VALUES (%s, %s, %s)", (username, password, role))
conn.commit()
return True
except pymssql.IntegrityError:
return False
finally:
cursor.close()
conn.close()
# Funci贸n para autenticar un usuario
def authenticate_user(username, password):
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute("SELECT UserID, Role FROM Users WHERE Username = %s AND Password = %s", (username, password))
user = cursor.fetchone()
return bool(user)
finally:
cursor.close()
conn.close()
# Funci贸n para obtener el rol del usuario
def get_user_role(username):
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute("SELECT Role FROM Users WHERE Username = %s", (username,))
role = cursor.fetchone()
return role[0] if role else None
finally:
cursor.close()
conn.close()