1) 仓库接口配置 - 新增时校验同一仓库只允许一份接口配置(含唯一索引兜底,迁移会先清理历史重复数据) - 接口配置页在有配置时禁用「新增配置」并提示 2) 员工档案微信绑定 - 列表返回 wechat_bound / 脱敏 openid / 绑定时间,页面新增「微信」列 - 新增员工微信绑定码、轮询结果、解绑接口(按员工系统账号绑定,绑定码仅操作人可轮询) - 导出增加「微信绑定」列 3) 停用员工后不再展示该主体 - 主体ID列表与可选主体列表只取启用(status=1)的关联,停用后登录不显示该主体且无该主体权限 - 权限解析对停用关联直接返回空权限兜底 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
79 lines
2.6 KiB
JavaScript
79 lines
2.6 KiB
JavaScript
'use strict';
|
||
|
||
const { Service } = require('egg');
|
||
|
||
/** 菜单权限范围:1集团全部 2所在公司 */
|
||
const SCOPE_GROUP = 1;
|
||
|
||
/**
|
||
* 权限范围判定服务
|
||
*
|
||
* 依据当前账号在指定主体下的角色组(roles → permissions.scope)判断权限范围:
|
||
* - 所有者(principal_users.role = owner)视为集团权限
|
||
* - 角色组内含任一「集团全部」权限 → group(可操作该主体全部公司)
|
||
* - 其余(仅「所在公司」权限或未分配角色组)→ company(只能操作自己关联的公司)
|
||
*/
|
||
class AccessService extends Service {
|
||
/**
|
||
* 解析账号在主体下的权限范围
|
||
* @param {number} userId 用户ID
|
||
* @param {number|string} principalId 主体ID
|
||
* @return {Promise<{isGroup: boolean, scope: string, relationId: number|null, role: string|null, roleId: number|null, ownCompanyIds: Array<number>}>} 权限上下文
|
||
*/
|
||
async resolve(userId, principalId) {
|
||
const empty = {
|
||
isGroup: false,
|
||
scope: 'company',
|
||
relationId: null,
|
||
role: null,
|
||
roleId: null,
|
||
ownCompanyIds: [],
|
||
};
|
||
if (!userId || principalId == null || principalId === '') return empty;
|
||
|
||
const relation = await this.ctx.model.PrincipalUser.findOne({
|
||
where: { user_id: userId, principal_id: Number(principalId) },
|
||
attributes: [ 'id', 'role', 'role_id', 'status' ],
|
||
});
|
||
if (!relation) return empty;
|
||
// 员工在该主体下被停用:视为没有任何权限(兜底,正常应被主体绑定校验拦住)
|
||
if (Number(relation.status) !== 1) return empty;
|
||
|
||
const companyRows = await this.ctx.model.PrincipalUserCompany.findAll({
|
||
where: { principal_user_id: relation.id },
|
||
attributes: [ 'subsidiary_id' ],
|
||
raw: true,
|
||
});
|
||
const ownCompanyIds = companyRows.map(row => Number(row.subsidiary_id));
|
||
|
||
let isGroup = relation.role === 'owner';
|
||
if (!isGroup && relation.role_id) {
|
||
const groupPermission = await this.ctx.model.Permission.findOne({
|
||
where: { scope: SCOPE_GROUP, status: 1 },
|
||
attributes: [ 'id' ],
|
||
include: [{
|
||
model: this.ctx.model.Role,
|
||
as: 'roles',
|
||
attributes: [ 'id' ],
|
||
where: { id: Number(relation.role_id) },
|
||
required: true,
|
||
through: { attributes: [] },
|
||
}],
|
||
});
|
||
isGroup = !!groupPermission;
|
||
}
|
||
|
||
return {
|
||
isGroup,
|
||
scope: isGroup ? 'group' : 'company',
|
||
relationId: Number(relation.id),
|
||
role: relation.role,
|
||
roleId: relation.role_id == null ? null : Number(relation.role_id),
|
||
ownCompanyIds,
|
||
};
|
||
}
|
||
}
|
||
|
||
module.exports = AccessService;
|
||
module.exports.SCOPE_GROUP = SCOPE_GROUP;
|