- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我不确定以前是如何处理这个问题的,但是我该如何使用 VueJs 并在 Vue 模板中授权操作?
如果我使用的是 Laravel 的 Blade ,这很容易(使用 @can
指令),但是在 Google 上搜索了几个小时后,没有任何文档或任何方法可以在 Vue 中执行此操作。
现在,我知道我可以简单地将用户权限加载到 View 内的数组/JSON 对象中,但似乎无法使用 Laravel 的门方法在 Vue 模板中显示/隐藏操作以确定是否允许用户对特定记录执行操作。
例如,有一个评论列表,但用户必须拥有评论才能看到“编辑”按钮。
问题是,如果我在 Vue 中实现逻辑,我将为此在整个后端和前端复制授权逻辑。
使用 Laravel 的策略,我能够执行特定操作的复杂授权。但我对如何在 Vue 中实现该政策感到困惑。
还有更复杂的场景,例如如果具有 admin
角色的用户正在浏览评论,即使他们不拥有评论,他们也应该能够编辑评论。
有人对这种情况有什么建议吗?
编辑:
现在我可以向我的模型添加一个属性访问器,例如:
型号:
class Comment extends Model
{
protected $appends = ['can_update'];
public function getCanUpdateAttribute()
{
return Gate::allows('update', $this);
}
}
View :
<button v-if="comment.can_update">Edit</button>
但这似乎又是在重复我的策略中已经存在的逻辑。
最佳答案
我最终使用了 Laravel resources来完成这个。
这是一个示例(注意 can
数组键):
class Ticket extends Resource
{
/**
* The "data" wrapper that should be applied.
*
* @var string
*/
public static $wrap = 'ticket';
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'answer_id' => $this->answer_id,
'summary' => $this->summary,
'description' => $this->description,
'creator' => $this->creator,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'reported_at' => $this->reported_at,
'closed_at' => $this->closed_at,
'closed' => $this->closed,
'answered' => $this->answered,
'can' => $this->permissions(),
];
}
/**
* Returns the permissions of the resource.
*
* @return array
*/
protected function permissions()
{
return [
'update' => Gate::allows('update', $this->resource),
'delete' => Gate::allows('delete', $this->resource),
'open' => Gate::allows('open', $this->resource),
];
}
}
这让我可以在 Vue 模板中使用简单的 bool 逻辑来控制前端的访问,而不是在前端复制实际的权限逻辑:
<router-link v-if="ticket.can.update" :to="{name:'tickets.edit', params: {ticketId: ticket.id}}" class="btn btn-sm btn-secondary">
<i class="fa fa-edit"></i> Edit
</router-link>
此外,如果用户能够创建资源,我使用 Laravel 资源集合 来应用权限:
class TicketCollection extends ResourceCollection
{
/**
* The "data" wrapper that should be applied.
*
* @var string
*/
public static $wrap = 'tickets';
/**
* Get any additional data that should be returned with the resource array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function with($request)
{
return [
'can' => [
'create' => Gate::allows('create', Ticket::class),
],
];
}
}
然后在我的 API Controller 中:
public function index()
{
$tickets = Ticket::paginate(25);
return new TicketCollection($tickets);
}
public function show(Ticket $ticket)
{
$ticket->load('media');
return new TicketResource($ticket);
}
这允许我验证当前经过身份验证的用户是否有权创建正在列出的资源,因为我们没有要验证的实际资源,我们可以在返回的集合上执行此操作,因为它完全与之相关。
在我看来,实现此模式是管理授权的最简单方法,无需在我的 Vue 应用程序中复制实际的授权逻辑,也无需使用 blade 将权限单独注入(inject)组件。
如果您的嵌套组件也需要权限,那么向组件注入(inject)权限最终会导致我遇到问题,因为这样您就需要将子组件权限传递给父组件才能验证它们。
对于嵌套权限,您可以从父资源返回子资源以获得还包含 can 权限数组的关系,因此您可以使用 Vue 轻松地遍历这些资源并使用简单的逻辑来确定用户也可以访问这些内容。
这种方法也很有用,因此我可以通过服务器端在不经常更改的资源上缓存每个用户的权限。
关于laravel - 在 VueJs 中使用 Laravel 的 Gate/Authorization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45868924/
我目前有一个使用 Authorize.net 的 ARB API 的应用程序。我的一切都按照我认为应该的方式工作,但我真的很想收到来自 authorize.net 测试服务器 (test.author
Authorize.net 永远不会允许您退还同一天提交的交易。因此,编写一个创建交易然后退款的测试是很困难的。 我找不到通过 API 执行此操作的方法,因此: 我可以要求 Authorize.net
我想使用 createTransactionRequest 创建付款资料。 这是我传递的请求参数。 { "createTransactionRequest":{ "merchantA
我根据这个例子开发了我的mvc应用程序: https://github.com/AzureADSamples/WebApp-WebAPI-OpenIDConnect-DotNet 身份验证与 Azur
所以我正在尝试实现以下场景: 应用程序受基本身份验证保护。假设它托管在 app.com 上 在应用程序前面的 HTTP 代理也需要身份验证。它托管在 proxy.com 因此,用户必须在同一个请求中为
根据 ASP.NET website ASP.NET MVC 框架包括几个 Action 过滤器: OutputCache – 此操作过滤器将 Controller 操作的输出缓存一段指定的时间。 H
我正在发布一些 JSON 数据并添加一个 Authorization标题。但是,请求对象没有正确的授权属性。 HTTP_AUTHORIZATION和 headers两者都显示正确的授权详细信息。 {'
更改群集的IP配置(所有外部IP更改,内部专用IP保持不变)后,某些kubectl命令不再对任何容器起作用。 pods 全部启动并运行,似乎发现自己没有问题。这是输出: bronger@penny:~
Authorize.Net will be upgrading access to the Internet connections that serve our data centers. Inst
您好,我正在学习 HTML5,我想知道这两个元元素之间有什么区别? 最佳答案 第一个无效,http-equiv被枚举,即它有一组对其有效的值和author不是一个。 (它旨在作为一种将 pragm
我们想在应用商店中发布一个应用。为我们构建它的第 3 方需要我们通过苹果开发门户创建的证书和配置文件。根据文档,创建证书的方法是使用 mac 的钥匙串(keychain)应用程序,然后选择“从证书颁发
在同一项目中,在 Nancy 中使用任一 token 授权形式保护模块的最佳方法是什么?我需要两种类型的安全性,并且我不希望创建单独的应用程序:一种使用 Form 保护 View ,另一种用于使用 T
我正在尝试获取访问 token ,但是我收到了这个错误 {"error_description":"授权服务器不支持授权授予类型","error":"unsupported_grant_type"}
如何从一组“author”(User)中过滤出具有“author”的对象? “对象”是帖子,具有作者(ForeignKey 到用户)。 我对此非常困惑,所以我很感激能得到帮助。当然,人们可以通过手动过
我有三个 Kafka 经纪人( kafka_2.11-0.10.0.0 )每个经纪人的安全配置如下, listeners=PLAINTEXT://xxxx:9093,SASL_PLAINTEXT://
@IBOutlet weak var menuButton: UIButton! @IBOutlet weak var clubButton: UIButton! @IBOutlet weak var
我不知道如何执行以下查询。我有 3 张 table : song (song_id, title, is_draft) author (author_id, name) song_author (so
我正在使用 Thymeleaf、Spring-boot 和 Java。我有导航栏的 li 项目,并且想要为其中一个 li 项目设置访问权限,以便如果 LoggedUser.client.policyT
刚从使用 Books 应用程序示例的 Djangobook 教程中学习时,您通过多对多关系将 Book 与 Author 相关,并将 Book 与 Publisher 相关。您可以使用 p.book_
当我的应用程序启动 map View 时,我请求 iOS8“使用时”位置权限。假设用户同意。 仅当用户选择加入我的地理围栏功能时,我才想请求始终许可。但是调用 CLLocationManager.re
我是一名优秀的程序员,十分优秀!