动态菜单和动态路由的 antd pro

Secured.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from 'react';
  2. import Exception from '../Exception/index';
  3. import CheckPermissions from './CheckPermissions';
  4. /**
  5. * 默认不能访问任何页面
  6. * default is "NULL"
  7. */
  8. const Exception403 = () => <Exception type="403" style={{ minHeight: 500, height: '80%' }} />;
  9. // Determine whether the incoming component has been instantiated
  10. // AuthorizedRoute is already instantiated
  11. // Authorized render is already instantiated, children is no instantiated
  12. // Secured is not instantiated
  13. const checkIsInstantiation = target => {
  14. if (!React.isValidElement(target)) {
  15. return target;
  16. }
  17. return () => target;
  18. };
  19. /**
  20. * 用于判断是否拥有权限访问此view权限
  21. * authority 支持传入 string ,funtion:()=>boolean|Promise
  22. * e.g. 'user' 只有user用户能访问
  23. * e.g. 'user,admin' user和 admin 都能访问
  24. * e.g. ()=>boolean 返回true能访问,返回false不能访问
  25. * e.g. Promise then 能访问 catch不能访问
  26. * e.g. authority support incoming string, funtion: () => boolean | Promise
  27. * e.g. 'user' only user user can access
  28. * e.g. 'user, admin' user and admin can access
  29. * e.g. () => boolean true to be able to visit, return false can not be accessed
  30. * e.g. Promise then can not access the visit to catch
  31. * @param {string | function | Promise} authority
  32. * @param {ReactNode} error 非必需参数
  33. */
  34. const authorize = (authority, error) => {
  35. /**
  36. * conversion into a class
  37. * 防止传入字符串时找不到staticContext造成报错
  38. * String parameters can cause staticContext not found error
  39. */
  40. let classError = false;
  41. if (error) {
  42. classError = () => error;
  43. }
  44. if (!authority) {
  45. throw new Error('authority is required');
  46. }
  47. return function decideAuthority(targer) {
  48. const component = CheckPermissions(authority, targer, classError || Exception403);
  49. return checkIsInstantiation(component);
  50. };
  51. };
  52. export default authorize;