test/mocks/cockpit: add permission function
Add the types relevant types for the `cockpit.permission` function so that we can establish whether the user has the correct privileges to run the frontend on-prem.
This commit is contained in:
parent
96eb0fe767
commit
49b2196c7b
3 changed files with 43 additions and 0 deletions
26
src/Utilities/useIsCockpitAdmin.tsx
Normal file
26
src/Utilities/useIsCockpitAdmin.tsx
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
|
||||
import cockpit from 'cockpit';
|
||||
|
||||
export const useIsCockpitAdmin = () => {
|
||||
const [isAdmin, setIsAdmin] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const permission = cockpit.permission({ admin: true });
|
||||
|
||||
const onChangeListener = () => {
|
||||
setIsAdmin(permission.allowed);
|
||||
};
|
||||
|
||||
permission.addEventListener('changed', onChangeListener);
|
||||
|
||||
// Check the initial state
|
||||
onChangeListener();
|
||||
|
||||
return () => {
|
||||
permission.removeEventListener('changed', onChangeListener);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return isAdmin;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue