Laravel 中间件限制对不需要的功能的访问

Laravel middleware limits access to unwanted functions

我正在 Laravel 做一个项目。 我有一个包含 post 和用户的数据库。这些 post 可以由创建它的用户和管理员修改和编辑。 为此,我为用户创建了一个新字段,其中有一个管理员和两个编辑器。 使用中间件限制访问后,只有admin和editor可以访问posts.

    $this->middleware('auth',['only' => ['create', 'store', 'edit', 'update', 'destroy']]);
    $this->middleware(['auth', 'roles:admin'],['only' => ['edit', 'update', 'destroy']]);

问题是现在只有管理员可以访问编辑和删除 post 功能。发布者被重定向到主页。

有没有办法放置一个 if 来绕过中间件重定向或类似的东西?

我会使用策略来简化事情,并删除中间件。

Create Policy

php artisan make:policy PostPolicy --model=Post

生成此文件

<?php

namespace App\Policies;

use App\Post;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class PostPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any models.
     *
     * @param  \App\User  $user
     * @return mixed
     */
    public function viewAny(User $user)
    {
        //
    }

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

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

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

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

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

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

修改每个动作的规则,例如我们需要指定只有管理员或post所有者可以更新一个post,所以

public function update(User $user, Post $post)
{
    if ($user->role === 'admin') {
        return true;
    }

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

然后注册保单 https://laravel.com/docs/8.x/authorization#registering-policies

<?php

namespace App\Providers;

use App\Models\Post;
use App\Policies\PostPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        Post::class => PostPolicy::class,
    ];

    /**
     * Register any application authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //
    }
}

最后授权你的控制器,将这一行添加到构造函数

public function __construct()
{
    $this->authorizeResource(Post::class, 'post');
}

请注意,函数调用中的第二个参数是路由参数的名称,如果您创建了一个足智多谋的控制器,post 将成为您的路由参数

如果你没有使用resourceful controller,或者想手动授权动作,那么你可以在构造函数中不添加上面这行

https://laravel.com/docs/8.x/authorization#via-controller-helpers

public function update(Request $request, Post $post)
{
    $this->authorize('update', $post);

   // The current user can update the blog post...
}

第一个参数是策略方法的名称,第二个参数是post对象