
- Created user model with SQLAlchemy ORM - Implemented authentication with JWT tokens (access and refresh tokens) - Added password hashing with bcrypt - Created API endpoints for registration, login, and user management - Set up Alembic for database migrations - Added health check endpoint - Created role-based access control (standard users and superusers) - Added comprehensive documentation
98 lines
2.6 KiB
Python
98 lines
2.6 KiB
Python
from typing import Any, Dict, Optional, Union
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.security import get_password_hash, verify_password
|
|
from app.models.user import User
|
|
from app.schemas.user import UserCreate, UserUpdate
|
|
|
|
|
|
def get_user(db: Session, user_id: int) -> Optional[User]:
|
|
"""Get a user by ID."""
|
|
return db.query(User).filter(User.id == user_id).first()
|
|
|
|
|
|
def get_user_by_email(db: Session, email: str) -> Optional[User]:
|
|
"""Get a user by email."""
|
|
return db.query(User).filter(User.email == email).first()
|
|
|
|
|
|
def get_user_by_username(db: Session, username: str) -> Optional[User]:
|
|
"""Get a user by username."""
|
|
return db.query(User).filter(User.username == username).first()
|
|
|
|
|
|
def get_users(
|
|
db: Session, skip: int = 0, limit: int = 100
|
|
) -> list[User]:
|
|
"""Get multiple users with pagination."""
|
|
return db.query(User).offset(skip).limit(limit).all()
|
|
|
|
|
|
def create_user(db: Session, obj_in: UserCreate) -> User:
|
|
"""Create a new user."""
|
|
db_obj = User(
|
|
email=obj_in.email,
|
|
username=obj_in.username,
|
|
hashed_password=get_password_hash(obj_in.password),
|
|
full_name=obj_in.full_name,
|
|
is_active=obj_in.is_active,
|
|
is_superuser=obj_in.is_superuser,
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
|
|
def update_user(
|
|
db: Session, *, db_obj: User, obj_in: Union[UserUpdate, Dict[str, Any]]
|
|
) -> User:
|
|
"""Update a user."""
|
|
if isinstance(obj_in, dict):
|
|
update_data = obj_in
|
|
else:
|
|
update_data = obj_in.dict(exclude_unset=True)
|
|
|
|
if update_data.get("password"):
|
|
hashed_password = get_password_hash(update_data["password"])
|
|
del update_data["password"]
|
|
update_data["hashed_password"] = hashed_password
|
|
|
|
for field in update_data:
|
|
if hasattr(db_obj, field):
|
|
setattr(db_obj, field, update_data[field])
|
|
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
|
|
def authenticate_user(
|
|
db: Session, *, email: str = None, username: str = None, password: str
|
|
) -> Optional[User]:
|
|
"""Authenticate a user by email/username and password."""
|
|
if email:
|
|
user = get_user_by_email(db, email=email)
|
|
elif username:
|
|
user = get_user_by_username(db, username=username)
|
|
else:
|
|
return None
|
|
|
|
if not user:
|
|
return None
|
|
if not verify_password(password, user.hashed_password):
|
|
return None
|
|
|
|
return user
|
|
|
|
|
|
def is_active(user: User) -> bool:
|
|
"""Check if user is active."""
|
|
return user.is_active
|
|
|
|
|
|
def is_superuser(user: User) -> bool:
|
|
"""Check if user is superuser."""
|
|
return user.is_superuser |