jogosultsági szintek RBAC beállítva tesztelve
This commit is contained in:
@@ -1,7 +1,79 @@
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from pydantic import BaseModel, Field, ConfigDict, model_validator
|
||||
from typing import Optional, List, Any
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Organization Member Schemas (P0: Full CRUD for Garage Employees)
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class OrganizationMemberBase(BaseModel):
|
||||
"""Base schema for organization member data."""
|
||||
user_id: int = Field(..., description="The user ID to add as a member")
|
||||
role: str = Field(default="MEMBER", description="Role within the organization. Valid values: OWNER, ADMIN, MANAGER, MEMBER, AGENT (PG_ENUM orguserrole)")
|
||||
status: str = Field(default="active", description="Membership status (active, inactive, archived)")
|
||||
|
||||
|
||||
class OrganizationMemberCreate(OrganizationMemberBase):
|
||||
"""Schema for adding a new member to a garage."""
|
||||
pass
|
||||
|
||||
|
||||
class OrganizationMemberUpdate(BaseModel):
|
||||
"""Schema for updating an existing member's role/status."""
|
||||
role: Optional[str] = Field(default=None, description="New role within the organization")
|
||||
status: Optional[str] = Field(default=None, description="New membership status (active, inactive, archived)")
|
||||
|
||||
|
||||
class PersonBrief(BaseModel):
|
||||
"""Nested person data for member responses."""
|
||||
first_name: str = ""
|
||||
last_name: str = ""
|
||||
email: Optional[str] = None
|
||||
phone: Optional[str] = None
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class OrganizationMemberResponse(BaseModel):
|
||||
"""Full member data returned by the API."""
|
||||
id: int
|
||||
user_id: Optional[int] = None
|
||||
organization_id: int
|
||||
role: str
|
||||
status: str = "active"
|
||||
is_verified: bool = False
|
||||
person: Optional[PersonBrief] = None
|
||||
created_at: Optional[str] = None
|
||||
updated_at: Optional[str] = None
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def coerce_enums_to_strings(cls, data: Any) -> Any:
|
||||
"""Convert PG_ENUM / Enum values to plain strings before Pydantic validation.
|
||||
|
||||
The OrganizationMember.role field uses a PostgreSQL ENUM type (OrgUserRole).
|
||||
When loaded via SQLAlchemy's from_attributes, the enum instance is not
|
||||
automatically serialized to a string. This validator ensures any enum value
|
||||
is converted to its string representation.
|
||||
"""
|
||||
if isinstance(data, dict):
|
||||
for field_name in ("role", "status"):
|
||||
val = data.get(field_name)
|
||||
if val is not None and not isinstance(val, str):
|
||||
# Handle PG_ENUM / Python enum instances
|
||||
data[field_name] = str(val.value if hasattr(val, "value") else val)
|
||||
return data
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Contact / Onboarding Schemas
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class ContactCreate(BaseModel):
|
||||
full_name: str
|
||||
email: str
|
||||
|
||||
Reference in New Issue
Block a user