簡體   English   中英

Laravel 中間件限制對不需要的功能的訪問

[英]Laravel middleware limits access to unwanted functions

我在 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();

        //
    }
}

最后授權您的 controller,將此行添加到構造函數

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

請注意,function 調用中的第二個參數是路由參數的名稱,如果您創建了資源豐富的 controller,則 post 將成為您的路由參數

如果你沒有使用資源豐富的 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...
}

第一個參數是策略方法的名稱,第二個參數是帖子 object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM