LoginForm.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace backendApi\modules\v1\models;
  3. use common\components\Model;
  4. use common\helpers\LoggerTool;
  5. use common\helpers\Tool;
  6. use common\libs\LoginIpChecker;
  7. use Yii;
  8. use yii\base\Exception;
  9. use yii\captcha\Captcha;
  10. use common\libs\logging\login\AdminLogin as AdminLoginLogger;
  11. /**
  12. * Login form
  13. */
  14. class LoginForm extends Model {
  15. public $adminName;
  16. public $password;
  17. public $verifyCode;
  18. public $lang;
  19. private $_user;
  20. const ERROR_IS_MODIFY_PASSWORD = 'ERROR_IS_MODIFY_PASSWORD';
  21. /**
  22. * @inheritdoc
  23. */
  24. public function attributeLabels()
  25. {
  26. return [
  27. 'adminName' => '登录帐号',
  28. 'password' => '登录密码',
  29. 'verifyCode' => '验证码',
  30. 'lang' => '语言标识',
  31. ];
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function rules() {
  37. return [
  38. // username and password are both required
  39. [['adminName', 'password', 'verifyCode'], 'required'],
  40. // rememberMe must be a boolean value
  41. ['verifyCode', 'captcha', 'captchaAction'=>'/v1/site/captcha'],
  42. // password is validated by validatePassword()
  43. //['password', 'validatePassword'],
  44. ];
  45. }
  46. /**
  47. * Validates the password.
  48. * This method serves as the inline validation for password.
  49. *
  50. * @param string $attribute the attribute currently being validated
  51. * @param array $params the additional name-value pairs given in the rule
  52. */
  53. public function validatePassword($attribute, $params) {
  54. if (!$this->hasErrors()) {
  55. $this->getUser();
  56. if(!$this->_user){
  57. $this->addError($attribute, 'Member does not exist');
  58. return false;
  59. }
  60. if (!$this->_user->validatePassword($this->password)) {
  61. $this->addError($attribute, '用户名或者密码错误');
  62. return false;
  63. }
  64. //验证IP
  65. $bindIp = trim($this->_user['BIND_IP']);
  66. if(!empty($bindIp) && !(new LoginIpChecker(Yii::$app->request->getUserIP(), $bindIp))->validate()){
  67. $this->addError($attribute, '登录IP与此账号绑定的IP不符');
  68. return false;
  69. }
  70. return true;
  71. }
  72. return false;
  73. }
  74. /**
  75. * 更新失败次数
  76. * @param $transaction
  77. * @param $returnResult
  78. * @throws \Exception
  79. */
  80. private function _updateFailTimes($transaction,$returnResult){
  81. Admin::updateAllCounters([
  82. 'FAIL_NUMS' => 1,
  83. ], 'ADMIN_NAME=:ADMIN_NAME', ['ADMIN_NAME' => $this->adminName]);
  84. $transaction->commit();
  85. if(isset($this->_user)){
  86. AdminLoginLogger::fail($this->_user,$returnResult);
  87. }
  88. }
  89. /**
  90. * 更新成功次数
  91. */
  92. private function _updateSuccessTimes(){
  93. Admin::updateAllCounters([
  94. 'LOGIN_NUMS' => 1,
  95. ], 'ADMIN_NAME=:ADMIN_NAME', ['ADMIN_NAME' => $this->adminName]);
  96. }
  97. /**
  98. * 更新系统语言类型
  99. * @param $lang
  100. * @return void
  101. */
  102. private function _updateLang($lang) {
  103. $lang = Tool::langConvert($lang);
  104. Admin::updateAll([
  105. 'LANG' => $lang,
  106. ], 'ADMIN_NAME=:ADMIN_NAME', ['ADMIN_NAME' => $this->adminName]);
  107. }
  108. /**
  109. * 登录
  110. * @return array|bool
  111. * @throws \yii\base\Exception
  112. * @throws \yii\db\Exception
  113. */
  114. public function login(){
  115. if(!$this->validate()){
  116. return false;
  117. }
  118. $transaction = \Yii::$app->db->beginTransaction();
  119. try{
  120. $this->getUser();
  121. if(!$this->_user){
  122. throw new Exception('The account does not exist'); // 账号不存在
  123. }
  124. if(!$this->_user['IS_ENABLE']){
  125. $this->_updateFailTimes($transaction,'账号已经被锁定,无法登录');
  126. throw new Exception('The account has been locked and cannot be logged in'); // 账号已经被锁定,无法登录
  127. }
  128. if (!$this->_user->validatePassword($this->password)) {
  129. $this->_updateFailTimes($transaction,'用户名或者密码错误');
  130. throw new Exception('Incorrect user name or password'); // 用户名或者密码错误
  131. }
  132. //验证IP
  133. $bindIp = trim($this->_user['BIND_IP']);
  134. if(!empty($bindIp) && !(new LoginIpChecker(Yii::$app->request->getUserIP(), $bindIp))->validate()){
  135. $this->_updateFailTimes($transaction,'登录IP与此账号绑定的IP不符');
  136. throw new Exception('The login IP does not match the IP bound to this account'.$bindIp); // 登录IP与此账号绑定的IP不符
  137. }
  138. //需要修改密码
  139. if($this->_user['IS_MODIFY_PASSWORD'] == 1){
  140. throw new Exception(self::ERROR_IS_MODIFY_PASSWORD);
  141. }
  142. $this->_updateSuccessTimes();
  143. // 如果用户没有设置语言类型,则选择前端传递的语言类型,并存储到表中
  144. if (!$this->_user['LANG']) {
  145. $this->_updateLang($this->lang);
  146. }
  147. $transaction->commit();
  148. AdminLoginLogger::success($this->_user);
  149. // 把用户的登录时间存在操作时间里
  150. Yii::$app->tokenRedis->hset('admin:timeOut', $this->_user->getId(), time());
  151. return Yii::$app->user->loginWithUAndP($this->_user);
  152. }catch(\Exception $e){
  153. $transaction->rollBack();
  154. $this->setError($e->getMessage());
  155. //AdminLoginLogger::fail($this->_user, $e->getMessage());
  156. return false;
  157. }
  158. }
  159. /**
  160. * Finds user by [[username]]
  161. *
  162. * @return User|null
  163. */
  164. public function getUser() {
  165. if ($this->_user === null) {
  166. $this->_user = User::findByUsername(strtolower($this->adminName));
  167. }
  168. return $this->_user;
  169. }
  170. }