AdminLoginForm.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\modules\admin\models;
  3. use Yii;
  4. use app\common\models\EModel;
  5. use app\models\LoginTimes;
  6. use app\models\IpBanned;
  7. use app\common\helpers\Cookie;
  8. use app\common\helpers\Session;
  9. class AdminLoginForm extends EModel
  10. {
  11. public $user_name;
  12. public $user_pwd;
  13. public $code;
  14. private $retryTimes;//后台登录允许尝试次数
  15. private $timeOut=3600;//后台登录错误次数清零时限
  16. public $loginResult = array();
  17. public function init()
  18. {
  19. $this->retryTimes = Yii::$app->params['retryTimes'];
  20. }
  21. //表单验证规则
  22. public function rules()
  23. {
  24. return array(
  25. array(['user_name','user_pwd','code'], 'required'),
  26. array('user_name', 'match','pattern'=>'/^[\x{4e00}-\x{9fa5}_a-zA-Z0-9]{5,36}$/u','message'=>'无效账号或密码'),
  27. array('code', 'match','pattern'=>'/^[\w\_]{4,6}$/','message'=>'无效验证码'),
  28. array('user_pwd', 'match','pattern'=>'/^[0-9a-zA-Z_!@#$%^&*?]{6,32}$/','message'=>'无效账号或密码'),
  29. );
  30. }
  31. public function attributeLabels()
  32. {
  33. return array(
  34. 'user_name' => Yii::t('attr','admin.user_name'),
  35. 'user_pwd' => Yii::t('attr','admin.user_pwd'),
  36. 'code' => Yii::t('attr','admin.code'),
  37. );
  38. }
  39. //判断是否已经超过尝试次数,超过则中断
  40. private function _exceededTimes()
  41. {
  42. $ip = MYIP;
  43. $startTime = intval(TIMESTAMP-$this->timeOut);
  44. $model = LoginTimes::find()->where('user_name=:user_name and ip=:ip and is_admin=1 and login_time>:login_time',array(':user_name'=>$this->user_name,':ip'=>$ip,':login_time'=>$startTime))->limit(1)->one();
  45. if(!empty($model))
  46. {
  47. $times = $model->times;
  48. if($times>=$this->retryTimes)
  49. {
  50. return true;
  51. }
  52. else
  53. {
  54. return false;
  55. }
  56. }
  57. else
  58. {
  59. return false;
  60. }
  61. }
  62. //登录尝试次数控制
  63. private function _failedTimes()
  64. {
  65. $ip = MYIP;
  66. $startTime = intval(TIMESTAMP-$this->timeOut);
  67. $model = LoginTimes::find()->where('user_name=:user_name and ip=:ip and is_admin=1 and login_time>:login_time',array(':user_name'=>$this->user_name,':ip'=>$ip,':login_time'=>$startTime))->limit(1)->one();;
  68. if(!empty($model))
  69. {
  70. $times = $model->times;
  71. if($times>=$this->retryTimes)
  72. {
  73. /*$ipBanned = new IpBanned();
  74. $ipBanned->ip = MYIP;
  75. $ipBanned->expires = TIMESTAMP+3600;
  76. $ipBanned->save();*/
  77. $timesInfo = '您的登录失败次数达到上限!';
  78. }
  79. else
  80. {
  81. $times++;
  82. $model->times=$times;
  83. $model->login_time = TIMESTAMP;
  84. $model->save();
  85. if($times==$this->retryTimes)
  86. {
  87. /*$ipBanned = new IpBanned();
  88. $ipBanned->ip = MYIP;
  89. $ipBanned->expires = TIMESTAMP+3600;
  90. $ipBanned->save();*/
  91. $timesInfo = '您的登录失败次数达到上限!';
  92. }
  93. else
  94. {
  95. $timesInfo = '您还能尝试登录'.intval($this->retryTimes-$times).'次!';
  96. }
  97. }
  98. }
  99. else
  100. {
  101. //清空超出时限的登录尝试记录
  102. LoginTimes::deleteAll('user_name=:user_name and is_admin=1',array(':user_name'=>$this->user_name));
  103. $model = new LoginTimes;
  104. $model->user_name = $this->user_name;
  105. $model->ip = $ip;
  106. $model->login_time = TIMESTAMP;
  107. $model->is_admin=1;
  108. $model->times = 1;
  109. $model->save();
  110. $timesInfo = '您还能尝试登录'.intval($this->retryTimes-1).'次!';
  111. }
  112. return $timesInfo;
  113. }
  114. //后台登录
  115. public function login()
  116. {
  117. if(empty($this->code))
  118. {
  119. $this->addError('AdminLoginForm','验证码不能为空');
  120. return false;
  121. }
  122. if(strtolower($this->code)!=strtolower(Session::getInitCls()->get(Yii::$app->params['capcha'])))
  123. {
  124. $this->addError('AdminLoginForm','验证码不正确');
  125. return false;
  126. }
  127. // if(TIMESTAMP-Yii::$app->session[Yii::$app->params['capchaTime']]>Yii::$app->params['capchaTimeout'])
  128. // {
  129. //
  130. // $this->addError('AdminLoginForm','验证码超时,请刷新后重试');
  131. // return false;
  132. // }
  133. if(empty($this->user_name))
  134. {
  135. $this->addError('AdminLoginForm','账号不能为空');
  136. return false;
  137. }
  138. if(empty($this->user_pwd))
  139. {
  140. $this->addError('AdminLoginForm','密码不能为空');
  141. return false;
  142. }
  143. //如果已经超出尝试次数
  144. if($this->_exceededTimes())
  145. {
  146. $this->addError('AdminLoginForm','您的登录失败次数达到上限');
  147. return false;
  148. }
  149. //获取管理员信息
  150. $admin = Admin::find()->where('MD5(user_name)=:user_name',array(':user_name'=>$this->user_name))->limit(1)->one();
  151. //管理员不存在
  152. if(empty($admin))
  153. {
  154. $this->addError('AdminLoginForm','账号或密码错误'.$this->_failedTimes());
  155. return false;
  156. }
  157. //管理员已被禁用
  158. if($admin->disabled==1)
  159. {
  160. $this->addError('AdminLoginForm','账号已被禁用');
  161. return false;
  162. }
  163. $this->user_name = $admin->user_name;
  164. //密码不正确
  165. if(md5($this->user_pwd.$admin->encrypt)!=$admin->user_pwd)
  166. {
  167. $this->addError('AdminLoginForm','账号或密码错误'.$this->_failedTimes());
  168. return false;
  169. }
  170. else
  171. {
  172. $role = Role::find()->where(['role_id'=>$admin->role_id])->one();
  173. //角色被禁用
  174. if(empty($role)||$role->disabled)
  175. {
  176. $this->addError('AdminLoginForm','角色已被禁用,请联系管理员');
  177. return false;
  178. }
  179. else
  180. {
  181. $identityInfo = array();
  182. $identityInfo['admin_id']=$admin->admin_id;
  183. $identityInfo['user_name']=$admin->user_name;
  184. $identityInfo['role_id']=$admin->role_id;
  185. $identityInfo['email']=$admin->email;
  186. $identityInfo['real_name']=$admin->real_name;
  187. $identityInfo['role_name']=$role->role_name;
  188. $identityInfo['avatar']=$admin->avatar;
  189. $identityInfo['cookieHash'] = sys_auth($admin->user_name);
  190. if(!empty($admin->last_login_ip))
  191. {
  192. $identityInfo['last_login_ip']=$admin->last_login_ip;
  193. }
  194. if(!empty($admin->last_login_time)){
  195. $identityInfo['last_login_time']=$admin->last_login_time;
  196. }
  197. $identityInfo = sys_auth(array2string($identityInfo));
  198. Cookie::setCookie(Yii::$app->params['adminCookieName'],$identityInfo);//使用COOKIE记录用户身份信息
  199. //为锁屏功能设置的一个开关变量
  200. Cookie::setCookie('lockscreen',0);
  201. //更新用户最后登录时间和IP
  202. $admin->last_login_ip = MYIP;
  203. $admin->last_login_time = TIMESTAMP;
  204. if($admin->save(false))
  205. {
  206. return true;
  207. }
  208. }
  209. }
  210. }
  211. }