AppUserProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Providers;
  3. use App\Models\User;
  4. use Illuminate\Auth\DatabaseUserProvider;
  5. use Illuminate\Contracts\Auth\Authenticatable as UserContract;
  6. use Illuminate\Support\Facades\Auth;
  7. class AppUserProvider extends DatabaseUserProvider
  8. {
  9. /**
  10. * Retrieve a user by their unique identifier.
  11. *
  12. * @param mixed $identifier
  13. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  14. */
  15. public function retrieveById($identifier)
  16. {
  17. $user = $this->conn->table($this->table)->where([
  18. 'sys_user_id' => $identifier,
  19. ])->where('status', '<>', 2)->first();
  20. $user->api_token = Auth::getPayload()->get('api_token');
  21. return $this->getGenericUser($user);
  22. }
  23. /**
  24. * Retrieve a user by their unique identifier and "remember me" token.
  25. *
  26. * @param mixed $identifier
  27. * @param string $token
  28. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  29. */
  30. public function retrieveByToken($identifier, $token)
  31. {
  32. return null;
  33. }
  34. /**
  35. * Retrieve a user by the given credentials.
  36. *
  37. * @param array $credentials
  38. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  39. */
  40. public function retrieveByCredentials(array $credentials)
  41. {
  42. if ($credentials['sys_user_id']) {
  43. // 用户中心已验证好、直接授权
  44. $user = [
  45. 'id'=>$credentials['sys_user_id'],
  46. 'sys_user_id'=>$credentials['sys_user_id'],
  47. 'user_name'=>$credentials['user_name'],
  48. 'nick_name'=>$credentials['nick_name'],
  49. 'api_token'=>$credentials['api_token'],
  50. 'password'=>'',
  51. 'phone'=>$credentials['phone'],
  52. 'email'=>$credentials['email'],
  53. 'create_time'=>$credentials['create_time'],
  54. 'update_time'=>$credentials['update_time']
  55. ];
  56. } else {
  57. // 查数据库授权
  58. $query = $this->conn->table($this->table);
  59. $user = $query->where('username', $credentials['username'])
  60. ->where('status', '<>', 2)->first();
  61. }
  62. return $this->getGenericUser($user);
  63. }
  64. /**
  65. * Get the generic user.
  66. *
  67. * @param mixed $user
  68. * @return \Illuminate\Auth\GenericUser|null
  69. */
  70. protected function getGenericUser($user)
  71. {
  72. if (!is_null($user)) {
  73. return new User((array)$user);
  74. }
  75. }
  76. protected $model = User::class;
  77. /**
  78. * Gets the name of the Eloquent user model.
  79. *
  80. * @return string
  81. */
  82. public function getModel()
  83. {
  84. return $this->model;
  85. }
  86. /**
  87. * Sets the name of the Eloquent user model.
  88. *
  89. * @param string $model
  90. * @return $this
  91. */
  92. public function setModel($model)
  93. {
  94. $this->model = $model;
  95. return $this;
  96. }
  97. public function validateCredentials(UserContract $user, array $credentials)
  98. {
  99. return true;
  100. }
  101. }