Skip to content

Instantly share code, notes, and snippets.

@bogdanq
Last active July 28, 2020 07:15
Show Gist options
  • Save bogdanq/2b040891c81f2c46aa0b51081378e386 to your computer and use it in GitHub Desktop.
Save bogdanq/2b040891c81f2c46aa0b51081378e386 to your computer and use it in GitHub Desktop.
Хелперы для гардов
interface IUserContext {
user: User_user;
}
export function onlyAuth(context: IUserContext) {
return Boolean(context.user);
}
export function onlyAdAgent({ user }: IUserContext) {
if (user && user.roles) {
return user.roles.includes(RolesNames.adAgent);
}
return false;
}
export function onlyManager({ user }: IUserContext) {
if (user && user.roles) {
return user.roles.includes(RolesNames.manager);
}
return false;
}
export function anyRole(roles: Array<(context: IUserContext) => boolean>) {
return (context: IUserContext) => {
return roles.some(role => role(context));
};
}
export function allRoles(roles: Array<(context: IUserContext) => boolean>) {
return (context: IUserContext) => {
return roles.every(role => role(context));
};
}
export function hasRole(role: string) {
return ({ user }: IUserContext) => {
if (user && user.roles) {
return user.roles.includes(role);
}
return false;
};
}
export function exceptRoles(roles: Array<(context: IUserContext) => boolean>) {
return (context: IUserContext) => {
return roles.every(role => !role(context));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment