laravel-5.4 - Laravel 5.4 受用户许可保护文档

标签 laravel-5.4 protected documents

我有一个 Laravel 项目,其中用户具有具有权限的角色(我正在使用 Zizaco/entrust ) 并且该应用仅供注册用户访问。

该应用程序包含上传的文件,但该文件不应公开查看,另一方面,该文件应在用户许可的情况下可访问。

我的问题:在这种情况下如何进行,如何在用户权限功能中保护文档?

最佳答案

我不确定这是否有帮助,但您可以创建一个特殊的 Controller 来下载/显示文档,您可以在其中检查实际用户的权限。

Entrust 文档中,您可以检查用户是否应该能够看到该文档:

$user->hasRole('owner'); //returns boolean

因此您可以在 Controller 中使用下面的代码:

$user = User::where('username', '=', 'Mark')->first();    
$pathToFile = Storage::get('file.pdf');
if ($user->hasRole('admin'))
{
    return response()->download($pathToFile); //if you want to display a file, then change download to file
}
else
{
    abort(403, 'Unauthorized action.');
}

请记住将此行添加到您的 Controller :

use Zizaco\Entrust\Traits\EntrustUserTrait;

您可以在此处阅读有关回复的更多信息:https://laravel.com/docs/5.4/responses和文件在这里:https://laravel.com/docs/5.4/filesystem


在此处查看简短语法,这将帮助您在 routes.php 中实现文件下载,而无需创建新的 Controller 。 https://github.com/Zizaco/entrust#short-syntax-route-filter

关于laravel-5.4 - Laravel 5.4 受用户许可保护文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44968568/

相关文章:

excel - 在 protected 工作表上启用宏按钮 - Excel

c++ - 绕过错误 C2248 "cannot access protected member declared in class"的有效方法

c++ - C++17 中 protected 构造函数的规则改变了吗?

java - 扫描图像到可读文本

Ruby YAML 多文档

php - 将新列添加到数据库表中,迁移失败并显示 - 无需迁移

node.js - Laravel 和 bootstrap glyphicons - 文件复制到哪里,如何设置相对路径以及如何解决 cachebusting?

php - 调用未定义的方法 Illuminate\Database\Query\Builder::withAccessToken()

php - Laravel:动态查询按 api 排序

elasticsearch - 在 Elasticsearch 中删除类型的文档