2026-10-05 18:10:18 -04:00
|
|
|
import { useCurrentServerPermissions } from '@/api/server/queries';
|
2020-03-29 14:19:17 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
export const hasPermission = (userPermissions: readonly string[], permission: string): boolean => {
|
|
|
|
|
if (userPermissions[0] === '*') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (permission.endsWith('.*')) {
|
|
|
|
|
const prefix = permission.slice(0, -1);
|
|
|
|
|
|
|
|
|
|
return userPermissions.some((granted) => granted.startsWith(prefix));
|
|
|
|
|
}
|
2020-03-29 14:19:17 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return userPermissions.includes(permission);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const usePermissions = (action: string | string[]): boolean[] => {
|
|
|
|
|
const userPermissions = useCurrentServerPermissions();
|
2020-03-29 21:30:33 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return (Array.isArray(action) ? action : [action]).map((permission) => hasPermission(userPermissions, permission));
|
2020-03-29 14:19:17 -07:00
|
|
|
};
|