Giter Site home page Giter Site logo

Comments (6)

AlexKhymenko avatar AlexKhymenko commented on September 26, 2024 3

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

AlexKhymenko avatar AlexKhymenko commented on September 26, 2024 2

@timhoekstra This is great idea. Will look into it

from ngx-permissions.

khodabandelu avatar khodabandelu commented on September 26, 2024 1

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.

timhoekstra avatar timhoekstra commented on September 26, 2024 1

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.

timhoekstra avatar timhoekstra commented on September 26, 2024

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.

AlexKhymenko avatar AlexKhymenko commented on September 26, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.