Last active
July 28, 2020 07:15
-
-
Save bogdanq/2b040891c81f2c46aa0b51081378e386 to your computer and use it in GitHub Desktop.
Хелперы для гардов
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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