Yii::t('common','Username'), 'password' => Yii::t('common','Password'), 'rememberMe' => Yii::t('common','Remember Me'), ]; } /** * Validates the password. * This method serves as the inline validation for password. * * @param string $attribute the attribute currently being validated * @param array $params the additional name-value pairs given in the rule */ public function validatePassword($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); if (!$user || !$user->validatePassword($this->password)) { $this->addError($attribute, Yii::t('common','Incorrect username or password.')); } } } /** * Logs in a user using the provided username and password. * * @return bool whether the user is logged in successfully */ public function login() { if ($this->validate()) { return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); } return false; //如果为true 将允许任意密码登陆 } /** * Finds user by [[username]] * * @return User|null */ protected function getUser() { if ($this->_user === null) { $this->_user = User::findByUsername($this->username); //千万注意后端的登陆使用的是Adminer模型,不是User } return $this->_user; } //登录记录 public function loginLog(){ $request = Yii::$app->getRequest(); $nowFile = $request -> getScriptFile(); $data = "登陆系统|途径:".$nowFile; $model = new CLog(); $model->username = $this->username; $model->ip = Yii::$app->request->userIP; $model->data = $data; $model->create_time = time(); $model->save(); //编制邮件发送通知 $message = Yii::$app->mailer->compose(); $message->setFrom(Yii::$app->params['noticeEmail']); //给登陆者发送 $subject = "账号登陆提醒"; $body = "您的账号【".$this->username."】刚刚登陆了网站:".Yii::$app->getRequest()->absoluteUrl; $message->setTo(Yii::$app->user->identity->email) ->setSubject($subject) ->setTextBody($body) ->send(); //给管理人员发送 $subject = "账号登陆提醒"; $body = "API站账号【".$this->username."】刚刚登陆了网站:".Yii::$app->getRequest()->absoluteUrl; $message->setTo('1280880631@qq.com') ->setSubject($subject) ->setTextBody($body) ->send(); } }