12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Providers;
- use App\Repositories\Enums\CacheEnum;
- use Illuminate\Auth\EloquentUserProvider as BaseEloquentUserProvider;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Str;
- class EloquentUserProvider extends BaseEloquentUserProvider
- {
- /**
- * Retrieve a user by their unique identifier.
- *
- * @param mixed $identifier
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function retrieveById($identifier)
- {
- /**
- * 区别guards
- * mead
- */
- // $class_name = get_class($this->createModel());
- // $cacheKey = CacheEnum::getCacheKey(CacheEnum::AUTHORIZATION_USER, $identifier) . ":{$class_name}";
- $cacheKey = CacheEnum::getCacheKey(CacheEnum::AUTHORIZATION_USER, $identifier);
- $cacheExpireTime = CacheEnum::getCacheExpireTime(CacheEnum::AUTHORIZATION_USER);
- return Cache::remember($cacheKey, $cacheExpireTime, function () use ($identifier) {
- $model = $this->createModel();
- return $this->newModelQuery($model)
- ->where($model->getAuthIdentifierName(), $identifier)
- ->first();
- });
- }
- /**
- * Retrieve a user by the given credentials.
- *
- * @param array $credentials
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function retrieveByCredentials(array $credentials)
- {
- if (empty($credentials) ||
- (count($credentials) === 1 &&
- Str::contains($this->firstCredentialKey($credentials), 'password'))) {
- return;
- }
- // First we will add each credential element to the query as a where clause.
- // Then we can execute the query and, if we found a user, return it in a
- // Eloquent User "model" that will be utilized by the Guard instances.
- $query = $this->newModelQuery();
- foreach ($credentials as $key => $value) {
- if (Str::contains($key, 'password')) {
- continue;
- }
- if (is_array($value) || $value instanceof Arrayable) {
- $query->whereIn($key, $value);
- } else {
- $query->where($key, $value);
- }
- }
- return $query->first();
- }
- }
|