简体   繁体   English

Angular 4-CanActivate检查组件正在路由

[英]Angular 4 - CanActivate check component being route

In CanActivate is there a way to check which component is being route/requested by the user for authentication purpose. 在CanActivate中,有一种方法可以检查用户正在路由/请求哪个组件以进行身份​​验证。 So instead of checking the url as i am doing below, i would like to do something like if(route.component instanceof MyComponent). 因此,与其像我在下面所做的那样检查URL,不如做if(route.component instanceof MyComponent)之类的事情。

I am implementing role based access with ability to config each role to have different access level (view,edit etc..) for different component. 我正在实现基于角色的访问,并具有将每个角色配置为对不同组件具有不同访问级别(视图,编辑等)的能力。 So the easiest thing I thought would be to get current component being route and then check access level for that component rather than creating different AuthGard for each component. 因此,我认为最简单的方法是获取当前组件的路由,然后检查该组件的访问级别,而不是为每个组件创建不同的AuthGard。

i am trying to do this by check url but its casing problem when there is parameter included in url. 我试图通过检查URL来做到这一点,但是当URL中包含参数时它的大小写问题。

 canActivate(route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot) {       
            let ac = this.auth.getAccessConfig();
            switch (state.url) {
                case '/site':
                    return ac.some(x => x.moduleID == AccessModulesEnum.Site && x.permissionID == AccessPermissionEnum.View);
                case '/site/new':
                    return ac.some(x => x.moduleID == AccessModulesEnum.Site && x.permissionID == AccessPermissionEnum.Modify);
                case '/site/:id': ***//this doesn't work as :id could be anything***
                    return ac.some(x => x.moduleID == AccessModulesEnum.SiteDetail && x.permissionID == AccessPermissionEnum.View);
                case '/reports/milestone':
                    return ac.some(x => x.moduleID == AccessModulesEnum.MilestoneReport && x.permissionID == AccessPermissionEnum.View);
                case '/reports/audit':
                    return ac.some(x => x.moduleID == AccessModulesEnum.AuditReport && x.permissionID == AccessPermissionEnum.View);
                case '/reports/schedule':
                    return ac.some(x => x.moduleID == AccessModulesEnum.ReportScheduler && x.permissionID == AccessPermissionEnum.View);
                case '/importer':
                    return ac.some(x => x.moduleID == AccessModulesEnum.Import && x.permissionID == AccessPermissionEnum.View);
                case '/reports':
                    return ac.some(x => x.moduleID == AccessModulesEnum.SiteReport && x.permissionID == AccessPermissionEnum.View);
                case '/admin/users':
                    return ac.some(x => x.moduleID == AccessModulesEnum.User && x.permissionID == AccessPermissionEnum.View);
                case '/admin/tables':
                    return ac.some(x => x.moduleID == AccessModulesEnum.ReferenceTable && x.permissionID == AccessPermissionEnum.View);
                default:
                    return false;
            }
        });
    }

Another question: is it possible to call AuthGard with parameter from Routes configuration like { path: 'site/:id', component:SiteDetail,canActivate:[(callCustomeAuthGard('SiteDetailComponent')] 另一个问题:是否可以使用来自Routes配置的参数调用AuthGard,例如{path:'site /:id',component:SiteDetail,canActivate:[(callCustomeAuthGard('SiteDetailComponent')]

Thanks 谢谢

have found solution for the above problem using route.routeConfig.path to check for route path instead of url which will be like 'site/:id' instead of 'site/2'. 已使用route.routeConfig.path而不是url(类似于“ site /:id”而不是“ site / 2”)来检查路由路径,从而找到了解决上述问题的解决方案。

But would like to if there is any better way of doing role base authentication as above. 但是,是否希望有更好的方法来进行如上所述的基于角色的身份验证。

Have you tried route.component ? 您是否尝试过route.component According to the docs , this should contain the component class being routed to. 根据docs ,其中应包含要路由到的组件类。

If so, the activation guard could ask the component class which permissions it requires. 如果是这样,则激活保护人员可以询问组件类它需要哪些权限。

Your ActivatedRouteSnapshot (the route parameter you use in the canActivate function) contains the component. 您的ActivatedRouteSnapshot(在canActivate函数中使用的route参数)包含该组件。

So you can just use: 因此,您可以使用:

if (route.component === MyComponent) {
  // Do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM