| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace App\Providers;
- use App\Models\User;
- use Illuminate\Auth\DatabaseUserProvider;
- use Illuminate\Contracts\Auth\Authenticatable as UserContract;
- use Illuminate\Support\Facades\Auth;
- class AppUserProvider extends DatabaseUserProvider
- {
- /**
- * Retrieve a user by their unique identifier.
- *
- * @param mixed $identifier
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function retrieveById($identifier)
- {
- $user = $this->conn->table($this->table)->where([
- 'sys_user_id' => $identifier,
- ])->where('status', '<>', 2)->first();
- $user->api_token = Auth::getPayload()->get('api_token');
- return $this->getGenericUser($user);
- }
- /**
- * Retrieve a user by their unique identifier and "remember me" token.
- *
- * @param mixed $identifier
- * @param string $token
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function retrieveByToken($identifier, $token)
- {
- return null;
- }
- /**
- * Retrieve a user by the given credentials.
- *
- * @param array $credentials
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
- */
- public function retrieveByCredentials(array $credentials)
- {
- if ($credentials['sys_user_id']) {
- // 用户中心已验证好、直接授权
- $user = [
- 'id'=>$credentials['sys_user_id'],
- 'sys_user_id'=>$credentials['sys_user_id'],
- 'user_name'=>$credentials['user_name'],
- 'nick_name'=>$credentials['nick_name'],
- 'api_token'=>$credentials['api_token'],
- 'password'=>'',
- 'phone'=>$credentials['phone'],
- 'email'=>$credentials['email'],
- 'create_time'=>$credentials['create_time'],
- 'update_time'=>$credentials['update_time']
- ];
- } else {
- // 查数据库授权
- $query = $this->conn->table($this->table);
- $user = $query->where('username', $credentials['username'])
- ->where('status', '<>', 2)->first();
- }
- return $this->getGenericUser($user);
- }
- /**
- * Get the generic user.
- *
- * @param mixed $user
- * @return \Illuminate\Auth\GenericUser|null
- */
- protected function getGenericUser($user)
- {
- if (!is_null($user)) {
- return new User((array)$user);
- }
- }
- protected $model = User::class;
- /**
- * Gets the name of the Eloquent user model.
- *
- * @return string
- */
- public function getModel()
- {
- return $this->model;
- }
- /**
- * Sets the name of the Eloquent user model.
- *
- * @param string $model
- * @return $this
- */
- public function setModel($model)
- {
- $this->model = $model;
- return $this;
- }
- public function validateCredentials(UserContract $user, array $credentials)
- {
- return true;
- }
- }
|