Laravel行动未经授权

时间:2017-11-16 00:18:06

标签: php laravel

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

  

“/应用程序/ MAMP / htdocs中/ eli42 /供应商/ laravel /框架/ SRC /照亮/粉底/异常/ Handler.php”   line:201 message:“此操作未经授权。”追踪:[{,...},...]

我正在使用laravel 5.5 policy不确定我是否正确行事,我在 $ protected policies

中的我的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>

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:4)

你不需要检索帖子,让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);
}

答案 1 :(得分:3)

那怎么样......那有用吗?

short bit=
    ch&0x2?1:
    (ch&0x4?2:
    (ch&0x8?3:
    (ch&0x10?4:
    (ch&0x20?5:
    (ch&0x40?6:7)))));