0

我有一个关于 Laravel 政策的快速问题。我正在编写一个非常简单的 API。我有一个带有一些 GET 端点的 Podcast 对象:

GET /api/podcasts => returns all podcasts
GET /api/podcasts/{podcast} => returns a given podcast

我正在使用绑定到 Podcast 模型的策略。我在我的类$this->authorizeResource(Podcast::class);的构造函数中使用了 a 。PodcastController这很好用,我只能访问我自己的播客!

现在,我有子对象,例如文件。所以我创建了一个新端点:

GET /api/podcasts/{podcast}/files/{file} => returns a specific file of a podcast

我已将其添加$this->authorizeResource(Podcast::class);到类的构造函数中FileController。这样做,我不能在 URL 中输入任何播客 ID,只能输入我自己的,这很好。但是,我可以在 URL 中输入任何文件 ID,包括不属于我的播客所有的文件。例如 :

GET /api/podcasts/1/files/3

播客#1 是我的,这很好但是文件#3 属于播客#2(不是#1),这不是我的。此时我应该获得未经授权的访问。

任何想法 ?谢谢

阿克塞尔

4

1 回答 1

0

我实际上找到了解决方案的开始,但我发现它很脏。我写了一个明确的模型绑定条件:

        Route::bind('file', function($value, $router) {
            return File::where('podcast_id', $router->parameter('podcast'))->findOrFail($value);
        });

你们怎么看?谢谢

于 2019-05-13T14:43:34.173 回答