mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-23 05:10:57 +00:00
feat(backend): 实现多租户数据隔离与权限管理架构
refactor(backend): 重构模型基类支持租户与客户隔离 feat(backend): 添加客户模块相关模型、CRUD和参数校验 docs(backend): 新增SaaS数据隔离设计方案文档 refactor(backend): 优化日志模块并添加类型注解 fix(backend): 修正字典模块查询参数移除creator字段 style(frontend): 统一按钮组件代码格式 fix(frontend): 修复表格序号计算逻辑 chore(frontend): 更新lint脚本使用pnpm替代npm
This commit is contained in:
@@ -1,39 +1,77 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional, List
|
||||
from typing import TYPE_CHECKING
|
||||
from sqlalchemy import String, Integer, ForeignKey, Boolean
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship, validates
|
||||
from sqlalchemy.sql import expression
|
||||
|
||||
from app.config.setting import settings
|
||||
from app.core.base_model import CreatorMixin
|
||||
from app.core.base_model import ModelMixin, UserMixin, TenantMixin
|
||||
from app.utils.common_util import SqlalchemyUtil
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.api.v1.module_system.user.model import UserModel
|
||||
from app.api.v1.module_system.tenant.model import TenantModel
|
||||
|
||||
class GenTableModel(CreatorMixin):
|
||||
|
||||
class GenTableModel(ModelMixin, UserMixin, TenantMixin):
|
||||
"""
|
||||
代码生成表
|
||||
|
||||
数据隔离策略:
|
||||
===========
|
||||
- 系统级生成: tenant_id=1 (平台管理员生成系统代码)
|
||||
- 租户级生成: tenant_id>1 (租户管理员生成租户业务代码)
|
||||
- 不需要customer_id: 代码生成是租户级功能,不需要客户隔离
|
||||
|
||||
用于存储代码生成器的表结构配置
|
||||
"""
|
||||
__tablename__ = 'gen_table'
|
||||
__table_args__ = ({'comment': '代码生成表'})
|
||||
__loader_options__ = ["columns", "creator"]
|
||||
|
||||
__tablename__: str = 'gen_table'
|
||||
__table_args__: dict[str, str] = ({'comment': '代码生成表'})
|
||||
__loader_options__: list[str] = ["columns", "created_by", "updated_by", "tenant"]
|
||||
|
||||
table_name: Mapped[str] = mapped_column(String(200), nullable=False, default='', comment='表名称')
|
||||
table_comment: Mapped[Optional[str]] = mapped_column(String(500), nullable=True, comment='表描述')
|
||||
table_comment: Mapped[str | None] = mapped_column(String(500), nullable=True, comment='表描述')
|
||||
|
||||
class_name: Mapped[str] = mapped_column(String(100), nullable=False, default='', comment='实体类名称')
|
||||
package_name: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, comment='生成包路径')
|
||||
module_name: Mapped[Optional[str]] = mapped_column(String(30), nullable=True, comment='生成模块名')
|
||||
business_name: Mapped[Optional[str]] = mapped_column(String(30), nullable=True, comment='生成业务名')
|
||||
function_name: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, comment='生成功能名')
|
||||
package_name: Mapped[str | None] = mapped_column(String(100), nullable=True, comment='生成包路径')
|
||||
module_name: Mapped[str | None] = mapped_column(String(30), nullable=True, comment='生成模块名')
|
||||
business_name: Mapped[str | None] = mapped_column(String(30), nullable=True, comment='生成业务名')
|
||||
function_name: Mapped[str | None] = mapped_column(String(100), nullable=True, comment='生成功能名')
|
||||
|
||||
sub_table_name: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, server_default=SqlalchemyUtil.get_server_default_null(settings.DATABASE_TYPE), comment='关联子表的表名')
|
||||
sub_table_fk_name: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, server_default=SqlalchemyUtil.get_server_default_null(settings.DATABASE_TYPE), comment='子表关联的外键名')
|
||||
sub_table_name: Mapped[str | None] = mapped_column(
|
||||
String(64),
|
||||
nullable=True,
|
||||
server_default=SqlalchemyUtil.get_server_default_null(settings.DATABASE_TYPE),
|
||||
comment='关联子表的表名'
|
||||
)
|
||||
sub_table_fk_name: Mapped[str | None] = mapped_column(
|
||||
String(64),
|
||||
nullable=True,
|
||||
server_default=SqlalchemyUtil.get_server_default_null(settings.DATABASE_TYPE),
|
||||
comment='子表关联的外键名'
|
||||
)
|
||||
|
||||
parent_menu_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, comment='父菜单ID')
|
||||
parent_menu_id: Mapped[int | None] = mapped_column(Integer, nullable=True, comment='父菜单ID')
|
||||
|
||||
# 关系定义
|
||||
columns: Mapped[List['GenTableColumnModel']] = relationship('GenTableColumnModel', order_by='GenTableColumnModel.sort', back_populates='table',cascade='all, delete-orphan')
|
||||
# 关联关系
|
||||
columns: Mapped[list['GenTableColumnModel']] = relationship(
|
||||
order_by='GenTableColumnModel.sort',
|
||||
back_populates='table',
|
||||
cascade='all, delete-orphan'
|
||||
)
|
||||
created_by: Mapped["UserModel | None"] = relationship(
|
||||
foreign_keys="GenTableModel.created_id",
|
||||
lazy="selectin"
|
||||
)
|
||||
updated_by: Mapped["UserModel | None"] = relationship(
|
||||
foreign_keys="GenTableModel.updated_id",
|
||||
lazy="selectin"
|
||||
)
|
||||
tenant: Mapped["TenantModel"] = relationship(
|
||||
foreign_keys="GenTableModel.tenant_id",
|
||||
lazy="selectin"
|
||||
)
|
||||
|
||||
@validates('table_name')
|
||||
def validate_table_name(self, key: str, table_name: str) -> str:
|
||||
@@ -50,39 +88,45 @@ class GenTableModel(CreatorMixin):
|
||||
return class_name.strip()
|
||||
|
||||
|
||||
class GenTableColumnModel(CreatorMixin):
|
||||
class GenTableColumnModel(ModelMixin, UserMixin, TenantMixin):
|
||||
"""
|
||||
代码生成表字段
|
||||
|
||||
数据隔离策略:
|
||||
- 继承自GenTableModel的隔离级别
|
||||
- 不需要customer_id
|
||||
|
||||
用于存储代码生成器的字段配置
|
||||
"""
|
||||
__tablename__ = 'gen_table_column'
|
||||
__table_args__ = ({'comment': '代码生成表字段'})
|
||||
__loader_options__ = ["table", "creator"]
|
||||
__tablename__: str = 'gen_table_column'
|
||||
__table_args__: dict[str, str] = ({'comment': '代码生成表字段'})
|
||||
__loader_options__: list[str] = ["table", "created_by", "updated_by", "tenant"]
|
||||
|
||||
# 数据库设计表字段
|
||||
column_name: Mapped[str] = mapped_column(String(200), nullable=False, comment='列名称')
|
||||
column_comment: Mapped[Optional[str]] = mapped_column(String(500), nullable=True, comment='列描述')
|
||||
column_comment: Mapped[str | None] = mapped_column(String(500), nullable=True, comment='列描述')
|
||||
column_type: Mapped[str] = mapped_column(String(100), nullable=False, comment='列类型')
|
||||
column_length: Mapped[Optional[str]] = mapped_column(String(50), nullable=True, comment='列长度')
|
||||
column_default: Mapped[Optional[str]] = mapped_column(String(200), nullable=True, comment='列默认值')
|
||||
column_length: Mapped[str | None] = mapped_column(String(50), nullable=True, comment='列长度')
|
||||
column_default: Mapped[str | None] = mapped_column(String(200), nullable=True, comment='列默认值')
|
||||
is_pk: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=expression.false(), comment='是否主键')
|
||||
is_increment: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=expression.false(), comment='是否自增')
|
||||
is_nullable: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=expression.true(), comment='是否允许为空')
|
||||
is_unique: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=expression.false(), comment='是否唯一')
|
||||
|
||||
# Python字段映射
|
||||
python_type: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, comment='Python类型')
|
||||
python_field: Mapped[Optional[str]] = mapped_column(String(200), nullable=True, comment='Python字段名')
|
||||
python_type: Mapped[str | None] = mapped_column(String(100), nullable=True, comment='Python类型')
|
||||
python_field: Mapped[str | None] = mapped_column(String(200), nullable=True, comment='Python字段名')
|
||||
|
||||
# 序列化配置
|
||||
is_insert: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=expression.true(), comment='是否为新增字段')
|
||||
is_edit: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=expression.true(), comment='是否编辑字段')
|
||||
is_list: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=expression.true(), comment='是否列表字段')
|
||||
is_query: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=expression.false(), comment='是否查询字段')
|
||||
query_type: Mapped[Optional[str]] = mapped_column(String(50), nullable=True, default=None, comment='查询方式')
|
||||
query_type: Mapped[str | None] = mapped_column(String(50), nullable=True, default=None, comment='查询方式')
|
||||
|
||||
# 前端展示配置
|
||||
html_type: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, default='input', comment='显示类型')
|
||||
dict_type: Mapped[Optional[str]] = mapped_column(String(200), nullable=True, default='', comment='字典类型')
|
||||
html_type: Mapped[str | None] = mapped_column(String(100), nullable=True, default='input', comment='显示类型')
|
||||
dict_type: Mapped[str | None] = mapped_column(String(200), nullable=True, default='', comment='字典类型')
|
||||
|
||||
# 排序和扩展配置
|
||||
sort: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment='排序')
|
||||
@@ -96,8 +140,20 @@ class GenTableColumnModel(CreatorMixin):
|
||||
comment='归属表编号'
|
||||
)
|
||||
|
||||
# 关系定义
|
||||
table: Mapped['GenTableModel'] = relationship('GenTableModel', back_populates='columns')
|
||||
# 关联关系
|
||||
table: Mapped['GenTableModel'] = relationship(back_populates='columns')
|
||||
created_by: Mapped["UserModel | None"] = relationship(
|
||||
foreign_keys="GenTableColumnModel.created_id",
|
||||
lazy="selectin"
|
||||
)
|
||||
updated_by: Mapped["UserModel | None"] = relationship(
|
||||
foreign_keys="GenTableColumnModel.updated_id",
|
||||
lazy="selectin"
|
||||
)
|
||||
tenant: Mapped["TenantModel"] = relationship(
|
||||
foreign_keys="GenTableColumnModel.tenant_id",
|
||||
lazy="selectin"
|
||||
)
|
||||
|
||||
@validates('column_name')
|
||||
def validate_column_name(self, key: str, column_name: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user