I already have an AuthService to authenticate user on login and AuthGuard to prevent access if not logged in.
Some pages I restrict access by UserProfile/Role but now I need to prevent actions on page.
I have roles like "Admin, Manager, Support and Agent", from greater to lower.
How to set level only to Manager or Above to edit content on a page that all can access (Support and Agent restrict to view only)?
This is my current canActivate method:
canActivate(route: ActivatedRouteSnapshot) {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (currentUser) {
// check if route is restricted by role
if (route.data.roles && route.data.roles.indexOf(currentUser.role) === -1) {
// role not authorised so redirect to home page
this.router.navigate(['/']);
return false;
}
// authorised so return true
return true;
}
// not logged in so redirect to login page
this.router.navigate(['auth/login']);
return false;
}
And this is my module routing.module.ts
const routes: Routes = [{
path: '',
component: ReportsComponent,
canActivateChild: [AuthGuard],
children: [
{
path: 'blocked-users',
component: BlockedUsersComponent,
data: { roles: [Role.admin, Role.manager, Role.suporte, Role.agent] },
children: [
{ ...
Need to fix these two topics:
1) Line data: { roles: [] }
I want to pass only the lower level (like Agent);
2) Inside component tell that only Manager can edit data (or just disable
a button if Role == Support or Agent)