无法在Laravel 5.2中调用Scope类中的Auth类

时间:2017-01-24 09:12:48

标签: laravel laravel-5.2

我添加了用户模型的外部范围。只需创建Scope,名称为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);
        }
    }
}

然后,我将此范围称为用户模型。

用户模型

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);
    }
}

当我在Auth::class课程中不使用DeveloperScope时,效果很好。原因是,我只想为所有Eloquent方法隐藏其他用户的主用户。当然,我可以使用session而不是Auth并检索用户ID。但是对我来说仍然很有趣,为什么浏览器在使用ERR_EMPTY_RESPONSE时会出错:Auth::class

2 个答案:

答案 0 :(得分:0)

尝试注入[Test] public void TestMethod2() { #region variableDecleration var adHost = new ActiveDirectoryHost(); adHost.AdPath = @"C:\User\Security\1.0.0.0"; adHost.CnNames = "CN=USERS"; adHost.DomainName = "USER.COM"; adHost.DomainPath = "DC=USER,DC=COM"; adHost.Fqn = "HOSTNAME.USER.COM.USER.COM"; adHost.HostName = "hostname.user.com"; adHost.OuPath = "OU=NewSecur"; adHost.SuperGroupName = "usergroup_1"; adHost.IpAddress = "xxx.xx.xx.x"; var adUserName = "username"; var adPassword = "password"; #endregion variableDecleration var validation = Substitute.ForPartsOf<ValidationHandler>(); validation.GetUserGroupList(userName: Arg.Is(adUserName ), recursive: Arg.Is(false), adHost: Arg.Is(adHost), adUsername: Arg.Is(adUserName), adPassword: Arg.Is(adPassword)).Returns("usergroup_1,usergroup_2"); var isUserInGroup = validation.IsUserMemberOfGroup(adUsername, "usergroup_1", adHost, adUserName, adPassword); Assert.That(isUserInGroup, Is.EqualTo(true)); } 类,然后在其上调用用户方法。例如:

Illuminate\Auth\Guard

你应该没关系它,因为Laravel会自动为你创建一个实例。

有关详细信息,请查看API:https://laravel.com/api/5.2/Illuminate/Auth/Guard.html

答案 1 :(得分:0)

您要查找的方法正是 Auth::hasUser() 。该方法是通过我的PR在Laravel 5.6中添加的。 (因此,您需要先升级Laravel)

[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);
        }
    }
}

只需调用Auth::hasUser()即可防止Auth::user()引起副作用。