'use strict'; /** * 集团权限校验中间件 * * 「所在公司」权限的账号只能在外部仓库做「推送商品 / 同步库存 / 查询库存」, * 仓库与接口配置的维护(新增/编辑/删除/接口配置/快递映射/调用日志等)仅集团权限可用。 * * @param {Object} [options] 配置项 * @param {boolean} [options.allowGet] 是否放行 GET(查询类接口,如仓库列表/详情) * @return {Function} koa 中间件 */ module.exports = (options = {}) => { const allowGet = !!options.allowGet; return async (ctx, next) => { const principalId = ctx.state.principalId != null ? ctx.state.principalId : (ctx.query.principal_id != null ? ctx.query.principal_id : (ctx.request.body || {}).principal_id); const access = await ctx.service.access.resolve(ctx.userId, principalId); if (!access.isGroup && !(allowGet && ctx.method === 'GET')) { ctx.status = 403; ctx.body = { code: 403, message: '所在公司权限仅支持推送商品、同步库存与查询库存' }; return; } await next(); }; };