This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-admin/lib/utils/has-privileges.ts

29 lines
746 B
TypeScript
Raw Permalink Normal View History

2022-08-29 18:09:28 +00:00
import { CurrentUserDto } from '../types/user-response.interface';
export default function userHasPrivileges(
user: CurrentUserDto | undefined,
...privileges: (string | string[])[]
): boolean {
if (!user) {
return false;
}
const userPrivileges = user?.privileges || [];
const withOrLogic = privileges.some((entry) => Array.isArray(entry));
if (withOrLogic) {
return privileges.some((entry) => {
if (Array.isArray(entry)) {
return entry.every((item) =>
userPrivileges.find((name) => name === item)
);
} else {
return userPrivileges.find((name) => name === entry);
}
});
}
return privileges.every((item) =>
userPrivileges.find((name) => name === item)
);
}