liufumingren b4b7c56729 feat: 权限范围收束与员工档案规则
- 新增 access 服务与 /api/access/context:按角色组权限判定集团 / 所在公司范围
- 菜单树按范围裁剪:所在公司范围仅保留「员工档案」,其余系统管理菜单隐藏
- 员工档案:角色组与所属公司必填;所在公司范围仅可见、仅可关联自己所属公司
- 创建者(所有者)行禁止编辑与解绑,且不可被移出主体;禁止新建所有者
- 所在公司范围禁止修改自己或他人的角色组、所属公司
- 新增员工时邮箱已注册则复用既有账号(reused),不再报「该邮箱已注册」
- 迁移:将所有者 role_id 统一指向系统管理员角色组

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-05 00:50:42 +08:00

220 lines
6.4 KiB
JavaScript
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.

'use strict';
const { Controller } = require('egg');
const { parsePagination, buildPageResult } = require('../lib/pagination');
/** 所在公司权限可访问的系统管理菜单(员工档案) */
const EMPLOYEE_MENU_PATH = '/system/employee';
/**
* @controller MenuController 菜单管理
*/
class MenuController extends Controller {
/**
* @summary 菜单列表
* @description 分页查询菜单,支持按父级、类型与状态过滤
* @router get /api/menus
* @apikey
* @request query integer page 页码,默认 1
* @request query integer pageSize 每页条数,默认 20
* @request query integer parent_id 父菜单ID,0 表示根节点
* @request query integer type 类型:1目录 2菜单 3按钮,不传则不过滤
* @request query integer status 状态:1启用 0禁用,不传则不过滤
* @response 200 menuListResponse success
*/
async index() {
const { ctx } = this;
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
const { parent_id: parentId, type, status } = ctx.query;
const where = {};
if (parentId !== undefined && parentId !== '') where.parent_id = Number(parentId);
if (type !== undefined && type !== '') where.type = Number(type);
if (status !== undefined && status !== '') where.status = Number(status);
const { count, rows } = await ctx.model.Menu.findAndCountAll({
where,
offset,
limit,
order: [[ 'sort', 'DESC' ], [ 'id', 'ASC' ]],
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
}
/**
* @summary 菜单树
* @description 一次性返回全部菜单并按 parent_id 组装为树形结构;
* 传入 principal_id 且当前账号在该主体下仅为「所在公司」权限时,
* 系统管理下仅保留「员工档案」(其余菜单需集团权限)
* @router get /api/menus/tree
* @apikey
* @request query integer status 状态:1启用 0禁用,不传则返回全部
* @request query integer principal_id 当前主体ID(用于判定权限范围)
* @response 200 baseResponse success
*/
async tree() {
const { ctx } = this;
const { status } = ctx.query;
const where = {};
if (status !== undefined && status !== '') where.status = Number(status);
const list = await ctx.model.Menu.findAll({
where,
order: [[ 'sort', 'DESC' ], [ 'id', 'ASC' ]],
});
let rows = list.map(item => item.toJSON());
if (ctx.query.principal_id) {
const access = await ctx.service.access.resolve(ctx.userId, ctx.query.principal_id);
if (!access.isGroup) rows = this.filterCompanyScopeMenus(rows);
}
const map = new Map();
const tree = [];
rows.forEach(item => {
item.children = [];
map.set(item.id, item);
});
rows.forEach(item => {
const parent = map.get(item.parent_id);
if (parent) {
parent.children.push(item);
} else {
tree.push(item);
}
});
ctx.body = { code: 0, message: 'success', data: tree };
}
/**
* 所在公司权限的菜单收束:系统管理目录下仅保留「员工档案」
* @param {Array<Object>} rows 菜单行
* @return {Array<Object>} 过滤后的菜单行
*/
filterCompanyScopeMenus(rows) {
const employee = rows.find(row => row.path === EMPLOYEE_MENU_PATH && Number(row.type) === 2);
if (!employee) return rows;
const systemId = Number(employee.parent_id);
const removedIds = new Set(
rows
.filter(row => Number(row.parent_id) === systemId && Number(row.id) !== Number(employee.id))
.map(row => Number(row.id))
);
// 父级被移除的子级一并移除,避免孤儿节点被挂到根节点
let changed = true;
while (changed) {
changed = false;
for (const row of rows) {
if (removedIds.has(Number(row.id))) continue;
if (removedIds.has(Number(row.parent_id))) {
removedIds.add(Number(row.id));
changed = true;
}
}
}
return rows.filter(row => !removedIds.has(Number(row.id)));
}
/**
* @summary 菜单详情
* @description 按主键查询菜单
* @router get /api/menus/{id}
* @apikey
* @request path integer *id 菜单主键ID
* @response 200 baseResponse success
*/
async show() {
const { ctx } = this;
const menu = await ctx.model.Menu.findByPk(ctx.params.id);
if (!menu) {
ctx.status = 404;
ctx.body = { code: 404, message: '菜单不存在' };
return;
}
ctx.body = { code: 0, message: 'success', data: menu };
}
/**
* @summary 新增菜单
* @description parent_id 不传默认为 0(根节点)
* @router post /api/menus
* @apikey
* @request body createMenuRequest *body
* @response 200 baseResponse success
*/
async create() {
const { ctx } = this;
const { name } = ctx.request.body;
if (!name) {
ctx.status = 400;
ctx.body = { code: 400, message: 'name 为必填项' };
return;
}
const menu = await ctx.model.Menu.create({
parent_id: 0,
type: 2,
sort: 0,
status: 1,
...ctx.request.body,
});
ctx.body = { code: 0, message: 'success', data: { id: menu.id } };
}
/**
* @summary 更新菜单
* @description 未传入的字段保持原值
* @router put /api/menus/{id}
* @apikey
* @request path integer *id 菜单主键ID
* @request body updateMenuRequest *body
* @response 200 baseResponse success
*/
async update() {
const { ctx } = this;
const menu = await ctx.model.Menu.findByPk(ctx.params.id);
if (!menu) {
ctx.status = 404;
ctx.body = { code: 404, message: '菜单不存在' };
return;
}
await menu.update(ctx.request.body);
ctx.body = { code: 0, message: 'success', data: { id: menu.id } };
}
/**
* @summary 删除菜单
* @description 物理删除菜单;存在子菜单时需先删除子菜单
* @router delete /api/menus/{id}
* @apikey
* @request path integer *id 菜单主键ID
* @response 200 baseResponse success
*/
async destroy() {
const { ctx } = this;
const menu = await ctx.model.Menu.findByPk(ctx.params.id);
if (!menu) {
ctx.status = 404;
ctx.body = { code: 404, message: '菜单不存在' };
return;
}
await menu.destroy();
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
}
}
module.exports = MenuController;