LoginForm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace backend\forms;
  3. use Yii;
  4. use common\helpers\StringHelper;
  5. use common\enums\MemberTypeEnum;
  6. use common\helpers\ArrayHelper;
  7. use common\enums\SubscriptionActionEnum;
  8. /**
  9. * Class LoginForm
  10. * @package backend\forms
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class LoginForm extends \common\forms\LoginForm
  14. {
  15. /**
  16. * 校验验证码
  17. *
  18. * @var
  19. */
  20. public $verifyCode;
  21. /**
  22. * 默认登录失败3次显示验证码
  23. *
  24. * @var int
  25. */
  26. public $attempts = 3;
  27. /**
  28. * @var bool
  29. */
  30. public $rememberMe = true;
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['username', 'password'], 'required'],
  38. ['rememberMe', 'boolean'],
  39. ['password', 'validatePassword'],
  40. ['password', 'validateIp'],
  41. ['verifyCode', 'captcha', 'on' => 'captchaRequired'],
  42. ];
  43. }
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'username' => '用户名',
  48. 'rememberMe' => '记住我',
  49. 'password' => '密码',
  50. 'verifyCode' => '验证码',
  51. ];
  52. }
  53. /**
  54. * 验证ip地址是否正确
  55. *
  56. * @param $attribute
  57. * @throws \yii\base\InvalidConfigException
  58. */
  59. public function validateIp($attribute)
  60. {
  61. $allowIp = Yii::$app->services->config->backendConfig('sys_allow_ip');
  62. if (!empty($allowIp)) {
  63. $ipList = StringHelper::parseAttr($allowIp);
  64. if (!ArrayHelper::ipInArray(Yii::$app->request->userIP, $ipList)) {
  65. // 记录行为日志
  66. Yii::$app->services->actionLog->create('login', '限制IP登录', 0, [], false);
  67. $this->addError($attribute, '登录失败,请联系管理员');
  68. }
  69. }
  70. }
  71. /**
  72. * @return mixed|null|static
  73. */
  74. public function getUser()
  75. {
  76. if ($this->_user === null) {
  77. $this->_user = Yii::$app->services->member->findByCondition([
  78. 'username' => $this->username,
  79. 'type' => MemberTypeEnum::MANAGER,
  80. ]);
  81. }
  82. return $this->_user;
  83. }
  84. /**
  85. * 验证码显示判断
  86. */
  87. public function loginCaptchaRequired()
  88. {
  89. if (Yii::$app->session->get('loginCaptchaRequired') >= $this->attempts) {
  90. $this->setScenario("captchaRequired");
  91. // 提醒
  92. Yii::$app->services->notify->sendRemind(0, SubscriptionActionEnum::ABNORMAL_LOGIN, 0, [
  93. 'username' => $this->username,
  94. 'attempts' => Yii::$app->session->get('loginCaptchaRequired')
  95. ]);
  96. }
  97. }
  98. /**
  99. * 登录
  100. *
  101. * @return bool
  102. * @throws \yii\base\InvalidConfigException
  103. */
  104. public function login()
  105. {
  106. if ($this->validate() && Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0)) {
  107. Yii::$app->session->remove('loginCaptchaRequired');
  108. return true;
  109. }
  110. $counter = Yii::$app->session->get('loginCaptchaRequired') + 1;
  111. Yii::$app->session->set('loginCaptchaRequired', $counter);
  112. return false;
  113. }
  114. }
粤ICP备19079148号