You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

permission.ts 1.9 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // /**
  2. // * 字符权限校验
  3. // * @param {Array} value 校验值
  4. // * @returns {Boolean}
  5. // */
  6. export function matchPerms(permissions: string[], value: string[]) {
  7. if (value && value instanceof Array && value.length > 0) {
  8. const permissionDatas = value;
  9. const all_permission = '*:*:*';
  10. const hasPermission = permissions.some((permission) => {
  11. return all_permission === permission || permissionDatas.includes(permission);
  12. });
  13. if (!hasPermission) {
  14. return false;
  15. }
  16. return true;
  17. }
  18. console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`);
  19. return false;
  20. }
  21. export function matchPerm(permissions: string[], value: string) {
  22. if (value && value.length > 0) {
  23. const permissionDatas = value;
  24. const all_permission = '*:*:*';
  25. const hasPermission = permissions.some((permission) => {
  26. return all_permission === permission || permissionDatas === permission;
  27. });
  28. if (!hasPermission) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`);
  34. return false;
  35. }
  36. export function matchPermission(permissions: string[] | undefined, value: any): boolean {
  37. if (permissions === undefined) return false;
  38. const type = typeof value;
  39. if (type === 'string') {
  40. return matchPerm(permissions, value);
  41. }
  42. return matchPerms(permissions, value);
  43. }
  44. /**
  45. * 角色权限校验
  46. * @param {Array} value 校验值
  47. * @returns {Boolean}
  48. */
  49. export function checkRole(roles: API.System.Role[] | undefined, value: string[]) {
  50. if (roles && value && value.length > 0) {
  51. for (let i = 0; i < roles?.length; i++) {
  52. for (let j = 0; j < value?.length; j++) {
  53. if (value[j] === roles[i].roleKey) {
  54. return true;
  55. }
  56. }
  57. }
  58. }
  59. console.error(`need roles! Like checkRole="['admin','editor']"`);
  60. return false;
  61. }