unis_hrms/app/models/rbac.py

154 lines
6.8 KiB
Python

"""RBAC + platform tables: users, roles, permissions, menus, logs, files, messages."""
from __future__ import annotations
from datetime import datetime
from sqlalchemy import Boolean, DateTime, ForeignKey, Index, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from ..db.base import Base, PKMixin, TimestampMixin, utcnow
DataScope = ("all", "dept_tree", "dept", "self")
class User(PKMixin, TimestampMixin, Base):
__tablename__ = "sys_user"
username: Mapped[str] = mapped_column(String(64), unique=True, index=True)
password_hash: Mapped[str] = mapped_column(String(255))
name: Mapped[str] = mapped_column(String(64))
email: Mapped[str | None] = mapped_column(String(128))
phone: Mapped[str | None] = mapped_column(String(32))
avatar: Mapped[str | None] = mapped_column(String(255))
employee_id: Mapped[int | None] = mapped_column(ForeignKey("hr_employee.id"))
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
is_superuser: Mapped[bool] = mapped_column(Boolean, default=False)
last_login_at: Mapped[datetime | None] = mapped_column(DateTime)
remark: Mapped[str | None] = mapped_column(String(255))
class Role(PKMixin, TimestampMixin, Base):
__tablename__ = "sys_role"
code: Mapped[str] = mapped_column(String(64), unique=True)
name: Mapped[str] = mapped_column(String(64))
description: Mapped[str | None] = mapped_column(String(255))
data_scope: Mapped[str] = mapped_column(String(16), default="self")
is_builtin: Mapped[bool] = mapped_column(Boolean, default=False)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
sort: Mapped[int] = mapped_column(Integer, default=0)
class Permission(PKMixin, Base):
__tablename__ = "sys_permission"
code: Mapped[str] = mapped_column(String(96), unique=True)
name: Mapped[str] = mapped_column(String(96))
module: Mapped[str] = mapped_column(String(32), index=True)
resource: Mapped[str] = mapped_column(String(48))
action: Mapped[str] = mapped_column(String(32))
class RolePermission(PKMixin, Base):
__tablename__ = "sys_role_permission"
__table_args__ = (UniqueConstraint("role_id", "permission_id", name="uq_role_perm"),)
role_id: Mapped[int] = mapped_column(ForeignKey("sys_role.id", ondelete="CASCADE"), index=True)
permission_id: Mapped[int] = mapped_column(
ForeignKey("sys_permission.id", ondelete="CASCADE"), index=True
)
class UserRole(PKMixin, Base):
__tablename__ = "sys_user_role"
__table_args__ = (UniqueConstraint("user_id", "role_id", name="uq_user_role"),)
user_id: Mapped[int] = mapped_column(ForeignKey("sys_user.id", ondelete="CASCADE"), index=True)
role_id: Mapped[int] = mapped_column(ForeignKey("sys_role.id", ondelete="CASCADE"), index=True)
class Menu(PKMixin, Base):
"""Navigation node; visibility is driven by ``perm_code``."""
__tablename__ = "sys_menu"
parent_id: Mapped[int | None] = mapped_column(ForeignKey("sys_menu.id"))
name: Mapped[str] = mapped_column(String(64))
path: Mapped[str | None] = mapped_column(String(128))
icon: Mapped[str | None] = mapped_column(String(48))
perm_code: Mapped[str | None] = mapped_column(String(96))
sort: Mapped[int] = mapped_column(Integer, default=0)
visible: Mapped[bool] = mapped_column(Boolean, default=True)
platform: Mapped[str] = mapped_column(String(16), default="web") # web | mobile
class RefreshToken(PKMixin, Base):
__tablename__ = "sys_refresh_token"
user_id: Mapped[int] = mapped_column(ForeignKey("sys_user.id", ondelete="CASCADE"), index=True)
jti: Mapped[str] = mapped_column(String(64), unique=True, index=True)
family: Mapped[str] = mapped_column(String(64), index=True)
user_agent: Mapped[str | None] = mapped_column(String(255))
expires_at: Mapped[datetime] = mapped_column(DateTime, index=True)
revoked_at: Mapped[datetime | None] = mapped_column(DateTime)
class OperationLog(PKMixin, Base):
__tablename__ = "sys_operation_log"
__table_args__ = (Index("ix_oplog_created", "created_at"),)
created_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow, index=True)
user_id: Mapped[int | None] = mapped_column(Integer, index=True)
username: Mapped[str | None] = mapped_column(String(64))
module: Mapped[str | None] = mapped_column(String(48))
action: Mapped[str | None] = mapped_column(String(48))
method: Mapped[str | None] = mapped_column(String(16))
path: Mapped[str | None] = mapped_column(String(255))
ip: Mapped[str | None] = mapped_column(String(64))
status_code: Mapped[int | None] = mapped_column(Integer)
duration_ms: Mapped[int | None] = mapped_column(Integer)
detail: Mapped[str | None] = mapped_column(Text)
class Attachment(PKMixin, TimestampMixin, Base):
__tablename__ = "sys_attachment"
name: Mapped[str] = mapped_column(String(255))
stored_name: Mapped[str] = mapped_column(String(255))
path: Mapped[str] = mapped_column(String(512))
content_type: Mapped[str | None] = mapped_column(String(128))
size: Mapped[int] = mapped_column(Integer, default=0)
biz_type: Mapped[str | None] = mapped_column(String(48), index=True) # contract/certificate/...
biz_id: Mapped[int | None] = mapped_column(Integer, index=True)
employee_id: Mapped[int | None] = mapped_column(Integer, index=True)
uploaded_by: Mapped[int | None] = mapped_column(Integer)
class Announcement(PKMixin, TimestampMixin, Base):
__tablename__ = "sys_announcement"
title: Mapped[str] = mapped_column(String(160))
content: Mapped[str] = mapped_column(Text)
category: Mapped[str] = mapped_column(String(32), default="notice") # notice/policy/activity
level: Mapped[str] = mapped_column(String(16), default="normal") # normal/important/urgent
publisher: Mapped[str | None] = mapped_column(String(64))
published_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow)
expires_at: Mapped[datetime | None] = mapped_column(DateTime)
is_published: Mapped[bool] = mapped_column(Boolean, default=True)
target: Mapped[str] = mapped_column(String(16), default="all") # all/employee/manager/hr
class Notification(PKMixin, Base):
__tablename__ = "sys_notification"
__table_args__ = (Index("ix_notify_user_read", "user_id", "is_read"),)
user_id: Mapped[int | None] = mapped_column(Integer, index=True)
employee_id: Mapped[int | None] = mapped_column(Integer, index=True)
title: Mapped[str] = mapped_column(String(160))
content: Mapped[str | None] = mapped_column(Text)
category: Mapped[str] = mapped_column(String(32), default="system")
link: Mapped[str | None] = mapped_column(String(255))
is_read: Mapped[bool] = mapped_column(Boolean, default=False)
created_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow)