projectPermission.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import { useSelector } from 'react-redux'
  21. import { makeSelectCurrentProject } from '../selectors'
  22. import { IProjectPermission } from '../types'
  23. function useProjectPermission<T>(
  24. component: T,
  25. permissionNames: keyof IProjectPermission,
  26. type?: 2 | 3
  27. ): T
  28. function useProjectPermission<T>(
  29. component: T,
  30. permissionNames: keyof IProjectPermission | Array<keyof IProjectPermission>,
  31. type?: 2 | 3
  32. ): T[]
  33. function useProjectPermission<T>(
  34. component: T,
  35. permissionNames: keyof IProjectPermission | Array<keyof IProjectPermission>,
  36. type?: 2 | 3
  37. ) {
  38. const currentProject = useSelector(makeSelectCurrentProject())
  39. const names: Array<keyof IProjectPermission> = [].concat(permissionNames)
  40. const authorizedComponents = names.map((permissionName) => {
  41. if (!currentProject) {
  42. return () => null
  43. }
  44. const { permission } = currentProject
  45. const nextPermission = ({
  46. ...(currentProject?.permission ?? {}),
  47. ...{
  48. dataOverviiewPermission: 3,
  49. dataDictionaryPermission: 3,
  50. makeRulePermission: 3,
  51. qualityAuditPermission: 3,
  52. auditAnalysisPermission: 3
  53. }
  54. })
  55. const typePermission = nextPermission[permissionName]
  56. if (!typePermission) {
  57. return () => null
  58. }
  59. let hasPermission = false
  60. if (typeof typePermission === 'boolean') {
  61. hasPermission = typePermission
  62. } else {
  63. // 0 隐藏
  64. // 1 只读
  65. // 2 修改
  66. // 3 删除
  67. switch (+typePermission) {
  68. case 3:
  69. hasPermission = true
  70. break
  71. case 2:
  72. case 1:
  73. hasPermission = type ? typePermission >= type : true // default readonly
  74. break
  75. }
  76. }
  77. if (hasPermission) {
  78. return component
  79. }
  80. const nullComponent = Array.isArray(component)
  81. ? component.map(() => () => null)
  82. : () => null
  83. return nullComponent
  84. })
  85. return typeof permissionNames === 'string'
  86. ? authorizedComponents[0]
  87. : authorizedComponents
  88. }
  89. export default useProjectPermission