简体   繁体   中英

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. So instead of checking the url as i am doing below, i would like to do something like 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.

i am trying to do this by check url but its casing problem when there is parameter included in 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')]

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'.

But would like to if there is any better way of doing role base authentication as above.

Have you tried route.component ? According to the docs , this should contain the component class being routed to.

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.

So you can just use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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