Skip to content

Commit

Permalink
添加登录验证码
Browse files Browse the repository at this point in the history
  • Loading branch information
shi-yang committed Feb 9, 2016
1 parent d2411c0 commit e2651ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
8 changes: 6 additions & 2 deletions backend/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Yii;
use yii\filters\AccessControl;
use common\components\BaseController;
use common\models\LoginForm;
use backend\models\LoginForm;

/**
* Site controller
Expand All @@ -21,7 +21,7 @@ public function behaviors()
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'actions' => ['login', 'error', 'captcha'],
'allow' => true,
],
[
Expand All @@ -43,6 +43,10 @@ public function actions()
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}

Expand Down
39 changes: 34 additions & 5 deletions backend/models/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
public $rememberMe = false;
public $verifyCode;

private $_user = false;

Expand All @@ -27,6 +28,20 @@ public function rules()
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
['verifyCode', 'required'],
['verifyCode', 'captcha'],
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'username' => Yii::t('app', 'Username or Email'),
'password' => Yii::t('app', 'Password'),
'rememberMe' => Yii::t('app', 'Remember Me'),
];
}

Expand All @@ -40,9 +55,11 @@ public function rules()
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$password_hash = Yii::$app->setting->get('admin_password');
if (!Yii::$app->security->validatePassword($this->password, $password_hash)) {
$this->addError($attribute, 'Incorrect username or password.');
$user = $this->getUser();
if (!$user) {
$this->addError('username', Yii::t('app', 'Username does not exist.'));
} elseif (!$user->validatePassword($this->password)) {
$this->addError('password', Yii::t('app', 'Incorrect password.'));
}
}
}
Expand All @@ -54,11 +71,23 @@ public function validatePassword($attribute, $params)
*/
public function login()
{
$administrator = Yii::$app->setting->get('administrator');
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}

/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
8 changes: 6 additions & 2 deletions backend/views/site/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</div><!-- /.login-logo -->
<div class="login-box-body">
<p class="login-box-msg">Sign in to start your session</p>
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?php $form = ActiveForm::begin(['id' => 'login-form', 'enableClientValidation' => false]); ?>
<?= $form->field($model, 'username', [
'inputOptions' => [
'placeholder' => $model->getAttributeLabel('username'),
Expand All @@ -27,12 +27,16 @@
'placeholder' => $model->getAttributeLabel('password'),
]
])->passwordInput()->label(false); ?>

<?= $form->field($model, 'verifyCode')->widget(\yii\captcha\Captcha::classname(), [
// configure additional widget properties here
]) ?>

<div class="row">
<div class="col-xs-4">
<?= Html::submitButton('Sign me in', ['class' => 'btn btn-primary btn-block btn-flat', 'name' => 'login-button']) ?>
</div><!-- /.col -->
</div>
<?php ActiveForm::end(); ?>
<a href="#">I forgot my password</a><br>
</div><!-- /.login-box-body -->
</div>

0 comments on commit e2651ae

Please sign in to comment.