Tags

, , , ,

Kurasa dari judulnya sudah cukup jelas apa yang ingin kita peroleh dari tutorial ini. Langsung saja, pertama kita ubah model LoginForm. Di model ini kita tambahkan propety verifyCode beserta rules-nya.

class LoginForm extends Model
{
    public $username;
    public $password;
    public $verifyCode;
    public $rememberMe = true;

    private $_user = false;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            [['verifyCode'], 'required', 'when'=>function(){return $this->captchaNeeded;}],
            [['verifyCode'], 'captcha', 'when'=>function(){return $this->captchaNeeded;}],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     */
    public function validatePassword()
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                Yii::$app->session->set('_try_login', Yii::$app->session->get('_try_login', 0)+1);
                $this->addError('password', 'Incorrect username or password.');
            }
        }
    }

    /**
     * Check if captcha is needed.
     *
     * @return boolean whether the user try to login more than 3 times
     */
    public function getCaptchaNeeded()
    {
        return Yii::$app->session->get('_try_login', 0) > 3;
    }

    /**
     * Logs in a user using the provided username and password.
     *
     * @return boolean 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);
        } 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;
    }
}

Selanjutnya di SiteController kita tambahkan action captcha.

public function actions()
    {
        return [
            'error' => [
                'class' => 'yiiwebErrorAction',
            ],
            'captcha' => [
                'class' => 'yiicaptchaCaptchaAction',
            ],
        ];
    }

Berikutnya adalah memodikifikasi view login. Kita tambahkan inputan untuk captcha. Tambahkan di atas field rememberMe (atau terserah di mana :D)

    <?php 
    if($model->captchaNeeded){
        echo $form->field($model, 'verifyCode')->widget(yiicaptchaCaptcha::className(), [
                    'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
                ]);
    }
    ?>
    <?= $form->field($model, 'rememberMe')->checkbox() ?>

Silakan dicoba 😀

Advertisements