-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCTOR-834: use self endpoint user permissions if roles interface pres…
…ent, by refactor useUserTenantPermissions hook to achieve that
- Loading branch information
Showing
4 changed files
with
132 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useQuery } from 'react-query'; | ||
|
||
import { useStripes } from '../StripesContext'; | ||
import { useNamespace } from '../components'; | ||
import useOkapiKy from '../useOkapiKy'; | ||
|
||
const INITIAL_DATA = []; | ||
|
||
const useUserSelfTenantPermissions = ( | ||
{ tenantId }, | ||
options = {}, | ||
) => { | ||
const stripes = useStripes(); | ||
const ky = useOkapiKy(); | ||
const api = ky.extend({ | ||
hooks: { | ||
beforeRequest: [(req) => req.headers.set('X-Okapi-Tenant', tenantId)] | ||
} | ||
}); | ||
const [namespace] = useNamespace({ key: 'user-self-permissions' }); | ||
|
||
const user = stripes.user.user; | ||
|
||
const { | ||
isFetching, | ||
isLoading, | ||
data, | ||
} = useQuery( | ||
[namespace, user?.id, tenantId], | ||
({ signal }) => { | ||
return api.get( | ||
'users-keycloak/_self', | ||
{ signal }, | ||
).json(); | ||
}, | ||
{ | ||
enabled: Boolean(user?.id && tenantId) && stripes.hasInterface('users-keycloak'), | ||
keepPreviousData: true, | ||
...options, | ||
}, | ||
); | ||
|
||
return ({ | ||
isFetching, | ||
isLoading, | ||
userPermissions: data?.permissions.permissions || INITIAL_DATA, | ||
totalRecords: data?.permissions.permissions.length || 0, | ||
}); | ||
}; | ||
|
||
export default useUserSelfTenantPermissions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useQuery } from 'react-query'; | ||
|
||
import { useStripes } from '../StripesContext'; | ||
import { useNamespace } from '../components'; | ||
import useOkapiKy from '../useOkapiKy'; | ||
|
||
const INITIAL_DATA = []; | ||
|
||
const useUserTenantPermissionNames = ( | ||
{ tenantId }, | ||
options = {}, | ||
) => { | ||
const stripes = useStripes(); | ||
const ky = useOkapiKy(); | ||
const api = ky.extend({ | ||
hooks: { | ||
beforeRequest: [(req) => req.headers.set('X-Okapi-Tenant', tenantId)] | ||
} | ||
}); | ||
const [namespace] = useNamespace({ key: 'user-affiliation-permissions' }); | ||
|
||
const user = stripes.user.user; | ||
|
||
const searchParams = { | ||
full: 'true', | ||
indexField: 'userId', | ||
}; | ||
|
||
const { | ||
isFetching, | ||
isLoading, | ||
data = {}, | ||
} = useQuery( | ||
[namespace, user?.id, tenantId], | ||
({ signal }) => { | ||
return api.get( | ||
`perms/users/${user.id}/permissions`, | ||
{ | ||
searchParams, | ||
signal, | ||
}, | ||
).json(); | ||
}, | ||
{ | ||
enabled: Boolean(user?.id && tenantId), | ||
keepPreviousData: true, | ||
...options, | ||
}, | ||
); | ||
|
||
return ({ | ||
isFetching, | ||
isLoading, | ||
userPermissions: data.permissionNames || INITIAL_DATA, | ||
totalRecords: data.totalRecords, | ||
}); | ||
}; | ||
|
||
export default useUserTenantPermissionNames; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters