我有一个关于Laravel政策的快速问题。我正在编写一个非常简单的API。我有一个带有一些GET端点的Podcast对象:
GET /api/podcasts => returns all podcasts
GET /api/podcasts/{podcast} => returns a given podcast我正在使用绑定到播客模型的策略。我在PodcastController类的构造函数中使用了$this->authorizeResource(Podcast::class);。这很好用,我只能访问我自己的播客!
现在,我有一些子对象,例如文件。所以我创建了一个新的端点:
GET /api/podcasts/{podcast}/files/{file} => returns a specific file of a podcast我已经在FileController类的构造函数中添加了$this->authorizeResource(Podcast::class);。这样做,我不能在URL中输入任何播客ID,只能输入我自己的ID,这很好。但是,我可以在URL中输入任何文件ID,包括不属于我的播客的文件。例如:
GET /api/podcasts/1/files/3播客#1是我的,这很好,但是文件#3属于播客#2 (不是#1),它不是我的。在这一点上我应该得到一个未经授权的访问。
有什么想法吗?谢谢
Axel
发布于 2019-05-13 22:43:34
我实际上找到了解决方案的开始,但我发现它很脏。我写了一个显式的模型绑定条件:
Route::bind('file', function($value, $router) {
return File::where('podcast_id', $router->parameter('podcast'))->findOrFail($value);
});你们觉得怎么样?谢谢
https://stackoverflow.com/questions/56113240
复制相似问题