5

i've seen some issues on this specific issue.

I'm lately only working on angular2 projects. Anyhow I'm rolling into an issue right now. In the deprecated Router I added my User Role in the data part of the route, I had overriden the routerOutlet so I could check this value before activating a route).

Currently we can conform to the CanActivate Class/Interface row w/e we can call it.

Right now I would like to be able to provide Permissions (which I've slapped into an Enum) to such CanActivate classes. Before I was simply checking if the user was logged in by accessing my SessionService, I however now need to check if a user has certain permissions.

As I see it now I have to implement a new class which implements CanActivate for every single permission. Right now this would mean I'd have 7. We're however in an early stage of this exact project and Without too much thought I could improve this to 11.

What I can see before me is an implementation which will only cost me about 3 - 5 lines per such canActivate implementation. However I would feel more helped by an implementation where I could provide properties or parameters.

Not to think of the situation I'd have to implement classes like:

CanActivateIfUserHasPermissionsViewContentAndViewUsers or even longer.

Note in my situation permissions are basically multiple actions. Like View all Content elements, Or edit all of them etc.

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
  • Good question :) I guess it can be done from another side. You can import next route into `CanActivateGuard` and see if user has permission. – Lyubimov Roman Aug 18 '16 at 16:12
  • You will get a path right, not the route object. So what happens when it's a child route, will that still match? Does your guard also know about the child routes? Or would you be scanning your submodules for route config. Haven't checked that yet though. – Mathijs Segers Aug 19 '16 at 05:15
  • I don't have a code to check now. But I thnik you will get `ActivatedRouteSnapshot`https://angular.io/docs/ts/latest/api/router/index/ActivatedRouteSnapshot-interface.html – Lyubimov Roman Aug 19 '16 at 05:22
  • I'm using that property inside a service now, which I populate on module construction – Mathijs Segers Aug 23 '16 at 09:37

1 Answers1

1

So I didn't find a way to implement my CanActivate with params.

I did however find a workaround which seems fine for me now. I have a service in which I can load my routes on Module construct. (I just add it in the constructor of the module).

My Routes now contain data { permissions: [My values here] }. Which are checked in the canActive hook, since the canActivate implementation also uses the same service.

My solution is based on my discussion on this other question: Angular2 RC5 Cross-Module Provider / Scanning

Community
  • 1
  • 1
Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
  • How do you access your data in CanActivate hook? If I try to do that I only have access to that if Im in a component already. When Im in a CanActivate guard I cant access the data, specified by the route. – Yaroslav Yakovlev Sep 19 '16 at 00:07
  • Hi, as stated, I load the Routes into a service. on construction of my application. So when the NgModule loads. This allows me when activating, to get the full information of the route from this service based on the target path. (I made a method which returns my route based on this). That way I get the full data and am allowed to check if it validates – Mathijs Segers Sep 19 '16 at 07:45