CheckPermissions.tsx 2.31 KB
Newer Older
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
1 2 3 4 5 6
import React from 'react';
// eslint-disable-next-line import/no-cycle
import PromiseRender from './PromiseRender';
import { CURRENT } from './renderAuthorize';

export type IAuthorityType =
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
7
  | undefined
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
8 9 10 11 12 13 14 15 16 17 18 19 20
  | string
  | string[]
  | Promise<any>
  | ((currentAuthority: string | string[]) => IAuthorityType);

/**
 * ้€š็”จๆƒ้™ๆฃ€ๆŸฅๆ–นๆณ•
 * Common check permissions method
 * @param { ๆƒ้™ๅˆคๅฎš | Permission judgment } authority
 * @param { ไฝ ็š„ๆƒ้™ | Your permission description } currentAuthority
 * @param { ้€š่ฟ‡็š„็ป„ไปถ | Passing components } target
 * @param { ๆœช้€š่ฟ‡็š„็ป„ไปถ | no pass components } Exception
 */
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
21
const checkPermissions = <T, K>(
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
22 23
  authority: IAuthorityType,
  currentAuthority: string | string[],
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
24 25 26
  target: T,
  Exception: K,
): T | K | React.ReactNode => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  // ๆฒกๆœ‰ๅˆคๅฎšๆƒ้™.้ป˜่ฎคๆŸฅ็œ‹ๆ‰€ๆœ‰
  // Retirement authority, return target;
  if (!authority) {
    return target;
  }
  // ๆ•ฐ็ป„ๅค„็†
  if (Array.isArray(authority)) {
    if (Array.isArray(currentAuthority)) {
      if (currentAuthority.some(item => authority.includes(item))) {
        return target;
      }
    } else if (authority.includes(currentAuthority)) {
      return target;
    }
    return Exception;
  }
  // string ๅค„็†
  if (typeof authority === 'string') {
    if (Array.isArray(currentAuthority)) {
      if (currentAuthority.some(item => authority === item)) {
        return target;
      }
    } else if (authority === currentAuthority) {
      return target;
    }
    return Exception;
  }
  // Promise ๅค„็†
  if (authority instanceof Promise) {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
56
    return <PromiseRender<T, K> ok={target} error={Exception} promise={authority} />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
57 58 59 60 61 62 63
  }
  // Function ๅค„็†
  if (typeof authority === 'function') {
    try {
      const bool = authority(currentAuthority);
      // ๅ‡ฝๆ•ฐๆ‰ง่กŒๅŽ่ฟ”ๅ›žๅ€ผๆ˜ฏ Promise
      if (bool instanceof Promise) {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
64
        return <PromiseRender<T, K> ok={target} error={Exception} promise={bool} />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
      }
      if (bool) {
        return target;
      }
      return Exception;
    } catch (error) {
      throw error;
    }
  }
  throw new Error('unsupported parameters');
};

export { checkPermissions };

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
79 80 81
function check<T, K>(authority: IAuthorityType, target: T, Exception: K): T | K | React.ReactNode {
  return checkPermissions<T, K>(authority, CURRENT, target, Exception);
}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
82 83

export default check;