在MethodDecorator中进行路由

时间:2019-06-25 10:07:46

标签: angular

我在api调用上有一个方法装饰器,用于确定用户是否有权执行api调用

@AuthDecorator([Constants.perm_read_document_type_indexid])
getDocumentTypes(Id: number): Observable<DocumentType[]> {
  return this.http.get<DocumentType[]>(`${environment.apiUrl}/documenttypes? 
Id=${indexId}`);
}

Decorator接受权限类型并检查本地存储,以查看用户是否具有该权限。

export function AuthDecorator(permissions: string[]): MethodDecorator  {
 return function auth(target: Function, key: string, descriptor: any) {    
     const originalMethod = descriptor.value; 
     descriptor.value =  function (...args: any[]) {

        var scopes = new 
 localStorage.getItem(localstorage_permissions)).scope.split(" ");

        if(permissions.every(v => scopes.includes(v)))
        {
            return originalMethod.apply(this, args);
        }
        else{

            // redirect to unauth route
        }
    }
    return descriptor;      
   }
  }

如果用户没有适当的权限,我想重定向到错误页面

我可以通过设置window.location.href来做到这一点,但我想在内置路由器中使用角度。是否可以将路由器传递给MethodDecorater?如果不是,最好的方法是什么?

0 个答案:

没有答案