CheckPermissions.js 2.34 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2
import React from 'react';
import PromiseRender from './PromiseRender';
陈帅's avatar
陈帅 committed
3
import { CURRENT } from './renderAuthorize';
afc163's avatar
afc163 committed
4 5

function isPromise(obj) {
6 7 8 9 10
  return (
    !!obj &&
    (typeof obj === 'object' || typeof obj === 'function') &&
    typeof obj.then === 'function'
  );
afc163's avatar
afc163 committed
11 12
}

ddcat1115's avatar
ddcat1115 committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/**
 * 通用权限检查方法
 * Common check permissions method
 * @param { 权限判定 Permission judgment type string |array | Promise | Function } authority
 * @param { 你的权限 Your permission description  type:string} currentAuthority
 * @param { 通过的组件 Passing components } target
 * @param { 未通过的组件 no pass components } Exception
 */
const checkPermissions = (authority, currentAuthority, target, Exception) => {
  // 没有判定权限.默认查看所有
  // Retirement authority, return target;
  if (!authority) {
    return target;
  }
  // 数组处理
afc163's avatar
afc163 committed
28 29
  if (Array.isArray(authority)) {
    if (authority.indexOf(currentAuthority) >= 0) {
ddcat1115's avatar
ddcat1115 committed
30 31
      return target;
    }
32 33 34 35 36 37 38 39
    if (Array.isArray(currentAuthority)) {
      for (let i = 0; i < currentAuthority.length; i += 1) {
        const element = currentAuthority[i];
        if (authority.indexOf(element) >= 0) {
          return target;
        }
      }
    }
ddcat1115's avatar
ddcat1115 committed
40 41 42 43
    return Exception;
  }

  // string 处理
afc163's avatar
afc163 committed
44
  if (typeof authority === 'string') {
ddcat1115's avatar
ddcat1115 committed
45 46 47
    if (authority === currentAuthority) {
      return target;
    }
48 49 50 51 52 53 54 55
    if (Array.isArray(currentAuthority)) {
      for (let i = 0; i < currentAuthority.length; i += 1) {
        const element = currentAuthority[i];
        if (authority.indexOf(element) >= 0) {
          return target;
        }
      }
    }
ddcat1115's avatar
ddcat1115 committed
56 57 58 59
    return Exception;
  }

  // Promise 处理
afc163's avatar
afc163 committed
60
  if (isPromise(authority)) {
61
    return <PromiseRender ok={target} error={Exception} promise={authority} />;
ddcat1115's avatar
ddcat1115 committed
62 63 64
  }

  // Function 处理
afc163's avatar
afc163 committed
65
  if (typeof authority === 'function') {
ddcat1115's avatar
ddcat1115 committed
66
    try {
陈帅's avatar
陈帅 committed
67
      const bool = authority(currentAuthority);
68 69 70 71
      // 函数执行后返回值是 Promise
      if (isPromise(bool)) {
        return <PromiseRender ok={target} error={Exception} promise={bool} />;
      }
ddcat1115's avatar
ddcat1115 committed
72 73 74 75 76 77 78 79 80 81 82 83 84
      if (bool) {
        return target;
      }
      return Exception;
    } catch (error) {
      throw error;
    }
  }
  throw new Error('unsupported parameters');
};

export { checkPermissions };

85 86
const check = (authority, target, Exception) =>
  checkPermissions(authority, CURRENT, target, Exception);
ddcat1115's avatar
ddcat1115 committed
87 88

export default check;