CheckPermissions.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React from 'react';
  2. import { CURRENT } from './renderAuthorize';
  3. // eslint-disable-next-line import/no-cycle
  4. import PromiseRender from './PromiseRender';
  5. export type IAuthorityType =
  6. | undefined
  7. | string
  8. | string[]
  9. | Promise<boolean>
  10. | ((currentAuthority: string | string[]) => IAuthorityType);
  11. /**
  12. * @en-US
  13. * General permission check method
  14. * Common check permissions method
  15. * @param {Permission judgment} authority
  16. * @param {Your permission | Your permission description} currentAuthority
  17. * @param {Passing components} target
  18. * @param {no pass components | no pass components} Exception
  19. * -------------------------------------------------------
  20. * @zh-CN
  21. * 通用权限检查方法 Common check permissions method
  22. *
  23. * @param { 权限判定 | Permission judgment } authority
  24. * @param { 你的权限 | Your permission description } currentAuthority
  25. * @param { 通过的组件 | Passing components } target
  26. * @param { 未通过的组件 | no pass components } Exception
  27. */
  28. const checkPermissions = <T, K>(
  29. authority: IAuthorityType,
  30. currentAuthority: string | string[],
  31. target: T,
  32. Exception: K,
  33. ): T | K | React.ReactNode => {
  34. // No judgment permission. View all by default
  35. // Retirement authority, return target;
  36. if (!authority) {
  37. return target;
  38. }
  39. // Array processing
  40. if (Array.isArray(authority)) {
  41. if (Array.isArray(currentAuthority)) {
  42. if (currentAuthority.some((item) => authority.includes(item))) {
  43. return target;
  44. }
  45. } else if (authority.includes(currentAuthority)) {
  46. return target;
  47. }
  48. return Exception;
  49. }
  50. // Deal with string
  51. if (typeof authority === 'string') {
  52. if (Array.isArray(currentAuthority)) {
  53. if (currentAuthority.some((item) => authority === item)) {
  54. return target;
  55. }
  56. } else if (authority === currentAuthority) {
  57. return target;
  58. }
  59. return Exception;
  60. }
  61. // Deal with promise
  62. if (authority instanceof Promise) {
  63. return <PromiseRender<T, K> ok={target} error={Exception} promise={authority} />;
  64. }
  65. // Deal with function
  66. if (typeof authority === 'function') {
  67. const bool = authority(currentAuthority);
  68. // The return value after the function is executed is Promise
  69. if (bool instanceof Promise) {
  70. return <PromiseRender<T, K> ok={target} error={Exception} promise={bool} />;
  71. }
  72. if (bool) {
  73. return target;
  74. }
  75. return Exception;
  76. }
  77. throw new Error('unsupported parameters');
  78. };
  79. export { checkPermissions };
  80. function check<T, K>(authority: IAuthorityType, target: T, Exception: K): T | K | React.ReactNode {
  81. return checkPermissions<T, K>(authority, CURRENT, target, Exception);
  82. }
  83. export default check;