- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
到目前为止,我能够允许用户编辑他自己的帖子,但每当我通过 if he's owner of the subreddit/category
条件时,它就会完全停止工作。
我有这3张 table
Users: id, name, email...
Subreddits: id, name, user_id...
Posts: id, link, title, user_id, subreddit_id...
这是 PostsController.php
中的 edit()
方法
public function edit(Post $post, Subreddit $subreddit)
{
if(Auth::id() !== $post->user_id) {
return view('home')->withErrors('You cannot do that');
} else {
return view('post/edit')->with('post', $post)->with('subreddit', $subreddit);
}
}
这是 View
@if(Auth::id() == $post->user_id)
<a href="{{ action('PostsController@edit', [$post->id]) }}">Edit</a>
@endif
这工作正常,它只检查登录的 user_id 是否与帖子的 user_id 相同并且更新通过。
但是如果我添加 if(Auth::id() == $subreddit->user_id)
它就会停止工作。它在所有帖子的 View 上显示“编辑”链接,但单击其中任何一个都会给我验证错误 You cannot do that
即使对于我拥有的帖子也是如此。
那么如何检查用户是文章的所有者还是要显示的类别的所有者并允许编辑?
使用 $subreddit->user_id
更新方法
public function edit(Post $post, Subreddit $subreddit)
{
if(Auth::id() == $post->user_id || Auth::id() == $subreddit->user_id) {
return view('post/edit')->with('post', $post)->with('subreddit', $subreddit);
} else {
return view('home')->withErrors('You cannot do that');
}
}
查看
@if(Auth::id() == $post->user_id || Auth::id() == $subreddit->user_id)
<a href="{{ action('PostsController@edit', [$post->id]) }}">Edit</a>
@endif
这将允许我编辑我自己的帖子,但仍然会在我自己的论坛类别/subreddit 中的帖子上给我验证错误 You cannot do that
。
这是我试过的 Gate
策略,但也没有用
class AuthServiceProvider extends ServiceProvider
{
// Authorizations and Permissions
public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
$gate->define('update-post', function ($user, $post) {
return $user->id === $post->user_id;
});
$gate->define('mod-update-post', function ($user, $subreddit) {
return $user->id === $subreddit->user_id;
});
}
PostsController.php
public function edit(Post $post, Subreddit $subreddit, User $user)
{
if(Gate::denies('update-post', $post) && Gate::denies('mod-update-post', $subreddit)) {
return view('home')->withErrors('You cannot do that');
} else {
return view('post/edit')->with('post', $post)->with('subreddit', $subreddit);
}
}
查看
@can('update-post', $post)
<a href="{{ action('PostsController@edit', [$post->id]) }}">Edit</a>
@endcan
使用上面的代码,如果 'update-post' 为真,我终于可以编辑帖子了,但我无法检查 mod-update-post
是否也为真,我一直收到验证错误你不能这样做
dd($subreddit)
inside edit() 方法显示一个空数组。 https://cryptbin.com/x6V6wX#26810f755a62f6c8837c0c0fe0371dcf
编辑:我想我已经解决了。我使用 $post->subreddit->user->id
而不是 $subreddit->user_id
因为它返回 null。现在,这一切似乎都取决于帖子属于用户还是用户是论坛所有者。
但是 View 上的编辑链接仍然显示我是否有访问权限。我无法同时仔细检查 update-post
和 mod-update-post
。并且使用 @can('update-post', $post)
只能使用一次,我无法仔细检查。
最佳答案
So how can I check if the users is the owner of the article OR the owner of the category to display and allow EDIT?
使用 Laravel 的新 authorization component .
编辑:我认为您误解了授权的使用方式。它应该用于检查当前用户 是否可以执行操作(或不能)。因此,您不希望为不同的用户类型定义多种方法。
以编辑帖子为例。这是您的授权检查的名称:@can('edit', $post)
。您不需要为普通用户定义一个不同的,而另一个为版主定义。只需将逻辑添加到您的编辑帖子方法中:
class PostPolicy
{
public function edit(User $user, Post $post)
{
// If user is administrator, then can edit any post
if ($user->isModerator()) {
return true;
}
// Check if user is the post author
if ($user->id === $post->author_id) {
return true;
}
return false;
}
}
如您所见,我正在用相同的方法进行不同的检查,因此在您的 Blade 模板中您可以这样做:
@can('edit', $post)
<a href="{{ route('post.edit', $post->getRouteKey()) }}">Edit post</a>
@endcan
希望这能解决问题。
关于php - 拉维尔 5 : allow user to edit post if he's the owner of the post or the owner of the forum category,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32951025/
我试图让我的 jQuery 脚本从单击的链接中提取 url,然后将其插入到我的视频标签中。有什么建议吗? 我试过拼接我从 .html() 中得到的内容,但引号总是搞砸了。
我遇到了 docker 的问题。 场景是这样的:我们使用Codebuild+Packer+docker创建AMI,用于deploy。在这一步中,我们从 Artifactory 中提取图像,并且除了提取
我目前正在学习 RxJS。 在文档中,我找到了这个数组。 我尝试在谷歌上搜索“pull and push javascript”,但我什至不知道如何调用这些实体/概念。我不明白那是什么意思?我假设 S
Title 在小屏幕上,我首先需要标题,然后是文本字段,但在中等以上的屏幕上,我需要相反的方式 - 我已经尝试过推和拉,但它们无法工作 - 有什么想法吗? 最佳答案 根据 Swa
zmq 的某些部分未以可预测的方式运行。 我正在使用 VS2013 和 zmq 3.2.4。为了不在我的 pubsub 框架中“丢失”消息 [旁白:我认为这是一个设计缺陷。我应该能够首先启动我的订阅者
我正在编写一个使用嵌套 Bootstrap 列的页面。我正在使用推/拉让列在移动设备上切换位置,而且效果很好。但是,在桌面上我遇到了一些奇怪的间距问题。嵌套列偏移到父列的右侧。 我设置了一个 fidd
在拉取一些 docker 镜像(但不是全部)时出现此错误: failed to register layer: Error processing tar file(exit status 1): op
我创建了一个 Kubernetes 集群,并为每个节点安装了 docker。 当我尝试使用 docker push local_registry_addr:port/image_id 将图像拉取或推送
没有明确地推/拉单个书签,书签何时从 repo 复制/更新到 repo? 在我对两个本地存储库的测试中,我无法推断出一致的行为。有时从 A 到 B 或 B 到 A 的推/拉会复制/更新书签,有时不会。
在 Bootstrap 3 文档中,他们给出了以下使用 push 和 pull 类更改列顺序 (http://getbootstrap.com/css/#grid-column-ordering) 的
从这个问题开始Three column Bootstrap layout with left sidebar at bottom我了解了 Bootstrap 列推拉。 下面的代码片段几乎可以得到我想要
许多 Repo 函数的签名包括 **kwargs,其中文档说,您可以将参数传递给底层包装的 git 命令。但是,*args 没有位置。为了传递类似标志的参数,如 --all。我原以为它们会像 my_r
如果您将大文件推送/拉到设备上,这真的很烦人,现在无法知道它有多远。是否可以运行 adb push 或 adb pull 并使用“bar”实用程序获取进度条? 这里的主要问题是我认为 adb 需要两个
当我尝试使用 Gitkrakent 向/从 Heroku 推/拉时,GitKraken 告诉我: "Please log in to continue" 请求的“用户/登录”是什么? (我个人 Her
我在 docker 容器中有一个 Jenkins 2.150.1。要安装这个 Jenkins,我只需使用 jenkinsci/blueocean:1.9.0图片。 我创建了一个管道,然后尝试使用我的
我想使用 Jenkins 做下一步: 1- docker pull 2- docker run -i -t 我已经在jenkins上安装了docker插件,但是这可行吗? docker plugi
如果我正在处理一些我不想提交的文件,我只需保存它们。然后我有其他文件想要推送到服务器,但是如果其他人对存储库进行了更改,并且我将它们拉下来,它会要求我 merge 或 rebase ..但是这些选项中
无论出于何种原因,我在 FB 上共享链接时尝试使用的图像都无法加载。给出的确切错误是: 提供了og:image,无法下载。发生这种情况的原因有多种,例如您的服务器使用不受支持的内容编码。爬虫接受 de
今天我买了三星 Galaxy Note 3,它配备了 Android 4.3。由于它太新了,我找不到根植我设备的方法,所以我尝试使用 adb 连接……我失败了。 所以,我用了这个 D:\android
我尝试通过 airflow cli test 命令测试 2 个任务` 第一个任务运行,自动将最后一个控制台推送到 xcom,我按预期在 Airflow GUI 中看到了值 some value 当我通
我是一名优秀的程序员,十分优秀!