"""Single source of truth for RBAC permission codes, roles and menus. The seed process (and the frontend navigation) are both derived from here, so a new module only has to be declared once. """ from __future__ import annotations from dataclasses import dataclass DEFAULT_ACTIONS = ["read", "create", "update", "delete"] MODULES: dict[str, dict] = { "system": { "label": "系统管理", "icon": "SafetyOutlined", "resources": { "user": {"label": "用户账号", "actions": [*DEFAULT_ACTIONS, "reset", "assign_role"]}, "role": {"label": "角色权限", "actions": [*DEFAULT_ACTIONS, "assign"]}, "menu": {"label": "菜单导航", "actions": ["read", "update"]}, "dict": {"label": "数据字典", "actions": DEFAULT_ACTIONS}, "announcement": {"label": "公告通知", "actions": [*DEFAULT_ACTIONS, "publish"]}, "log": {"label": "操作日志", "actions": ["read"]}, "maintenance": {"label": "数据维护", "actions": ["read", "backup", "export", "purge"]}, }, }, "org": { "label": "组织架构", "icon": "ApartmentOutlined", "resources": { "department": {"label": "部门管理", "actions": [*DEFAULT_ACTIONS, "import", "export"]}, "position": {"label": "职位管理", "actions": [*DEFAULT_ACTIONS, "import", "export"]}, "joblevel": {"label": "职级职等", "actions": [*DEFAULT_ACTIONS, "import", "export"]}, }, }, "employee": { "label": "人事档案", "icon": "IdcardOutlined", "resources": { "employee": {"label": "员工档案", "actions": [*DEFAULT_ACTIONS, "export", "import"]}, "contract": {"label": "劳动合同", "actions": [*DEFAULT_ACTIONS, "remind"]}, "change": {"label": "人事异动", "actions": ["read", "create", "approve", "delete"]}, "adjustment": {"label": "调薪记录", "actions": ["read", "create", "approve", "delete"]}, "document": {"label": "证明开具", "actions": ["read", "create", "approve", "delete"]}, "attachment": {"label": "档案附件", "actions": ["read", "create", "delete"]}, }, }, "recruitment": { "label": "招聘管理", "icon": "UserSwitchOutlined", "resources": { "job": {"label": "招聘职位", "actions": [*DEFAULT_ACTIONS, "close"]}, "candidate": {"label": "候选人", "actions": [*DEFAULT_ACTIONS, "pool"]}, "application": {"label": "应聘流程", "actions": [*DEFAULT_ACTIONS, "move"]}, "interview": {"label": "面试安排", "actions": [*DEFAULT_ACTIONS, "feedback"]}, "offer": {"label": "Offer 管理", "actions": [*DEFAULT_ACTIONS, "approve", "send"]}, "report": {"label": "招聘分析", "actions": ["read"]}, }, }, "payroll": { "label": "薪资管理", "icon": "MoneyCollectOutlined", "resources": { "period": {"label": "计薪期间", "actions": DEFAULT_ACTIONS}, "item": {"label": "薪资项目", "actions": DEFAULT_ACTIONS}, "plan": {"label": "薪资账套", "actions": DEFAULT_ACTIONS}, "standard": {"label": "薪资标准", "actions": ["read", "create", "update", "delete", "import"]}, "batch": { "label": "月度算薪", "actions": [*DEFAULT_ACTIONS, "calculate", "approve", "pay", "export"], }, "payslip": {"label": "工资条", "actions": ["read", "update", "publish"]}, "report": {"label": "薪资报表", "actions": ["read", "export"]}, }, }, "benefits": { "label": "福利保险", "icon": "InsuranceOutlined", "resources": { "plan": {"label": "参保方案", "actions": DEFAULT_ACTIONS}, "enrollment": {"label": "参保登记", "actions": [*DEFAULT_ACTIONS, "import"]}, "item": {"label": "福利项目", "actions": DEFAULT_ACTIONS}, "record": {"label": "福利发放", "actions": [*DEFAULT_ACTIONS, "grant", "pay"]}, "leave": {"label": "假期管理", "actions": ["read", "create", "update", "approve"]}, "leavebalance": {"label": "假期额度", "actions": ["read", "create", "update", "generate"]}, }, }, "selfservice": { "label": "员工自助", "icon": "MobileOutlined", "resources": { "home": {"label": "自助首页", "actions": ["read"]}, "profile": {"label": "我的信息", "actions": ["read", "update"]}, "payslip": {"label": "我的工资条", "actions": ["read"]}, "insurance": {"label": "我的五险一金", "actions": ["read"]}, "benefit": {"label": "我的福利", "actions": ["read"]}, "leave": {"label": "请假申请", "actions": ["read", "create", "cancel"]}, "approval": {"label": "我的审批", "actions": ["read", "approve"]}, "document": {"label": "证明申请", "actions": ["read", "create"]}, "task": {"label": "HR 待办", "actions": ["read", "update"]}, }, }, } @dataclass(frozen=True) class PermissionDef: code: str name: str module: str resource: str action: str @property def label(self) -> str: module = MODULES[self.module] resource = module["resources"][self.resource] return f"{module['label']} / {resource['label']}" ACTION_LABELS = { "read": "查看", "create": "新增", "update": "编辑", "delete": "删除", "export": "导出", "import": "导入", "reset": "重置密码", "assign": "分配权限", "assign_role": "分配角色", "publish": "发布", "approve": "审批", "move": "流转阶段", "feedback": "面试评价", "send": "发送", "close": "关闭职位", "pool": "进入人才库", "calculate": "一键算薪", "pay": "发放", "grant": "批量发放", "remind": "到期提醒", "generate": "生成额度", "backup": "一键备份", "purge": "清空数据", } def build_permissions() -> list[PermissionDef]: out: list[PermissionDef] = [] for module_key, module in MODULES.items(): for res_key, res in module["resources"].items(): for action in res["actions"]: code = f"{module_key}:{res_key}:{action}" name = f"{res['label']}-{ACTION_LABELS.get(action, action)}" out.append(PermissionDef(code, name, module_key, res_key, action)) return out def module_perm(module_key: str, action: str = "read") -> str: return f"{module_key}:*:{action}" # --- menus (web console + mobile H5) -------------------------------------- MENU_TREE: list[dict] = [ { "name": "工作台", "path": "/dashboard", "icon": "DashboardOutlined", "perm": "selfservice:home:read", "sort": 10, }, { "name": "人事档案", "icon": "IdcardOutlined", "sort": 20, "children": [ {"name": "员工档案", "path": "/employees", "icon": "TeamOutlined", "perm": "employee:employee:read"}, {"name": "合同管理", "path": "/employees/contracts", "icon": "FileDoneOutlined", "perm": "employee:contract:read"}, {"name": "人事异动", "path": "/employees/changes", "icon": "SwapOutlined", "perm": "employee:change:read"}, {"name": "证明开具", "path": "/employees/documents", "icon": "FileProtectOutlined", "perm": "employee:document:read"}, ], }, { "name": "招聘管理", "icon": "UserSwitchOutlined", "sort": 30, "children": [ {"name": "招聘职位", "path": "/recruitment/jobs", "icon": "ProfileOutlined", "perm": "recruitment:job:read"}, {"name": "招聘看板", "path": "/recruitment/pipeline", "icon": "ProjectOutlined", "perm": "recruitment:application:read"}, {"name": "候选人库", "path": "/recruitment/candidates", "icon": "ContactsOutlined", "perm": "recruitment:candidate:read"}, {"name": "面试安排", "path": "/recruitment/interviews", "icon": "CalendarOutlined", "perm": "recruitment:interview:read"}, {"name": "Offer 管理", "path": "/recruitment/offers", "icon": "MailOutlined", "perm": "recruitment:offer:read"}, {"name": "招聘分析", "path": "/recruitment/analytics", "icon": "PieChartOutlined", "perm": "recruitment:report:read"}, ], }, { "name": "薪资管理", "icon": "MoneyCollectOutlined", "sort": 40, "children": [ {"name": "月度算薪", "path": "/payroll/batches", "icon": "CalculatorOutlined", "perm": "payroll:batch:read"}, {"name": "工资条", "path": "/payroll/payslips", "icon": "AccountBookOutlined", "perm": "payroll:payslip:read"}, {"name": "薪资账套", "path": "/payroll/plans", "icon": "GroupOutlined", "perm": "payroll:plan:read"}, {"name": "薪资标准", "path": "/payroll/standards", "icon": "SlidersOutlined", "perm": "payroll:standard:read"}, {"name": "薪资项目", "path": "/payroll/items", "icon": "TagsOutlined", "perm": "payroll:item:read"}, {"name": "薪资报表", "path": "/payroll/reports", "icon": "BarChartOutlined", "perm": "payroll:report:read"}, ], }, { "name": "福利保险", "icon": "InsuranceOutlined", "sort": 50, "children": [ {"name": "参保登记", "path": "/benefits/enrollments", "icon": "SafetyCertificateOutlined", "perm": "benefits:enrollment:read"}, {"name": "参保方案", "path": "/benefits/plans", "icon": "ClusterOutlined", "perm": "benefits:plan:read"}, {"name": "福利项目", "path": "/benefits/items", "icon": "GiftOutlined", "perm": "benefits:item:read"}, {"name": "福利发放", "path": "/benefits/records", "icon": "TrophyOutlined", "perm": "benefits:record:read"}, {"name": "假期管理", "path": "/benefits/leaves", "icon": "ScheduleOutlined", "perm": "benefits:leave:read"}, ], }, { "name": "组织架构", "icon": "ApartmentOutlined", "sort": 60, "children": [ {"name": "部门结构", "path": "/org/departments", "icon": "ApartmentOutlined", "perm": "org:department:read"}, {"name": "职位管理", "path": "/org/positions", "icon": "SolutionOutlined", "perm": "org:position:read"}, {"name": "职级职等", "path": "/org/job-levels", "icon": "PartitionOutlined", "perm": "org:joblevel:read"}, ], }, { "name": "系统管理", "icon": "SafetyOutlined", "sort": 90, "children": [ {"name": "用户管理", "path": "/system/users", "icon": "UserOutlined", "perm": "system:user:read"}, {"name": "角色权限", "path": "/system/roles", "icon": "SecurityScanOutlined", "perm": "system:role:read"}, {"name": "数据字典", "path": "/system/dicts", "icon": "BookOutlined", "perm": "system:dict:read"}, {"name": "公告通知", "path": "/system/announcements", "icon": "NotificationOutlined", "perm": "system:announcement:read"}, {"name": "操作日志", "path": "/system/logs", "icon": "HistoryOutlined", "perm": "system:log:read"}, {"name": "数据维护", "path": "/system/maintenance", "icon": "DatabaseOutlined", "perm": "system:maintenance:read"}, ], }, ] MOBILE_MENU_TREE: list[dict] = [ {"name": "首页", "path": "/m", "icon": "home", "perm": "selfservice:home:read", "sort": 10}, {"name": "请假", "path": "/m/leave", "icon": "calendar", "perm": "selfservice:leave:read", "sort": 20}, {"name": "工资", "path": "/m/payslips", "icon": "gold", "perm": "selfservice:payslip:read", "sort": 30}, {"name": "福利", "path": "/m/benefits", "icon": "gift", "perm": "selfservice:benefit:read", "sort": 40}, {"name": "我的", "path": "/m/profile", "icon": "user", "perm": "selfservice:profile:read", "sort": 50}, ] # 普通员工在 PC 浏览器登录时使用的自助导航。名称、顺序和权限与 H5 底部导航 # 完全一致,只把目的地换成了管理端外壳内的响应式页面。 SELF_WEB_MENU_TREE: list[dict] = [ {"name": "首页", "path": "/dashboard", "icon": "HomeOutlined", "perm": "selfservice:home:read", "sort": 10}, {"name": "请假", "path": "/self/leave", "icon": "CalendarOutlined", "perm": "selfservice:leave:read", "sort": 20}, {"name": "工资", "path": "/self/payslips", "icon": "MoneyCollectOutlined", "perm": "selfservice:payslip:read", "sort": 30}, {"name": "福利", "path": "/self/benefits", "icon": "GiftOutlined", "perm": "selfservice:benefit:read", "sort": 40}, {"name": "我的", "path": "/self/profile", "icon": "UserOutlined", "perm": "selfservice:profile:read", "sort": 50}, ] # 通知 / 待办里落库的 link 一律取这里的路径,不要在业务代码里手写字符串。 # 约定:用 H5 路径入库(历史数据即如此),前端渲染前统一过 toWebPath(), # 于是同一条消息在 Web 工作台跳 /self/payslips、在 H5 跳 /m/payslips,永不 404。 # 审批与待办不在两份菜单树里(两端都从首页进入),因此在此显式登记。 SELF_LINKS = { "home": "/m", "leave": "/m/leave", "payslips": "/m/payslips", "benefits": "/m/benefits", "approvals": "/m/approvals", "tasks": "/m/tasks", "profile": "/m/profile", } # --- built-in roles -------------------------------------------------------- # 员工自助 (H5) 基线:任何绑定档案的账号都应当能完成「看工资条 / 请假 / 改资料」 # 等自助动作。管理者与专员首先是员工,因此除超级管理员外,各角色模板均并入该基线; # 审批动作单独授予(普通员工不得自批)。 SELF_SERVICE: list[str] = [ "selfservice:home:read", "selfservice:profile:read", "selfservice:profile:update", "selfservice:payslip:read", "selfservice:insurance:read", "selfservice:benefit:read", "selfservice:leave:*", "selfservice:document:*", "selfservice:approval:read", "selfservice:task:read", "selfservice:task:update", ] SELF_APPROVE = "selfservice:approval:approve" ROLE_TEMPLATES: list[dict] = [ { "code": "super_admin", "name": "超级管理员", "data_scope": "all", "description": "拥有系统全部权限", "perms": ["*"], }, { "code": "hr_admin", "name": "HR 负责人", "data_scope": "all", "description": "人力资源全模块管理", "perms": ["org:*:read", "org:*:write", "employee:*:*", "recruitment:*:*", "payroll:*:read", "payroll:period:*", "payroll:standard:*", "payroll:batch:*", "payroll:payslip:*", "payroll:report:*", "benefits:*:*", "selfservice:*:*", "system:log:read", "system:announcement:*"], }, { "code": "hr_specialist", "name": "人事专员", "data_scope": "dept_tree", "description": "档案、合同、异动与招聘执行", "perms": ["org:*:read", "employee:employee:*", "employee:contract:*", "employee:change:*", "employee:document:*", "employee:attachment:*", "recruitment:job:*", "recruitment:candidate:*", "recruitment:application:*", "recruitment:interview:*", "recruitment:offer:read", "recruitment:report:read", "benefits:enrollment:read", "benefits:leave:*", "system:dict:*", *SELF_SERVICE, SELF_APPROVE], }, { "code": "recruit_manager", "name": "招聘经理", "data_scope": "dept_tree", "description": "招聘全流程与人才库", "perms": ["org:*:read", "employee:employee:read", "recruitment:*:*", "benefits:leave:read", *SELF_SERVICE, SELF_APPROVE], }, { "code": "payroll_admin", "name": "薪酬福利专员", "data_scope": "all", "description": "薪资核算、五险一金与福利发放", "perms": ["org:*:read", "employee:employee:read", "employee:adjustment:*", "payroll:*:*", "benefits:*:*", *SELF_SERVICE, SELF_APPROVE], }, { "code": "manager", "name": "部门经理", "data_scope": "dept_tree", "description": "查看本部门员工、审批下属申请", "perms": ["org:*:read", "employee:employee:read", "employee:change:read", "recruitment:job:read", "recruitment:candidate:read", "recruitment:application:read", "recruitment:interview:read", "benefits:leave:read", "payroll:report:read", *SELF_SERVICE, SELF_APPROVE], }, { "code": "employee", "name": "普通员工", "data_scope": "self", "description": "仅可使用员工自助 (H5)", "perms": list(SELF_SERVICE), }, ] def role_matches(code: str, patterns: list[str]) -> bool: """Match ``module:resource:action`` against patterns such as ``employee:*:read``.""" parts = code.split(":") for pattern in patterns: if pattern == "*": return True pp = pattern.split(":") if len(pp) != len(parts): continue if all(a == "*" or a == b for a, b in zip(pp, parts)): return True return False def menu_catalogue_order() -> tuple[list[str], dict[str, list[str]]]: """权限清单的排序基准:**与主界面菜单一致**(模块顺序 → 模块内资源顺序)。 排序不是再抄一份常量,而是从 ``MENU_TREE`` / ``MOBILE_MENU_TREE`` 里按出现顺序 解析 ``perm`` 得到,于是「菜单挪位置」和「角色授权页顺序」永远同步: * 只统计分组节点的子项:顶层的「工作台」是单页叶子,不代表一个模块分组; * 菜单里没有的模块/资源(员工自助、纯接口权限点)按 ``MODULES`` 声明顺序补在后面。 返回 ``(模块顺序, {模块: 资源顺序})``。 """ modules: list[str] = [] resources: dict[str, list[str]] = {} def touch(code: object) -> None: parts = str(code or "").split(":") if len(parts) != 3: return module, resource = parts[0], parts[1] if module not in MODULES: return if module not in modules: modules.append(module) bucket = resources.setdefault(module, []) if resource not in bucket: bucket.append(resource) for node in MENU_TREE: for child in node.get("children") or []: touch(child.get("perm")) # H5 导航给出「员工自助」内部资源的顺序(请假/工资条/福利/我的…) for node in MOBILE_MENU_TREE: touch(node.get("perm")) for module_key, module in MODULES.items(): if module_key not in modules: modules.append(module_key) bucket = resources.setdefault(module_key, []) for res_key in module["resources"]: if res_key not in bucket: bucket.append(res_key) return modules, resources