【问题标题】:Laravel action is not authorizedLaravel 动作未授权
【发布时间】:2017-11-16 00:18:06
【问题描述】:

我正在尝试删除属于发布它的用户的帖子,但是我收到此错误(顺便说一下,这是在网络日志中)

“/Applications/MAMP/htdocs/eli42/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php” 行:201 消息:“此操作未经授权。”跟踪:[{,…},…]

我正在使用 laravel 5.5 policy 不确定我这样做是否正确,我在 $protected 政策中的 AuthServiceProvider 中注册了它

Post::class => PostPolicy::class,

路线

Route::delete('auth/post/{id}', 'PostController@destroy');

PostPolicy.php

<?php

namespace App\Policies;

use App\User;
use App\Post;

use Illuminate\Auth\Access\HandlesAuthorization;

class PostPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view the post.
     *
     * @param  \App\User  $user
     * @param  \App\Post  $post
     * @return mixed
     */
    public function view(User $user, Post $post)
    {
        //
    }

    /**
     * Determine whether the user can create posts.
     *
     * @param  \App\User  $user
     * @return mixed
     */
    public function create(User $user)
    {
        //
    }

    /**
     * Determine whether the user can update the post.
     *
     * @param  \App\User  $user
     * @param  \App\Post  $post
     * @return mixed
     */
    public function update(User $user, Post $post)
    {
        //
    }

    /**
     * Determine whether the user can delete the post.
     *
     * @param  \App\User  $user
     * @param  \App\Post  $post
     * @return mixed
     */
    public function delete(User $user, Post $post)
    {
        //

        return $user->id === $post->user_id;

    }

PostController.php(此文件有更多代码,但我想突出显示删除功能)

<?php

namespace App\Http\Controllers;

use App\Post;
use App\User;
use App\Policies\TaskPolicy; 


use Illuminate\Http\Request;
use Illuminate\Http\Response;

class PostController extends Controller
{

    public function destroy($id, Post $post)
    {
        $mypost = $this->authorize('delete',$post);

        if($mypost){
             Post::destroy($id);

        }




    }
}

Main.js 删除帖子

$scope.deletePost = function(post){
    var index = $scope.myposts.indexOf(post);

    if(index != -1){
        $scope.myposts.splice(index, 1);
    }

    $http.delete('auth/post/' + post.id);

};

html

   <button ng-click="deletePost(post)">x</button>

之前

之后

【问题讨论】:

  • 向我们展示您的路线可能有助于我们确定您的问题。
  • @Mark 我更新了帖子

标签: php laravel


【解决方案1】:

您不需要检索帖子,让 Laravel 为您完成。

将您的路线编辑成这样:

Route::delete('auth/post/{post}', 'PostController@destroy');

请注意,如果 Laravel 找到,大括号之间的 post 将是分配给帖子的变量名。如果没有找到帖子,Laravel 将返回 Not Found 404。

然后在你的控制器中,你必须告诉 Laravel 你期待一个通过路由的帖子:

方法签名将如下所示:destroy(Post $post)$post 是您路线中的 {post}

最后,为了授权,你不会得到authorize method返回的帖子。你将 Laravel 找到的 $post 传递给 authorize 方法。

这里是完整的方法:

public function destroy(Post $post)
{
    $this->authorize('delete', $post);

    if ($post->delete()) {
        return response()->json(['message' => 'deleted']);
    };

    return response()->json(['error' => 'something went wrong'], 400);
}

【讨论】:

  • @hamound 我还有一个问题,
  • 对于不属于使用刀片的用户的帖子,是否有隐藏删除&lt;button ng-click="deletePost(post)"&gt;x&lt;/button&gt; 按钮?
  • 未经测试。但试试@if( auth()-&gt;user()-&gt;can('delete', $post))
  • 虽然,我不知道你是如何使用带角度的刀片的。我不是角度专家。
  • @hamound 只需切换符号和角度,刀片工作正常,但我遇到了找不到帖子的问题,谷歌搜索它。
【解决方案2】:

那怎么样……这行得通吗?

public function destroy($id)
    {
        $post = Post::first($id);
        $user = auth()->user();
        print_r($post);
        print_r($user);
        $mypost = $user->can('delete', $post);

        if($mypost){
             Post::destroy($id);
        }

    }

【讨论】:

  • 越来越近了,我现在得到Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given,不知道这到底意味着什么
  • 我正在使用 Angular 删除帖子,这是一个因素吗?
  • 只是为了好玩,尝试一下...确保 $post 包含有效的帖子模型实例并且 $user 返回登录用户...想知道这是否不是您的问题之一.. .
  • 请记住我使用这条路线删除帖子Route::delete('auth/post/{id}', 'PostController@destroy');
  • 你从 Angular 传递了什么 ID?同样的错误在哪里?在 Post::first($id) ?它必须解析为一个好的 Post 实例。
猜你喜欢
  • 1970-01-01
  • 2019-05-04
  • 2020-04-20
  • 2021-07-22
  • 1970-01-01
  • 2020-11-15
  • 2017-06-12
  • 1970-01-01
  • 2019-03-07
相关资源
最近更新 更多