29 lines
746 B
TypeScript
29 lines
746 B
TypeScript
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)
|
|
);
|
|
}
|