Laravel 5.4盖茨和政策:政策是否可以覆盖盖茨?

时间:2017-05-20 18:47:38

标签: php laravel laravel-5 laravel-5.4 policies

UserPolicy似乎覆盖了我所做的大门。 说明: 在下面的AuthServiceProvider中,当我注释掉行

User::class => UserPolicy::class

在受保护的政策中,我的盖茨工作正常。

什么可能使大门淹没? (以下文件)

AuthServiceProvider:

<?php

namespace App\Providers;

use App\Project;
use App\Comment;
use App\ProjectImage;
use App\User;
use App\Policies\ProjectPolicy;
use App\Policies\CommentPolicy;
use App\Policies\ProjectImagePolicy;
use App\Policies\UserPolicy;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as       ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{

    protected $policies = [
        Project::class => ProjectPolicy::class,
        Comment::class => CommentPolicy::class,
        ProjectImage::class => ProjectImagePolicy::class,
        User::class => UserPolicy::class
    ];


     public function boot()
    {
        $this->registerPolicies();

        Gate::define('follow', function(User $me, User $target){
            return $me->id !== $target->id
                   && !$me->subscriptions->contains($target->id);
        });
        Gate::define('unfollow', function(User $me, User $target){
            return $me->subscriptions->contains($target->id);
        });
    }
}

UserPolicy

<?php

namespace App\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Support\Facades\Auth;

class UserPolicy
{
    use HandlesAuthorization;

    public function before($user)
    {
        return $user->isAdmin();
    }


    public function viewUnpublished(User $me, User $target)
    {
        return $me->id === $target->id;
    }


    public function update(User $me, User $target)
    {
        return $me->id === $target->id;
    }
}

我呼叫大门的UserController:

public function show(User $user)
{
    $refDate = new Carbon;
    $refDate->subMonth();
    $publishedProjects = $user->projects->where('published', true);
    if(Route::is('unpublished')){
        $this->authorize('viewUnpublished', $user);
    }

    $canFollow = Gate::allows('follow', $user) ? true : false;
    $canUnfollow = Gate::allows('unfollow', $user) ? true : false;

    $unpublishedProjects = $user->projects->where('published', false);
    return view('user.profile', compact('user', 'publishedProjects', 'refDate', 'unpublishedProjects', 'canFollow', 'canUnfollow'));
}

0 个答案:

没有答案