Spaces:
Runtime error
Runtime error
Create auth.py
Browse files
auth.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import os
|
| 3 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 4 |
+
|
| 5 |
+
# Path to the SQLite database file in persistent storage
|
| 6 |
+
DB_PATH = "/data/users.db"
|
| 7 |
+
|
| 8 |
+
def get_db_connection():
|
| 9 |
+
"""Establishes a connection to the SQLite database."""
|
| 10 |
+
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
| 11 |
+
conn = sqlite3.connect(DB_PATH)
|
| 12 |
+
conn.row_factory = sqlite3.Row
|
| 13 |
+
return conn
|
| 14 |
+
|
| 15 |
+
def create_user_table():
|
| 16 |
+
"""Creates the users table if it doesn't already exist."""
|
| 17 |
+
conn = get_db_connection()
|
| 18 |
+
cursor = conn.cursor()
|
| 19 |
+
cursor.execute('''
|
| 20 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 21 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 22 |
+
email TEXT UNIQUE NOT NULL,
|
| 23 |
+
enrollment_no TEXT UNIQUE,
|
| 24 |
+
password_hash TEXT NOT NULL,
|
| 25 |
+
role TEXT NOT NULL CHECK(role IN ('student', 'faculty')),
|
| 26 |
+
enrolled_by INTEGER,
|
| 27 |
+
FOREIGN KEY(enrolled_by) REFERENCES users(id)
|
| 28 |
+
)
|
| 29 |
+
''')
|
| 30 |
+
conn.commit()
|
| 31 |
+
|
| 32 |
+
# Add a default faculty user if one doesn't exist for initial setup
|
| 33 |
+
cursor.execute("SELECT * FROM users WHERE role = 'faculty'")
|
| 34 |
+
if cursor.fetchone() is None:
|
| 35 |
+
print("Creating default faculty user...")
|
| 36 |
+
cursor.execute('''
|
| 37 |
+
INSERT INTO users (email, password_hash, role)
|
| 38 |
+
VALUES (?,?,?)
|
| 39 |
+
''', ('faculty@ggits.net', generate_password_hash('ggits@123'), 'faculty'))
|
| 40 |
+
conn.commit()
|
| 41 |
+
print("Default faculty user created: faculty@ggits.net / ggits@123")
|
| 42 |
+
|
| 43 |
+
conn.close()
|
| 44 |
+
|
| 45 |
+
def add_student(email, enrollment_no, password, faculty_id):
|
| 46 |
+
"""Adds a new student to the database, enrolled by a faculty member."""
|
| 47 |
+
if not email.endswith('@ggits.net'):
|
| 48 |
+
return "Error: Email must be a @ggits.net address."
|
| 49 |
+
|
| 50 |
+
conn = get_db_connection()
|
| 51 |
+
cursor = conn.cursor()
|
| 52 |
+
try:
|
| 53 |
+
cursor.execute('''
|
| 54 |
+
INSERT INTO users (email, enrollment_no, password_hash, role, enrolled_by)
|
| 55 |
+
VALUES (?,?,?,?,?)
|
| 56 |
+
''', (email, enrollment_no, generate_password_hash(password), 'student', faculty_id))
|
| 57 |
+
conn.commit()
|
| 58 |
+
return f"Student {email} enrolled successfully."
|
| 59 |
+
except sqlite3.IntegrityError as e:
|
| 60 |
+
if 'email' in str(e):
|
| 61 |
+
return "Error: A user with this email already exists."
|
| 62 |
+
if 'enrollment_no' in str(e):
|
| 63 |
+
return "Error: A user with this enrollment number already exists."
|
| 64 |
+
return "Error: Could not add student due to a database constraint."
|
| 65 |
+
finally:
|
| 66 |
+
conn.close()
|
| 67 |
+
|
| 68 |
+
def verify_user(identifier, password):
|
| 69 |
+
"""
|
| 70 |
+
Verifies a user's credentials. The identifier can be an email or enrollment number.
|
| 71 |
+
Returns the user's data if successful, otherwise None.
|
| 72 |
+
"""
|
| 73 |
+
conn = get_db_connection()
|
| 74 |
+
cursor = conn.cursor()
|
| 75 |
+
|
| 76 |
+
# Check if identifier is email or enrollment number
|
| 77 |
+
if '@' in identifier:
|
| 78 |
+
cursor.execute("SELECT * FROM users WHERE email =?", (identifier,))
|
| 79 |
+
else:
|
| 80 |
+
cursor.execute("SELECT * FROM users WHERE enrollment_no =?", (identifier,))
|
| 81 |
+
|
| 82 |
+
user = cursor.fetchone()
|
| 83 |
+
conn.close()
|
| 84 |
+
|
| 85 |
+
if user and check_password_hash(user['password_hash'], password):
|
| 86 |
+
return dict(user)
|
| 87 |
+
return None
|