tenant-check.js 697 B

123456789101112131415161718
  1. 'use strict';
  2. const _ = require('lodash');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. module.exports = options => {
  5. return async function tenantCheck(ctx, next) {
  6. const request = ctx.request;
  7. if (request.method !== 'GET') {
  8. const tenant = _.get(request, 'header.x-tenant');
  9. console.log(request.body);
  10. // 该中间只能通过master/与内容中的_tenant字段相同 的权限进行增删改
  11. if (tenant !== 'master') {
  12. const _tenant = _.get(request.body, '_tenant');
  13. if (!_tenant || _tenant !== tenant) { throw new BusinessError(ErrorCode.ACCESS_DENIED, '您没有访问的权限!'); }
  14. }
  15. }
  16. await next();
  17. };
  18. };