Comments (6)
@timhoekstra @khodabandelu After careful thinking its much better to add Your code to the documentation so it will be easier for people to create their own custom guard.It will solve more problems. Thank You for helping!
from ngx-permissions.
@timhoekstra This is great idea. Will look into it
from ngx-permissions.
i need default value for all canload and canActivate to redirect to page 403 so that I dont have to duplicate redirecTo option to set route 403 for permission exception whereever i use NgxPermissionGuard .
for solve this problem i have to create own permmissionGuard and override NgxPermissionsRouterData before call NgxPermissionGuard methods.
my own Guard that I use NgxPermissionsGuard methods to support this feature.
@Injectable({providedIn: 'root'})
export class PermissionsGuard implements CanActivate {
constructor(private ngxPermissionsGuard: NgxPermissionsGuard, private authService: AuthService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> | boolean {
if (route && route.data && route.data.permissions as NgxPermissionsRouterData && route.data.permissions.redirectTo == undefined) {
route.data.permissions.redirectTo = '/error/error-403';
}
if (!this.checkAuth()) {
return false
} else {
return this.ngxPermissionsGuard.canActivate(route, state);
}
}
canLoad(route: Route): boolean | Observable<boolean> | Promise<boolean> {
if (route && route.data && route.data.permissions as NgxPermissionsRouterData && route.data.permissions.redirectTo == undefined) {
route.data.permissions.redirectTo = '/error/error-403';
}
if (!this.checkAuth()) {
return false
} else {
return this.ngxPermissionsGuard.canLoad(route);
}
}
checkAuth() {
const currentUser = this.authService.currentUserValue;
if (currentUser) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.authService.logout();
return false;
}
}
from ngx-permissions.
This is use case when in the project You just want to block user coming to page, not to redirect him somewhere
Do you mean having to specify it on every route as empty?
I would expect the following:
defaultRedirectTo specified, no redirectTo on route -> defaultRedirectTo triggers
defaultRedirectTo specified, redirectTo on route -> redirectTo on route triggers
If you want to keep the option open to block the incoming user then yes I think it's a good idea to be able to specify an empty object to block the user either as the defaultRedirectTo or the route redirectTo.
I would like to remark that in the meantime I have also taken the approach of @khodabandelu and created a new guard that forwards the canActivate etc. calls to NgxPermissionsGuard and put some default behavior in there. That also solves the problem.
from ngx-permissions.
I will try to expand a bit on this question and add the necessary information for the feature request according to the new issue guidelines since I have it as well.
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[x ] Feature request
[ ] Documentation issue or request
Current behavior
What I'm looking for is a way to configure a default behavior for when the permissions get rejected to handle the permission rejection and in my use-case redirect to a specific route.
What I could currently find is that there is the redirectTo property that can be configured in the routing configuration (https://github.com/AlexKhymenko/ngx-permissions#multiple-redirection-rules).
To achieve the same default behavior for all routes that have permissions defined then this would mean having to have a central function that I provide on each route that gets defined.
Kind of like is documented.
export function defaultRedirectTo(rejectedPermissionName: string, activateRouteSnapshot: ActivatedRouteSnapshot, routeStateSnapshot: RouterStateSnapshot) => {
return 'login';
}
permissions: {
redirectTo: {
path: defaultRedirectTo
}
}
However it's easy to forget to define this in some places which leads to a difference in behavior in the application. This is something I would like to prevent and I would like the default behavior to be predictable by default.
Expected behavior
I was thinking a good place for it would be a default redirectTo on the NgxPermissionsModule.forRoot which gets overriden by redirectTo on the specific route permissions?
NgxPermissionsModule.forRoot({
defaultRedirectTo: myFunction
})
This approach could then also be applied to
NgxPermissionsModule.forChild({
defaultRedirectTo: myFunction
})
Minimal reproduction of the problem with instructions
Environment
Angular version: X.Y.Z
ngx-permissions version: X.Y.Z
Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
from ngx-permissions.
@khodabandelu @timhoekstra What do You think, is it ok If You still have to specify redirectTo object (even empty) ?
permissions: {
only: 'ADMIN1',
redirectTo: {}
} as NgxPermissionsRouterData
This is use case when in the project You just want to block user coming to page, not to redirect him somewhere
from ngx-permissions.
Related Issues (20)
- SyntaxError: Cannot use import statement outside a module
- Help Ukraine any help
- Roles With Permissions HOT 2
- Guard is not working with angular SSR
- `getPermission` returns an incomplete type HOT 2
- Can't bind to 'ngxPermissionsOnly' since it isn't a known property of 'div' HOT 6
- Add flag to disable ngx-permissions in runtime HOT 4
- Cannot find module 'ngx-permissions' from HOT 1
- ngx-permissions not working in child-components (features) HOT 1
- [feaure] - NgxPermissionsGuard - implement "canMatch" HOT 2
- routeStateSnapshot in redirect is undefined HOT 1
- Set NgxPermissionsGuard functions to protected access levels HOT 2
- How can I set the permissions base on URL parameters? HOT 5
- Unable to make NgxPermissionsService singleton across application and dependent libraries HOT 2
- Angular 16 support with providers HOT 4
- Check if all permissions from array are present
- How to use Rolle and Permission without Rolle HOT 3
- Identify the directive in the loadPermissions function HOT 1
- NgxPermissionsGuard deprecated HOT 2
- Angular 17 Support HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-permissions.