Closed winsonet closed 1 year ago
Hi did You try something ike this ?
permissions: {
only: ['p1','p2'],
redirectTo: {
p1: '/route/p1',
p2: '/route/p2',
default: 'login'
}
}
If this doesnt work its possible to create customGuard and use ngxPermissionsGuard in the constructor of your guard to just check permissions.
thanks for your reply, but unfortunately, it will be dead looping and redirect to the page with this setting and let the browser hang, can you show me some simple how to create customGuard and use ngxPermissionsGuard ?
many thanks!
The main idea is this one If You want to see video https://www.youtube.com/watch?v=d92LXFfdrJc&list=PLHw3vRAUIqUOLqBpoR-eYvZxiPGPh18y5&index=8&t=1s
and this is code for easier understanding
@Injectable({
providedIn: 'root'
})
export class CustomGuard implements CanActivate {
constructor(private ngxPermissionsGuard: NgxPermissionsGuard,
private router: Router) {
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const alwaysData = route.data['permissions'].always
const notFoundData = route.data['permissions'].notFound
const alwaysRequestData: any = {
...route,
data: {
permissions: {
only: alwaysData
}
}
}
const notFoundRequestData: any = {
...route,
data: {
permissions: {
only: notFoundData
}
}
}
const alwaysGuard = this.ngxPermissionsGuard.canActivate(alwaysRequestData, state) as Promise<boolean>
const notFoundGuard = this.ngxPermissionsGuard.canActivate(notFoundRequestData, state) as Promise<boolean>
return alwaysGuard.then((data) => {
if (!data) {
this.router.navigate(['denied'])
return Promise.reject();
}
return notFoundGuard
}).then((data) => {
if (!data) {
this.router.navigate(['not-found'])
}
return data;
});
}
}
I will try and study it, many thanks!
I have solved the problem, thanks!
I'm submitting a...
Current behavior
Can't check the permissions base on difference URL parameters
Expected behavior
For example, there are below URL with difference parameters
and I set the router code as below
can I set the
p1
grade user only can access URL withp1
and thep2
user only can access the URL withp2
and so on...?Thanks!
Environment
Angular version: 15.0.4 ngx-permissions version: 14.0.0