()
| 2 | import { useConfig } from '@/store/context/ConfigContext' |
| 3 | |
| 4 | export const useAuth = () => { |
| 5 | const { isOpenSource } = useConfig() |
| 6 | const permissions = useSelector((state) => state.auth.permissions) |
| 7 | const features = useSelector((state) => state.auth.features) |
| 8 | const isGlobal = useSelector((state) => state.auth.isGlobal) |
| 9 | const currentUser = useSelector((state) => state.auth.user) |
| 10 | |
| 11 | const hasPermission = (permissionId) => { |
| 12 | if (isOpenSource || isGlobal) { |
| 13 | return true |
| 14 | } |
| 15 | if (!permissionId) return false |
| 16 | const permissionIds = permissionId.split(',') |
| 17 | if (permissions && permissions.length) { |
| 18 | return permissionIds.some((permissionId) => permissions.includes(permissionId)) |
| 19 | } |
| 20 | return false |
| 21 | } |
| 22 | |
| 23 | const hasAssignedWorkspace = (workspaceId) => { |
| 24 | if (isOpenSource || isGlobal) { |
| 25 | return true |
| 26 | } |
| 27 | const activeWorkspaceId = currentUser?.activeWorkspaceId || '' |
| 28 | if (workspaceId === activeWorkspaceId) { |
| 29 | return true |
| 30 | } |
| 31 | return false |
| 32 | } |
| 33 | |
| 34 | const hasDisplay = (display) => { |
| 35 | if (!display) { |
| 36 | return true |
| 37 | } |
| 38 | |
| 39 | // if it has display flag, but user has no features, then it should not be displayed |
| 40 | if (!features || Array.isArray(features) || Object.keys(features).length === 0) { |
| 41 | return false |
| 42 | } |
| 43 | |
| 44 | // check if the display flag is in the features |
| 45 | if (Object.hasOwnProperty.call(features, display)) { |
| 46 | const flag = features[display] === 'true' || features[display] === true |
| 47 | return flag |
| 48 | } |
| 49 | |
| 50 | return false |
| 51 | } |
| 52 | |
| 53 | return { hasPermission, hasAssignedWorkspace, hasDisplay } |
| 54 | } |
no test coverage detected