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

export type IAuthorityType =
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
7
  | undefined
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
8 9
  | string
  | string[]
10
  | Promise<boolean>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
11 12 13 14 15 16 17 18 19 20
  | ((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 => {
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
27 28 29 30
  console.log('authority:', authority);
  console.log('currentAuthority:', currentAuthority);
  console.log('target:', target);
  console.log('noMatch2', Exception);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
  // ๆฒกๆœ‰ๅˆคๅฎšๆƒ้™.้ป˜่ฎคๆŸฅ็œ‹ๆ‰€ๆœ‰
  // 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;
    }
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
45
    console.log('noMatch3', Exception);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    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
61
    return <PromiseRender<T, K> ok={target} error={Exception} promise={authority} />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
62 63 64 65 66 67 68
  }
  // Function ๅค„็†
  if (typeof authority === 'function') {
    try {
      const bool = authority(currentAuthority);
      // ๅ‡ฝๆ•ฐๆ‰ง่กŒๅŽ่ฟ”ๅ›žๅ€ผๆ˜ฏ Promise
      if (bool instanceof Promise) {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
69
        return <PromiseRender<T, K> ok={target} error={Exception} promise={bool} />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83
      }
      if (bool) {
        return target;
      }
      return Exception;
    } catch (error) {
      throw error;
    }
  }
  throw new Error('unsupported parameters');
};

export { checkPermissions };

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
84 85 86
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
87 88

export default check;