How to catch Exception 403
Closed this issue · 17 comments
Can you please tell me how to catch or handle your 403 exeption pls.
Thank you
Can you post your code?
Which code? I just want to catch this:
to redirecto to a notification and no to the 403 page, I did that with Spatie Permissions, but I can't find your exception.
Example with Spatie permissions:
if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
return redirect()->route('dashboard')
->with('message_error', ''.Lang::get('backend.nopermissions').'');
}
Best Regards
Now i understand. You can add this to you Handler:
use Junges\ACL\Exceptions\Unauthorized;
and in the render function:
if ($exception instanceof Unauthorized){
//your code here
}
I'll add it to the README file. Thank you again!
Post the code in your handler function please, because for me it is working very well
I am in Exceptions\Handler.php and here is my function:
public function render($request, Exception $exception)
{
if ($exception instanceof Unauthorized) {
return redirect()->route('dashboard')
->with('message_error', ''.Lang::get('backend.nopermissions').'');
}
if ($exception instanceof \Illuminate\Session\TokenMismatchException) {
return redirect()->route('dashboard')
->with('message_error', ''.Lang::get('backend.sessionexpired').'');
}
if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
return redirect()->route('dashboard')
->with('message_error', ''.Lang::get('backend.modelnotfoundordeactivaded').'');
}
return parent::render($request, $exception);
}
The others ones works perfect, your exception nope.
I'm using the same, and it is working:
if ($exception instanceof Unauthorized)
return redirect()
->route('missing-permissions')
->with('message', 'Permission Denied');
I'll debug and give you an answer asap
Working now, the problem was I am trying to redirect dashboard and protecting dashboard too hahahaha.
Sorry.
hahahah
It's good that it's working!
Any problems please report.
Thanks!
Can we detect on basis of which permission missing, the exception is thrown ? I could really use that to reduce a lot of unwanted manual code in my app.
Like, if the exception is thrown because of mission permission: A or B or C.
Hi @pravnkay, just to be clear, you mean to show the missing permission in the exception message?
For example:
This user does not have the necessary permissions [permission-slug] to access this route.
I need to catch the list of denied permissions in handler.php when defining custom handlers.
The current exception is :
Junges\ACL\Exceptions\UnauthorizedException ~statusCode: 403~headers: Array(0)*message: "This user does not have the necessary permissions to access this route"...
~statusCode: 403
~headers: Array(0)
*message: "This user does not have the necessary permissions to access this route"
~string: ""
*code: 0
*file: "<application_path>\vendor\mateusjunges\laravel-acl\src\Exceptions\UnauthorizedException.php"
*line: 33
~trace: Array(43)
~previous: null
but, how about this : (look after the error message)
Junges\ACL\Exceptions\UnauthorizedException ~statusCode: 403~headers: Array(0)*message: "This user does not have the necessary permissions to access this route"...
~statusCode: 403
~headers: Array(0)
*message: "This user does not have the necessary permissions to access this route"
*denied:Array[
0: 'view-profile'
1: 'view-files'
]**
~string: ""
*code: 0
*file: "D:\Pro\laragon\www\matri\vendor\mateusjunges\laravel-acl\src\Exceptions\UnauthorizedException.php"
*line: 33
~trace: Array(43)
~previous: null
Hope I am clear about that.
@pravnkay can you please open an issue with a feature request? I think we can discuss it better there.
@pravnkay can you please open an issue with a feature request? I think we can discuss it better there.
Opened #175 (comment)