Files
wecom_it_smart_desk/backend/app/models/role.py
T

92 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# =============================================================================
# 角色模型 — roles 表
# =============================================================================
# 说明:定义系统角色(user/agent/admin),支持 RBAC 权限控制
# =============================================================================
import uuid
from datetime import datetime
from typing import Optional
from sqlalchemy import String, Boolean, DateTime, Text, JSON
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class Role(Base):
"""角色模型 — 对应 roles 表。
预置三个角色:
- user: 所有在职员工默认角色(is_default=True
- agent: IT坐席,通过企微标签或eHR字段映射
- admin: 管理员,通过管理后台手动绑定
"""
__tablename__ = "roles"
# 主键:UUID
id: Mapped[str] = mapped_column(
String(36),
primary_key=True,
default=lambda: str(uuid.uuid4()),
)
# 角色标识:user/agent/admin(唯一)
name: Mapped[str] = mapped_column(
String(50),
unique=True,
nullable=False,
comment="角色标识:user/agent/admin",
)
# 显示名称:用户/坐席/管理员
display_name: Mapped[str] = mapped_column(
String(100),
nullable=False,
comment="显示名称:用户/坐席/管理员",
)
# 角色描述
description: Mapped[Optional[str]] = mapped_column(
Text,
nullable=True,
comment="角色描述",
)
# 权限列表(JSON数组)
permissions: Mapped[list] = mapped_column(
JSON,
nullable=False,
default=list,
comment="权限列表(JSON数组)",
)
# 是否默认角色(user=True
is_default: Mapped[bool] = mapped_column(
Boolean,
nullable=False,
default=False,
comment="是否默认角色(所有员工自动获得)",
)
# 创建时间
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
default=datetime.now,
comment="创建时间",
)
# 更新时间(自动刷新)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
default=datetime.now,
onupdate=datetime.now,
comment="更新时间",
)
def __repr__(self) -> str:
return f"<Role(id={self.id}, name={self.name}, display_name={self.display_name})>"