46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
|
|
"""授权请求版本化,禁止匿名覆盖他人的修改。"""
|
||
|
|
from typing import Literal
|
||
|
|
|
||
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||
|
|
|
||
|
|
|
||
|
|
class PermissionOverrideIn(BaseModel):
|
||
|
|
model_config = ConfigDict(extra="forbid")
|
||
|
|
permission_code: str = Field(min_length=1, max_length=128)
|
||
|
|
effect: Literal["ALLOW", "DENY"]
|
||
|
|
|
||
|
|
|
||
|
|
class AuthorizationPreviewIn(BaseModel):
|
||
|
|
model_config = ConfigDict(extra="forbid")
|
||
|
|
role_codes: list[str] = Field(default_factory=list, max_length=100)
|
||
|
|
# 保留空字段兼容旧客户端;不再允许个人允许/禁止覆盖角色授权。
|
||
|
|
overrides: list[PermissionOverrideIn] = Field(default_factory=list, max_length=0)
|
||
|
|
|
||
|
|
|
||
|
|
class UserAuthorizationIn(AuthorizationPreviewIn):
|
||
|
|
expected_revision: int = Field(ge=1)
|
||
|
|
reason: str = Field(min_length=1, max_length=500)
|
||
|
|
|
||
|
|
@field_validator("reason")
|
||
|
|
@classmethod
|
||
|
|
def reason_required(cls, value):
|
||
|
|
if not value.strip():
|
||
|
|
raise ValueError("修改原因不能为空")
|
||
|
|
return value.strip()
|
||
|
|
|
||
|
|
|
||
|
|
class RolePermissionsIn(BaseModel):
|
||
|
|
model_config = ConfigDict(extra="forbid")
|
||
|
|
permission_codes: list[str] = Field(max_length=300)
|
||
|
|
expected_revision: int = Field(ge=1)
|
||
|
|
reason: str = Field(min_length=1, max_length=500)
|
||
|
|
_reason = field_validator("reason")(UserAuthorizationIn.reason_required.__func__)
|
||
|
|
|
||
|
|
|
||
|
|
class AuthorizationSettingsIn(BaseModel):
|
||
|
|
model_config = ConfigDict(extra="forbid")
|
||
|
|
maker_user_id: int | None = Field(default=None, ge=1)
|
||
|
|
expected_revision: int = Field(ge=1)
|
||
|
|
reason: str = Field(min_length=1, max_length=500)
|
||
|
|
_reason = field_validator("reason")(UserAuthorizationIn.reason_required.__func__)
|