Files
leocrm/app/schemas/user.py
T
Agent Zero 627360113f fix(permissions): fix 10 high-priority permission system issues
P8: Invalidate all Redis sessions when is_system_admin changes
- Added is_system_admin to UserUpdate schema and UserResponse
- Added invalidate_all_user_sessions call in users.py route
- Added is_system_admin param to user_service.update_user

P9: Remove no-op permission resolution strategies
- Only highest_wins supported, others removed as no-ops
- Updated tenant.py CheckConstraint to only allow highest_wins
- Added KI-Kommentar in permissions.py

P10: Remove legacy check_permission from auth.py
- Removed duplicate check_permission and filter_fields_by_permission
- Fixed ai_copilot_service.py to use permissions.check_permission
- Updated ai_copilot route to pass resolved permissions dict

P11: Verified — no guest_users remnants found

P12: Migrate ContactFolderPermission to EntityPermission
- contact_folder_permission_service now delegates to entity_permission_service
- contact_folder_service uses EntityPermission queries
- Removed ContactFolderPermission from models/__init__.py
- Created migration 0114 to migrate data and drop table

P13: Added RLS migration history comment in alembic/env.py

P14: Verified — services already apply visibility_filter
- saved_filters/views filter by user_id (personal data)
- workspaces are UI context only
- notifications already filter by entity access

P15: Split entity_permission_service.py (932 lines) into 4 modules
- permission_resolver.py: get_effective_access, get_visible_ids, etc.
- permission_cache.py: Redis caching functions
- permission_audit.py: Audit logging helpers
- entity_permission_service.py: CRUD operations + re-exports

P16: Centralize PERM_RANK in permissions.py
- Single source: app.core.permissions.PERM_RANK
- Updated all services to import from permissions.py

P17: Fix MIGRATION_DATABASE_URL to use crm_migration
- docker-compose.yaml defaults changed from crm_user to crm_migration
- .env.docker.example updated
- prestart.sh comment updated
2026-08-06 12:05:09 +02:00

72 lines
2.5 KiB
Python

"""User schemas."""
from __future__ import annotations
import re
from pydantic import BaseModel, EmailStr, Field, field_validator
def _validate_password_complexity(password: str) -> str:
"""Validate password meets complexity requirements.
Requires: min 8 chars, at least 1 uppercase, 1 lowercase, 1 digit.
"""
if len(password) < 8:
raise ValueError("Password must be at least 8 characters")
if not re.search(r"[A-Z]", password):
raise ValueError("Password must contain at least one uppercase letter")
if not re.search(r"[a-z]", password):
raise ValueError("Password must contain at least one lowercase letter")
if not re.search(r"\d", password):
raise ValueError("Password must contain at least one digit")
return password
class UserCreate(BaseModel):
email: EmailStr = Field(..., examples=["user@leocrm.local"])
name: str = Field(..., min_length=1, max_length=200, examples=["John Doe"])
password: str = Field(..., min_length=8, examples=["secure-password"])
role: str = Field(default="viewer", examples=["viewer"])
role_id: str | None = Field(default=None, description="UUID of a custom Role", examples=["550e8400-e29b-41d4-a716-446655440000"])
is_active: bool = True
@field_validator("password")
@classmethod
def validate_password(cls, v: str) -> str:
return _validate_password_complexity(v)
class UserUpdate(BaseModel):
name: str | None = Field(None, min_length=1, max_length=200)
first_name: str | None = Field(None, max_length=100)
last_name: str | None = Field(None, max_length=100)
email: EmailStr | None = None
avatar_url: str | None = None
role: str | None = None
role_id: str | None = Field(default=None, description="UUID of a custom Role; send null to clear")
is_active: bool | None = None
is_system_admin: bool | None = Field(None, description="System admin flag — requires system admin to set")
current_password: str | None = Field(None, description="Required when changing password")
new_password: str | None = Field(None, min_length=8, description="New password")
class UserResponse(BaseModel):
id: str
email: str
name: str
first_name: str | None = None
last_name: str | None = None
avatar_url: str | None = None
role: str
role_id: str | None = None
is_active: bool
is_system_admin: bool = False
tenant_id: str
class PaginatedUsers(BaseModel):
items: list[UserResponse]
total: int
page: int
page_size: int