2020-03-29 21:19:17 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { useDeepMemo } from '@/plugins/useDeepMemo';
|
|
|
|
|
|
|
|
export const usePermissions = (action: string | string[]): boolean[] => {
|
|
|
|
const userPermissions = ServerContext.useStoreState(state => state.server.permissions);
|
|
|
|
|
|
|
|
return useDeepMemo(() => {
|
2020-03-30 04:30:33 +00:00
|
|
|
if (userPermissions[0] === '*') {
|
2020-03-30 04:52:18 +00:00
|
|
|
return Array(Array.isArray(action) ? action.length : 1).fill(true);
|
2020-03-30 04:30:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 21:19:17 +00:00
|
|
|
return (Array.isArray(action) ? action : [ action ])
|
|
|
|
.map(permission => (
|
2020-03-30 04:30:33 +00:00
|
|
|
// Allows checking for any permission matching a name, for example files.*
|
|
|
|
// will return if the user has any permission under the file.XYZ namespace.
|
|
|
|
(
|
|
|
|
permission.endsWith('.*') &&
|
|
|
|
permission !== 'websocket.*' &&
|
|
|
|
userPermissions.filter(p => p.startsWith(permission.split('.')[0])).length > 0
|
|
|
|
) ||
|
|
|
|
// Otherwise just check if the entire permission exists in the array or not.
|
|
|
|
userPermissions.indexOf(permission) >= 0
|
|
|
|
));
|
2020-03-29 21:19:17 +00:00
|
|
|
}, [ action, userPermissions ]);
|
|
|
|
};
|