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

安娜·加西亚

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

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

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

有没有办法绕过中间件重定向或类似的东西?

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

创建策略

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)
    {
        //
    }
}

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

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 将成为您的路由参数

如果您没有使用资源丰富的控制器,或者想手动授权操作,那么您可以使用而无需在构造函数中添加上述行

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 对象

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章