简体   繁体   English

Laravel授权()混淆

[英]Laravel Authorize() confusion

I'm currently migrating a project from CodeIgniter to Laravel5. 我目前正在将一个项目从CodeIgniter迁移到Laravel5。

I saw in Laracasts that you can use the Request::authorize() method to authorize access before the controller is called, and it returns true or false. 我在Laracasts中看到,您可以在调用控制器之前使用Request::authorize()方法来授权访问,并返回true或false。

This would (I think) be the ideal solution as I can contain permission checks within the request, rather than pollute the controller with permission checks and redirections / responses. 这将(我认为)是理想的解决方案,因为我可以在请求中包含权限检查,而不是通过权限检查和重定向/响应污染控制器。

The only problem is, when I return false from authorize() , it simply loads an empty white page with forbidden written, and I can't find any documentation on laravel.com on how to template it (either there is no documentation, or I'm overlooking it) 唯一的问题是,当我从authorize()返回false时,它只是加载一个带有forbidden写入的空白页,我在laravel.com上找不到任何关于如何模板化的文档(要么没有文档,要么我忽略了它)

I know I can edit the 404 page in errors/404.blade.php , but I can't work out how to customize the 403 page, which I've tried to add a custom 403.blade.php page, which doesn't get displayed. 我知道我可以编辑errors/404.blade.php的404页面,但我无法弄清楚如何自定义403页面,我试图添加一个自定义的403.blade.php页面,其中没有得到显示。 ( https://mattstauffer.co/blog/laravel-5.0-custom-error-pages ) https://mattstauffer.co/blog/laravel-5.0-custom-error-pages

Is placing these permission checks in the Request a good idea? 将这些权限检查放在请求中是个好主意吗? Or am I missing something? 或者我错过了什么?

Update I ran a backtrace from authorize() , and it looks like it throws an UnauthorizedException , which extends RuntimeException . 更新我从authorize()运行了一个回溯,它看起来像是抛出一个UnauthorizedException ,它扩展了RuntimeException I've tried catching both in the routes.php file, which doesn't work either. 我试过在routes.php文件中捕获它们,这两个都不起作用。

I've also tried to create middleware, and call the middleware from a method, which doesn't work either, since the middleware's not even called at all. 我也试图创建中间件,并从一个方法调用中间件,这也不起作用,因为中间件甚至根本没有被调用。

Update 2 Ok, so I found out that I can only call $this->middleware() from the constructor, not individual methods, which is progress, I guess. 更新2好的,所以我发现我只能从构造函数中调用$this->middleware() ,而不是单个方法,这是进步,我猜。

What i do is add a forbiddenResponse() method to Request abstract class. 我所做的是为Request抽象类添加一个forbiddenResponse()方法。 You can return a response object from that method to render a human readable error. 您可以从该方法返回响应对象以呈现人类可读错误。

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;

abstract class Request extends FormRequest {

    public function forbiddenResponse()
    {
        return new JsonResponse('Unauthorized', 403);
        // or return Response::make('Unauthorized', 403);
    }
}

Check the app\\Exceptions\\Handler.php file. 检查app\\Exceptions\\Handler.php文件。 That's where you can customize your exception handling. 您可以在这里自定义异常处理。

The 403 error launches a HttpException . 403错误启动HttpException By default, Laravel will look under your resources\\views\\errors\\ directory and try to find a view that corresponds to the same status code. 默认情况下,Laravel将查看您的resources\\views\\errors\\目录下的内容,并尝试查找与相同状态代码对应的视图。 Since you already said that you've created a file called 403.blade.php inside that folder, it should render this page for 403 errors. 由于您已经说过在该文件夹中创建了一个名为403.blade.php文件,因此它应该呈现此页面403错误。

One last thing, remember to check inside your web server config file ( httpd.conf for Apache, sites-available\\your-host for Nginx), if you have a default behavior for any error. 最后一点,如果您有任何错误的默认行为,请记得检查您的Web服务器配置文件(Apache的httpd.conf ,Nginx的sites-available\\your-host )。 If you're using Homestead, you can check the Nginx config file for anything like error_page 404 /index.php; 如果你正在使用Homestead,你可以查看Nginx配置文件,查看error_page 404 /index.php; , comment the line and restart the service. ,注释该行并重新启动该服务。 That's not the ideal scenario but usually works. 这不是理想的情况,但通常有效。

Override the method within your form request object 覆盖表单请求对象中的方法

class CreateUserRequest extends FormRequest {

    public function forbiddenResponse(){
        return abort(403);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM