2021-05-04 09:59:42 +02:00
|
|
|
import { FC } from 'react';
|
2021-04-20 19:13:31 +02:00
|
|
|
|
2021-05-04 09:59:42 +02:00
|
|
|
import AccessContext from '../../contexts/AccessContext';
|
|
|
|
import { ADMIN } from './permissions';
|
2021-04-20 19:13:31 +02:00
|
|
|
|
|
|
|
// TODO: Type up redux store
|
|
|
|
interface IAccessProvider {
|
|
|
|
store: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IPermission {
|
2021-05-04 09:59:42 +02:00
|
|
|
permission: string;
|
|
|
|
project: string | null;
|
2021-04-20 19:13:31 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 09:59:42 +02:00
|
|
|
const AccessProvider: FC<IAccessProvider> = ({ store, children }) => {
|
|
|
|
const hasAccess = (permission: string, project: string) => {
|
|
|
|
const permissions = store.getState().user.get('permissions') || [];
|
|
|
|
|
|
|
|
const result = permissions.some((p: IPermission) => {
|
|
|
|
if (p.permission === ADMIN) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-04 21:25:35 +02:00
|
|
|
|
2021-05-04 09:59:42 +02:00
|
|
|
if (p.permission === permission && p.project === project) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-04 21:25:35 +02:00
|
|
|
|
|
|
|
if (p.permission === permission && project === undefined) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-04 09:59:42 +02:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
const context = { hasAccess };
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AccessContext.Provider value={context}>
|
|
|
|
{children}
|
|
|
|
</AccessContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AccessProvider;
|