import { Unauthorized } from '../http/errors'; import { Request, Response } from '../http'; // async decorators aren't supported yet, so just use strings. export function RoleAuth(roles: string[]) { return (target: any, key: string, callable: PropertyDescriptor) => { callable = callable || Object.getOwnPropertyDescriptor(target, key); let func = callable.value; callable.value = function () { let args = [].slice.call(arguments); // the meat of the decorator let req = args[0]; if (req.data && roles.indexOf(req.data.role) < 0) { throw new Unauthorized( 'You do not have permissions to access this resource.' ); } // end meat return func.apply(this, args); }; return callable; } }