gpt4 book ai didi

php - 拉维尔 5 : allow user to edit post if he's the owner of the post or the owner of the forum category

转载 作者:IT王子 更新时间:2023-10-29 00:28:13 24 4
gpt4 key购买 nike

到目前为止,我能够允许用户编辑他自己的帖子,但每当我通过 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-postmod-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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com