加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel 5:如果用户是帖子的所有者或论坛类别的所有者,

发布时间:2020-12-14 19:44:54 所属栏目:大数据 来源:网络整理
导读:到目前为止,我能够允许用户编辑他自己的帖子,但每当我通过他是subreddit / category条件的所有者时,它就会完全停止工作. 我有这3张桌子 Users: id,name,email...Subreddits: id,user_id...Posts: id,link,title,user_id,subreddit_id... 这是PostsController
到目前为止,我能够允许用户编辑他自己的帖子,但每当我通过他是subreddit / category条件的所有者时,它就会完全停止工作.

我有这3张桌子

Users: id,name,email...
Subreddits: id,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);
        }
    }

这就是观点

@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)它就会停止工作.它在所有帖子的视图上显示“编辑”链接,但点击其中任何一个都会给我验证错误即使对于我拥有的帖子也不能这样做.

那么如何检查用户是文章的所有者还是要显示的类别的所有者并允许编辑?

使用$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',$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上的帖子上这样做.

这是我尝试过的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',$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',$subreddit);
    }
    }

视图

@can('update-post',$post)
     <a href="{{ action('PostsController@edit',[$post->id]) }}">Edit</a>
@endcan

使用上面的代码,如果’update-post’为true,我终于能够编辑帖子但是我无法检查mod-update-post是否也是真的,我一直收到验证错误你不能这样做

dd($subreddit)里面的edit()方法显示一个空数组. https://cryptbin.com/x6V6wX#26810f755a62f6c8837c0c0fe0371dcf

编辑:我想我已经解决了.我使用$post-> subreddit-> user-> id而不是$subreddit-> user_id,因为它返回null.现在这一切似乎都基于帖子属于用户或用户是论坛所有者.

但是视图上的编辑链接仍然显示我是否有权访问.我无法同时检查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

希望这可以解决问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读