Spaces:
Runtime error
Runtime error
Fix on auth password
Browse files- src/crud/users.py +62 -36
src/crud/users.py
CHANGED
@@ -5,56 +5,82 @@ from sqlalchemy.orm import Session
|
|
5 |
from fastapi import HTTPException, status
|
6 |
|
7 |
from src import models
|
8 |
-
from src.
|
9 |
-
|
10 |
-
def get_user_by_id(db: Session, user_id: int) -> models.User | None:
|
11 |
-
"""Fetches a user by their primary key ID."""
|
12 |
-
return db.query(models.User).filter(models.User.id == user_id).first()
|
13 |
|
14 |
def get_user_by_username(db: Session, username: str) -> models.User | None:
|
15 |
-
"""
|
|
|
|
|
16 |
return db.query(models.User).filter(models.User.username == username).first()
|
17 |
|
18 |
-
def
|
19 |
-
"""
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
def create_user(db: Session,
|
23 |
-
"""
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
raise HTTPException(
|
26 |
-
status_code=status.
|
27 |
-
detail=f"Username '{
|
28 |
)
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
hashed_password=hashed_password,
|
33 |
-
role=
|
34 |
-
department=
|
35 |
-
tag_id=
|
|
|
36 |
)
|
37 |
-
|
|
|
38 |
db.commit()
|
39 |
-
db.refresh(
|
40 |
-
|
|
|
41 |
|
42 |
-
def
|
43 |
-
"""
|
44 |
-
|
|
|
|
|
45 |
if not db_user:
|
46 |
-
raise HTTPException(
|
47 |
-
|
48 |
-
|
49 |
-
if existing_tag_user and existing_tag_user.username != username:
|
50 |
-
raise HTTPException(
|
51 |
-
status_code=status.HTTP_409_CONFLICT,
|
52 |
-
detail=f"Tag ID '{tag_id}' is already assigned to another user."
|
53 |
)
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
db.commit()
|
57 |
db.refresh(db_user)
|
|
|
58 |
return db_user
|
59 |
|
60 |
def delete_user(db: Session, username_to_delete: str, current_admin: models.User) -> models.User:
|
@@ -90,4 +116,4 @@ def delete_user(db: Session, username_to_delete: str, current_admin: models.User
|
|
90 |
db.delete(user_to_delete)
|
91 |
db.commit()
|
92 |
|
93 |
-
return user_to_delete
|
|
|
5 |
from fastapi import HTTPException, status
|
6 |
|
7 |
from src import models
|
8 |
+
from src.crud import hash_password # Import from crud module instead of auth
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def get_user_by_username(db: Session, username: str) -> models.User | None:
|
11 |
+
"""
|
12 |
+
Retrieves a user by their username.
|
13 |
+
"""
|
14 |
return db.query(models.User).filter(models.User.username == username).first()
|
15 |
|
16 |
+
def get_user_by_id(db: Session, user_id: int) -> models.User | None:
|
17 |
+
"""
|
18 |
+
Retrieves a user by their ID.
|
19 |
+
"""
|
20 |
+
return db.query(models.User).filter(models.User.id == user_id).first()
|
21 |
+
|
22 |
+
def get_all_users(db: Session, skip: int = 0, limit: int = 100) -> list[models.User]:
|
23 |
+
"""
|
24 |
+
Retrieves all users with pagination.
|
25 |
+
"""
|
26 |
+
return db.query(models.User).offset(skip).limit(limit).all()
|
27 |
|
28 |
+
def create_user(db: Session, user_data: models.UserCreate) -> models.User:
|
29 |
+
"""
|
30 |
+
Creates a new user account.
|
31 |
+
"""
|
32 |
+
# Check if username already exists
|
33 |
+
existing_user = get_user_by_username(db, user_data.username)
|
34 |
+
if existing_user:
|
35 |
raise HTTPException(
|
36 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
37 |
+
detail=f"Username '{user_data.username}' is already registered."
|
38 |
)
|
39 |
+
|
40 |
+
# Hash the password
|
41 |
+
hashed_password = hash_password(user_data.password) # Use the imported function
|
42 |
+
|
43 |
+
# Create new user
|
44 |
+
db_user = models.User(
|
45 |
+
username=user_data.username,
|
46 |
+
name=user_data.name,
|
47 |
hashed_password=hashed_password,
|
48 |
+
role=user_data.role,
|
49 |
+
department=user_data.department,
|
50 |
+
tag_id=user_data.tag_id,
|
51 |
+
is_active=user_data.is_active if user_data.is_active is not None else True
|
52 |
)
|
53 |
+
|
54 |
+
db.add(db_user)
|
55 |
db.commit()
|
56 |
+
db.refresh(db_user)
|
57 |
+
|
58 |
+
return db_user
|
59 |
|
60 |
+
def update_user(db: Session, user_id: int, user_update: models.UserCreate) -> models.User:
|
61 |
+
"""
|
62 |
+
Updates an existing user.
|
63 |
+
"""
|
64 |
+
db_user = get_user_by_id(db, user_id)
|
65 |
if not db_user:
|
66 |
+
raise HTTPException(
|
67 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
68 |
+
detail=f"User with ID {user_id} not found."
|
|
|
|
|
|
|
|
|
69 |
)
|
70 |
+
|
71 |
+
# Update fields
|
72 |
+
db_user.username = user_update.username
|
73 |
+
db_user.name = user_update.name
|
74 |
+
if user_update.password: # Only update password if provided
|
75 |
+
db_user.hashed_password = hash_password(user_update.password)
|
76 |
+
db_user.role = user_update.role
|
77 |
+
db_user.department = user_update.department
|
78 |
+
db_user.tag_id = user_update.tag_id
|
79 |
+
db_user.is_active = user_update.is_active if user_update.is_active is not None else True
|
80 |
+
|
81 |
db.commit()
|
82 |
db.refresh(db_user)
|
83 |
+
|
84 |
return db_user
|
85 |
|
86 |
def delete_user(db: Session, username_to_delete: str, current_admin: models.User) -> models.User:
|
|
|
116 |
db.delete(user_to_delete)
|
117 |
db.commit()
|
118 |
|
119 |
+
return user_to_delete
|