AuthForm.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace backend\forms;
  3. use common\models\AccessToken;
  4. use common\models\Client;
  5. use common\models\User;
  6. use components\Exception;
  7. use Yii;
  8. class AuthForm extends BaseForm
  9. {
  10. /**
  11. * @var
  12. */
  13. public $username;
  14. /**
  15. * @var
  16. */
  17. public $password;
  18. /**
  19. * @var null
  20. */
  21. public $access_token = null;
  22. /**
  23. * @var null
  24. */
  25. private $_user = null;
  26. /**
  27. * @var
  28. */
  29. private $_response;
  30. /**
  31. * 失效时长
  32. */
  33. const EXPIRES = 30 * 86400;
  34. public function rules()
  35. {
  36. return [
  37. [['username', 'password'], 'required', 'on' => ['access_token']],
  38. [['username', 'password'], 'trim', 'on' => ['access_token']],
  39. //['username', 'validateUser', 'on' => 'access_token'],
  40. ['password', 'validatePassword', 'on' => ['access_token']],
  41. ];
  42. }
  43. public function validatePassword($attribute)
  44. {
  45. if (!$this->hasErrors()) {
  46. if (!$this->findUser(['username' => $this->username, 'status' => User::STATUS_ACTIVE])
  47. || !$this->password
  48. || !$this->_user['password']
  49. || !Yii::$app->security->validatePassword($this->password, $this->_user['password']))
  50. {
  51. $this->addError($attribute, 2001);
  52. }
  53. }
  54. }
  55. /**
  56. * getAccessToken
  57. * @author: libingke
  58. * @return string
  59. */
  60. public function getAccessToken()
  61. {
  62. $this->generateAccessToken();
  63. return $this->_response;
  64. }
  65. public function generateAccessToken()
  66. {
  67. $this->access_token = Yii::$app->security->generateRandomString();
  68. $expires = strtotime(date('Y-m-d 23:59:59')) + static::EXPIRES;
  69. //insert
  70. if ( !($one = AccessToken::findOne(['access_token' => $this->access_token])) ) {
  71. $model = new AccessToken();
  72. $model->access_token = $this->access_token;
  73. $model->user_id = $this->_user['uid'];
  74. $model->ip = isset(Yii::$app->request->userIP) ? Yii::$app->request->userIP : '';
  75. $model->user_agent = isset(Yii::$app->request->userAgent) ? Yii::$app->request->userAgent : '';
  76. $model->expires = $expires;
  77. if (!$model->save(false))
  78. throw new Exception(2002);
  79. }
  80. $this->_response = array(
  81. "access_token" => $this->access_token,
  82. "expires" => $expires
  83. );
  84. }
  85. /**
  86. * findUser
  87. * @author: libingke
  88. * @param array $query
  89. */
  90. public function findUser(Array $query = [])
  91. {
  92. if (!$this->_user) {
  93. $user = User::findOne($query);
  94. if ($user)
  95. $this->_user = [
  96. 'uid' => $user->id,
  97. 'username' => $user->username,
  98. 'password' => $user->password_hash
  99. ];
  100. }
  101. return $this->_user;
  102. }
  103. }