Can not call Auth class inside Scope class in Laravel 5.2

隐身守侯 提交于 2020-03-26 21:33:31

问题


I added External Scope for User Model. Simply created Scope, with the name DeveloperScope

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Auth;

class DeveloperScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        if(Auth::user()->id != 1){
            $builder->where('id', '<>', 1);
        }
    }
}

Then, I called this scope for User model.

User Model

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Scopes\DeveloperScope;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use SoftDeletes;
    use Authenticatable, CanResetPassword;


    protected $table = 'users';

    protected $fillable = ['first_name', 'last_name', 'email', 'password'];

    protected $hidden = ['password', 'remember_token'];

    protected $dates = ['deleted_at'];

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new DeveloperScope);
    }
}

It works well, when I don't use Auth::class inside DeveloperScope class. The reason is that, I just want to hide the main user for another users for all Eloquent methods. Of course, I can use session instead of Auth and retrieve user id. But it is still interesting for me, why browser gives an error: ERR_EMPTY_RESPONSE while using Auth::class ?


回答1:


The method what you are looking for is exactly Auth::hasUser(). The method was added in Laravel 5.6 through my PR. (So you need to upgrade Laravel first)

[5.6] Add ability to determine if the current user is ALREADY authenticated without triggering side effects by mpyw · Pull Request #24518 · laravel/framework

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class DeveloperScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        if (!Auth::hasUser() || Auth::user()->id != 1) {
            $builder->where('id', '<>', 1);
        }
    }
}

Just call Auth::hasUser() to prevent Auth::user() from causing side effects.




回答2:


Try injecting the Illuminate\Auth\Guard class and then call the user method on it. e.g.:

$this->auth->user();

You should be fine with just type-hinting it since Laravel will automatically new up an instance for you.

For more info check out the API: https://laravel.com/api/5.2/Illuminate/Auth/Guard.html



来源:https://stackoverflow.com/questions/41824174/can-not-call-auth-class-inside-scope-class-in-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!