Yii Framework Forum

Captha always incorrect


(Abdulhar) #1

Use extensions yii2/ user master.

  1. Override model - LoginForm.php (\www\basic\models)

  2. Override view - login.php (\www\basic\views\user\security)

  3. Override controller - SecurityController.php (\www\basic\controllers\user)

LoginForm.php




namespace app\models;

use dektrium\user\models\LoginForm as BaseLoginForm;


class LoginForm extends BaseLoginForm

{

    public $vcaptcha;

    public function rules()

    {

        $rules = parent::rules();

        $rules[] = ['vcaptcha', 'required'];

        $rules[] = ['vcaptcha', 'captcha'];


        return $rules;

    }




}



login.php





use dektrium\user\widgets\Connect;

use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\captcha\Captcha;




$this->title = Yii::t('user', 'Sign in');

$this->params['breadcrumbs'][] = $this->title;

?>


<?= $this->render('/_alert', ['module' => Yii::$app->getModule('user')]) ?>


<div class="row">

    <div class="col-md-4 col-md-offset-4">

        <div class="panel panel-default">

            <div class="panel-heading">

                <h3 class="panel-title"><?= Html::encode($this->title) ?></h3>

            </div>

            <div class="panel-body">

                <?php $form = ActiveForm::begin([

                    'id'                     => 'login-form',

                    'enableAjaxValidation'   => true,

                    'enableClientValidation' => false,

                    'validateOnBlur'         => false,

                    'validateOnType'         => false,

                    'validateOnChange'       => false,

                ]) ?>


                <?= $form->field($model, 'login', ['inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control', 'tabindex' => '1']]) ?>


                <?= $form->field($model, 'password', ['inputOptions' => ['class' => 'form-control', 'tabindex' => '2']])->passwordInput()->label(Yii::t('user', 'Password') . ($module->enablePasswordRecovery ? ' (' . Html::a(Yii::t('user', 'Forgot password?'), ['/user/recovery/request'], ['tabindex' => '5']) . ')' : '')) ?>


                <?= $form->field($model, 'vcaptcha')->widget(Captcha::className(), [

                    'template' => '<div class="row"><div class="col-lg-9">{image}</div><div class="col-lg-6">{input}</div></div>',

                    'captchaAction' => 'security/captcha',


                ]) ?>


                <?= $form->field($model, 'rememberMe')->checkbox(['tabindex' => '4']) ?>


                <?= Html::submitButton(Yii::t('user', 'Sign in'), ['class' => 'btn btn-primary btn-block', 'tabindex' => '3']) ?>


                <?php ActiveForm::end(); ?>

                <?php





                ?>




            </div>

        </div>

        <?php if ($module->enableConfirmation): ?>

            <p class="text-center">

                <?= Html::a(Yii::t('user', 'Didn\'t receive confirmation message?'), ['/user/registration/resend']) ?>

            </p>

        <?php endif ?>

        <?php if ($module->enableRegistration): ?>

            <p class="text-center">

                <?= Html::a(Yii::t('user', 'Don\'t have an account? Sign up!'), ['/user/registration/register']) ?>

            </p>

        <?php endif ?>

        <?= Connect::widget([

            'baseAuthUrl' => ['/user/security/auth'],

        ]) ?>

    </div>

</div>




SecurityController.php




namespace app\controllers\user;

use dektrium\user\controllers\SecurityController as BaseSecurityController;


use Yii;

use yii\authclient\AuthAction;

use yii\authclient\ClientInterface;

use yii\filters\AccessControl;

use yii\filters\VerbFilter;

use yii\helpers\Url;

use yii\web\Controller;

use yii\web\Response;


class SecurityController extends BaseSecurityController

{


    public function behaviors()

    {

        return [

            'access' => [

                'class' => AccessControl::className(),

                'only' => ['logout'],

                'rules' => [

                    [

                        'actions' => ['logout'],

                        'allow' => true,

                        'roles' => ['@'],

                    ],


                ],

            ],

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'logout' => ['post'],

                ],

            ],

        ];

    }

    public function actions()

    {

        return [

            'error' => [

                'class' => 'yii\web\ErrorAction',

            ],

            'captcha' => [

                'class' => 'yii\captcha\CaptchaAction',

               // 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,

            ],

        ];

    }





}




config




<?php


$params = require(__DIR__ . '/params.php');


$config = [

    'id' => 'basic',

    'basePath' => dirname(__DIR__),

    'bootstrap' => ['log'],

    'components' => [

        'request' => [

            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation

            'cookieValidationKey' => 'fgfghfgh65665ghgfjg',

        ],

        'cache' => [

            'class' => 'yii\caching\FileCache',

        ],

        'user' => [

            'identityClass' => 'dektrium\user\models\User',


        ],

        'errorHandler' => [

            'errorAction' => 'site/error',

        ],

        'mailer' => [

            'class' => 'yii\swiftmailer\Mailer',

            // send all mails to a file by default. You have to set

            // 'useFileTransport' to false and configure a transport

            // for the mailer to send real emails.

            'useFileTransport' => true,

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'authClientCollection' => [

            'class' => 'yii\authclient\Collection',


        ],

        'view' => [

            'theme' => [

                'pathMap' => [

                    '@dektrium/user/views' => '@app/views/user'

                ],

            ],

        ],


        'db' => require(__DIR__ . '/db.php'),

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            ],

    ],

    'modules' => [

        'user' => [

            'class' => 'dektrium\user\Module',

            'enableConfirmation' => false,

            'modelMap' => [

                'User' => 'app\models\User',

                'LoginForm' => 'app\models\LoginForm',

            ],

            'controllerMap' => [

                'security' => 'app\controllers\user\SecurityController',

            ],

            /*'modelMap' => [

                            'User' => 'app\models\User',

                            'login' => 'app\controllers\user\LoginController'

                        ],*/

        ],

        'debug' =>

            [

                'class' => 'yii\debug\Module',

            ],

    ],

    'params' => $params,

];


if (YII_ENV_DEV) {

    // configuration adjustments for 'dev' environment

    $config['bootstrap'][] = 'debug';

    $config['modules']['debug'] = [

        'class' => 'yii\debug\Module',

    ];


    $config['bootstrap'][] = 'gii';

    $config['modules']['gii'] = [

        'class' => 'yii\gii\Module',

    ];

}


return $config;




So then i go to locolhost/user/login add put data. Captha always return "The verification code is incorrect"


(Abdulhar) #2

It’s work. But if replace captchaAction’ => ‘security/captcha’, to captchaAction’ => ‘/site/captcha’. Why it’s not work with captchaAction’ => ‘security/captcha’,