Improve auth models

This commit is contained in:
Urtzi Alfaro
2025-07-19 21:16:25 +02:00
parent abc8b68ab4
commit c7fd6135f0
9 changed files with 382 additions and 120 deletions

View File

@@ -9,6 +9,7 @@ import structlog
from app.core.database import get_db
from app.schemas.auth import UserResponse, PasswordChangeRequest
from app.schemas.users import UserUpdate
from app.services.user_service import UserService
from app.core.auth import get_current_user
from app.models.users import User
@@ -29,8 +30,6 @@ async def get_current_user_info(
full_name=current_user.full_name,
is_active=current_user.is_active,
is_verified=current_user.is_verified,
tenant_id=str(current_user.tenant_id) if current_user.tenant_id else None,
role=current_user.role,
phone=current_user.phone,
language=current_user.language,
timezone=current_user.timezone,
@@ -46,7 +45,7 @@ async def get_current_user_info(
@router.put("/me", response_model=UserResponse)
async def update_current_user(
user_update: dict,
user_update: UserUpdate,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db)
):
@@ -59,8 +58,6 @@ async def update_current_user(
full_name=updated_user.full_name,
is_active=updated_user.is_active,
is_verified=updated_user.is_verified,
tenant_id=str(updated_user.tenant_id) if updated_user.tenant_id else None,
role=updated_user.role,
phone=updated_user.phone,
language=updated_user.language,
timezone=updated_user.timezone,

View File

@@ -1,4 +1,3 @@
# ================================================================
# services/auth/app/models/users.py - FIXED VERSION
# ================================================================
"""
@@ -7,6 +6,7 @@ User models for authentication service - FIXED
from sqlalchemy import Column, String, Boolean, DateTime, Text
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship # Import relationship
from datetime import datetime, timezone
import uuid
@@ -22,8 +22,7 @@ class User(Base):
full_name = Column(String(255), nullable=False)
is_active = Column(Boolean, default=True)
is_verified = Column(Boolean, default=False)
tenant_id = Column(UUID(as_uuid=True), nullable=True)
role = Column(String(50), default="user") # user, admin, super_admin
# Removed tenant_id and role from User model
# FIXED: Use timezone-aware datetime for all datetime fields
created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
@@ -34,6 +33,11 @@ class User(Base):
phone = Column(String(20))
language = Column(String(10), default="es")
timezone = Column(String(50), default="Europe/Madrid")
# Relationships
# Define the many-to-many relationship through TenantMember
tenant_memberships = relationship("TenantMember", back_populates="user", cascade="all, delete-orphan") # Changed back_populates to avoid conflict
tenants = relationship("Tenant", secondary="tenant_members", back_populates="users")
def __repr__(self):
return f"<User(id={self.id}, email={self.email})>"
@@ -46,8 +50,7 @@ class User(Base):
"full_name": self.full_name,
"is_active": self.is_active,
"is_verified": self.is_verified,
"tenant_id": str(self.tenant_id) if self.tenant_id else None,
"role": self.role,
# Removed tenant_id and role from to_dict
"created_at": self.created_at.isoformat() if self.created_at else None,
"last_login": self.last_login.isoformat() if self.last_login else None,
"phone": self.phone,

View File

@@ -35,8 +35,6 @@ class UserProfile(BaseModel):
timezone: str
is_active: bool
is_verified: bool
tenant_id: Optional[str]
role: str
created_at: datetime
last_login: Optional[datetime]

View File

@@ -39,9 +39,6 @@ class AuthService:
detail="Email already registered"
)
# Generate tenant_id if not provided
tenant_id = user_data.tenant_id if hasattr(user_data, 'tenant_id') and user_data.tenant_id else str(uuid.uuid4())
# Hash password
hashed_password = security_manager.hash_password(user_data.password)
@@ -49,7 +46,6 @@ class AuthService:
user = User(
email=user_data.email,
hashed_password=hashed_password,
tenant_id=tenant_id,
full_name=user_data.full_name,
phone=user_data.phone,
language=user_data.language,

View File

@@ -7,6 +7,7 @@ from sqlalchemy import select, update, delete
from fastapi import HTTPException, status
from passlib.context import CryptContext
import structlog
from datetime import datetime, timezone
from app.models.users import User
from app.core.config import settings
@@ -59,6 +60,7 @@ class UserService:
update_data[field] = user_data[field]
if update_data:
update_data["updated_at"] = datetime.now(timezone.utc)
await db.execute(
update(User)
.where(User.id == user_id)
@@ -107,7 +109,7 @@ class UserService:
await db.execute(
update(User)
.where(User.id == user_id)
.values(hashed_password=new_hashed_password)
.values(hashed_password=new_hashed_password, updated_at=datetime.now(timezone.utc))
)
await db.commit()