Yii2 captcha image is not displaying - yii2

I want to add Captcha verification on login page. But there is an error, captcha image is not showing and when I try to view the captcha image http://xxx.yii2/site/captcha?v=5806eb0c3aa05 , below error display
The image “http://xxx.yii2/site/captcha?v=5806ce094fa84” cannot be displayed because it contains errors.
Below is my SiteController
class SiteController extends Controller {
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout','resetPassword'],
'rules' => [
[
'actions' => ['logout','resetpassword'],
'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,
],
];
}
LoginForm controller
class LoginForm extends Model {
public $username;
public $password;
public $rememberMe = true;
public $verifyCode;
private $_user = false;
const ERROR_NONE=0;
const ERROR_USERNAME_INVALID=1;
const ERROR_USERNAME_LOCKED = 3;
const ERROR_USERNAME_INACTIVE = 4;
const ERROR_PASSWORD_INVALID=2;
public $role_id;
public $salt;
public $_id;
private $_identity;
/**
* #return array the validation rules.
*/
public function rules()
{
return [
[['username', 'password'], 'required'],
['rememberMe', 'boolean'],
['username','validateMember'],
['password', 'validatePassword'],
['verifyCode', 'captcha'],
];
}
Login form view
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className()) ?>
<?= $form->field($model, 'rememberMe')->checkbox([
'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",
]) ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('<i class="fa fa-sign-in fa-fw"></i> Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>

Check this.
Note that CaptchaAction requires either GD2 extension or ImageMagick PHP extension.
See: Class yii\captcha\CaptchaAction
Another possible reason of the problem is wrong action specified. To resolve it you need to specify if manually both in model and widget.
Model:
['verifyCode', 'captcha', 'captchaAction' => 'site/captcha']
Widget:
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'captchaAction' => 'site/captcha'
]) ?>

Related

Toggle user confirmation not working

I am working on an application with user platform shown below:
Controller
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Model
public function attributeLabels()
{
return [
'user_id' => Yii::t('app', 'User ID'),
'user_login_id' => Yii::t('app', 'User Login ID'),
'user_password' => Yii::t('app', 'Password'),
'user_type' => Yii::t('app', 'User Type'),
'is_block' => Yii::t('app', 'Block Status'),
'is_confirmed' => Yii::t('app', 'Block Status'),
'confirmed_at' => Yii::t('app', 'Date Confirmed'),
'created_at' => Yii::t('app', 'Created At'),
'created_by' => Yii::t('app', 'Created By'),
'updated_at' => Yii::t('app', 'Updated At'),
'updated_by' => Yii::t('app', 'Updated By'),
'current_pass' => Yii::t('app','Current Password'),
'new_pass' => Yii::t('app','New Password'),
'retype_pass' => Yii::t('app', 'Retype Password'),
'admin_user' => Yii::t('app', 'Admin Username'),
'create_password' => Yii::t('app', 'Password'),
'confirm_password' => Yii::t('app', 'Confirm Password'),
];
}
View
<?php Pjax::begin() ?>
<div class="box box-primary">
<div class="box-body">
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => "{items}\n{pager}",
'columns' => [
'user_login_id',
'user_type',
[
'attribute' => 'created_at',
'value' => function ($data) {
return (!empty($data->created_at) ? Yii::$app->formatter->asDate($data->created_at) : ' - ');
},
],
[
'header' => Yii::t('urights', 'Confirmation'),
'value' => function ($model) {
if (!$model->is_confirmed) {
return '<div class="text-center"><span class="text-success">' . Yii::t('urights', 'Confirmed') . '</span></div>';
} else {
return Html::a(Yii::t('urights', 'Confirm'), ['confirm', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to confirm this user?'),
]);
}
},
'format' => 'raw',
],
[
'header' => Yii::t('urights', 'Block status'),
'value' => function ($model) {
if ($model->is_block) {
return Html::a(Yii::t('urights', 'Unblock'), ['block', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to unblock this user?'),
]);
} else {
return Html::a(Yii::t('urights', 'Block'), ['block', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-danger btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to block this user?'),
]);
}
},
'format' => 'raw',
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
],
],
]);
?>
</div>
</div>
<?php Pjax::end() ?>
As shown in the diagram, when I click on Confirm (Green Button), it shown disable the button and turn it to Confirmed. Then set is_confimred to 0 (zero).
Also, if I click on Block (Red Button), it should change the button caption to unblock and set is_block to 0.
However, I am not getting result, but I have the page shown below:
How do I resolve it?
You should add toggle functions for both confirm and block to the controller.
public function actionConfirm($id)
{
if(($model = User::findOne($id)) !== null) {
$model->is_confirmed = $model->is_confirmed ? false : true;
$model->update();
}
return $this->redirect(['index']);
}
public function actionBlock($id)
{
if(($model = User::findOne($id)) !== null) {
$model->is_block = $model->is_block ? false : true;
$model->update();
}
return $this->redirect(['index']);
}

Eerror in social login in Yii2: Setting unknown property: yii\web\Application::authClientCollection

When I try to access app it get error after use yii2-authclient.
I follow Facebook Authentication using Yii2 authclient
All is setup, but this error occur:
Setting unknown property: yii\web\Application::authClientCollection
My frontend/main.php is:
'components' => [
'request' => [
'csrfParam' => '_csrf-frontend',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'advanced-frontend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
// 'urlManager' => [
// 'enablePrettyUrl' => true,
// 'showScriptName' => false,
// 'rules' => [
// ],
// ],
],
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
'clientId' => '2061735130522390',
'clientSecret' => '2f302dc7358730820c091ca4444afbae',
'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
],
],
],
My site controller is:
class SiteController extends Controller
{
/**
* {#inheritdoc}
*/
public $successUrl = "success";
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* {#inheritdoc}
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'successCallback'],
],
];
}
public function successCallback($client) {
// get user data from client
$userAttributes = $client->getUserAttributes();
$user = User::find()->where(['email'=>$userAttributes['email']])->one();
if (!empty($user))
{
Yii::$app->user->login($user);
}
else{
$session = Yii::$app->session;
$session['attribute'] = $userAttributes;
$this->successUrl = Url::to(['signup']);
}
die(print_r($userAttributes));
// do some thing with user data. for example with $userAttributes['email']
}
/**
* Displays homepage.
*
* #return mixed
*/
public function actionIndex()
{
return $this->render('index');
}
/**
* Logs in a user.
*
* #return mixed
*/
.
.
.
}
login.php is:
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'rememberMe')->checkbox() ?>
<div style="color:#999;margin:1em 0">
If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>.
</div>
<div class="form-group">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
<p>OR</p>
<?= yii\authclient\widgets\AuthChoice::widget([
'baseAuthUrl' => ['site/auth']
]) ?>
<?php ActiveForm::end(); ?>
</div>
</div>
Your config is incorrect, authClientCollection config should be inside of components array - in your case it is outside of it. You should move authClientCollection element one line up and change this:
],
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
'clientId' => '2061735130522390',
'clientSecret' => '2f302dc7358730820c091ca4444afbae',
'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
],
],
],
To this:
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
'clientId' => '2061735130522390',
'clientSecret' => '2f302dc7358730820c091ca4444afbae',
'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
],
],
],
],

my export in yii2 using kartik-v/yii2-export not working

i am trying to use kartik export and its not working for me. In config file I have added following code:
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
],
],
In Composer i have added folowing code
"kartik-v/yii2-export": "#dev",
"kartik-v/yii2-mpdf":"#dev",
"kartik-v/yii2-grid": "#dev"
My view code is like this:
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use yii\bootstrap\Tabs;
use kartik\export\ExportMenu;
use yii\widgets\Pjax;
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="general-info-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php
<p>
<?= Html::a('create', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php
$gridColumns = [
'sfcl_name',
[
'attribute'=> 'org_type',
'value' => 'orgType.cv_lbl'
],
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'columns' => [
'sfcl_name',
'phone',
[
'attribute'=>'regd_dt_ad',
'format'=>['date', 'php:Y-M-d'],
'xlFormat'=>'mmm\-dd\, yyyy', // different date format
'width'=>'100px'
],
[
'attribute'=> 'org_type',
'value' => 'orgType.cv_lbl'
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
My controller code is:
My controller code to call this view : public function actionIndex()
{
$searchModel = new SfclGeneralSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
On click of export menu nothing is happening.In tutorial i saw there were options for exporting to pdf ,html,csv,json,text.such options doesnot appers in my case.Is the css not working or what?
you run "composer update" command?.
also in your web.php
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
],
],
add the property downloadAction, like this:
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
'downloadAction' => 'gridview/export/download',
]
],

Login with other fields using Auth in cakephp3

Problem regarding login with Auth in cakephp3
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Articles',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'display',
'home'
]
]);
it only allows me to use username by default, Okay If I wanted to login using email, that through I have searched and got this:
UserController.php
public function login()
{
if ($this->request->is('post'))
{
$this->Auth->config('authenticate', [
'Form' => [
'fields' => ['username' => 'email']
]
]);
$this->Auth->constructAuthenticate();
$this->request->data['email'] = $this->request->data['username'];
unset($this->request->data['username']);
$user = $this->Auth->identify();
if ($user)
{
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
I can change fields from username to email and reconstruct it but What if I wanted to login with ID field.
$this->Auth->config('authenticate', [
'Form' => [
'fields' => ['username' => 'id']
]
]);
If I change from email to id, it is not allowing me to login. Do I have to use then queries instead?
I just had the same issue and solved it like this using cakePHP 3.3
Your AppController
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'unauthorizedRedirect' => $this->referer(),
'loginRedirect' => [
'plugin' => false,
'controller' => 'Pages',
'action' => 'home',
],
]);
Your UserController (login action)
/**
* #return Response|null
*/
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error('Your username or password is incorrect.');
}
}
Your basic Users/login.ctp template
<?= $this->Form->create() ?>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
Your problem is, when you changed the 'email' field to 'id' in the below code,
`('authenticate', ['Form' => ['fields' => ['username' => 'id']]]);`
you should have changed the input field of your view 'login.ctp'.
from this: <?= $this->Form->input('email') ?>
to this: <?= $this->Form->input('id') ?>

yii2 Deleting multiple Rows in GridView using Checkbox

How can I use in GridView delete selected object,in Yii 2 Framework such as following image:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'name',
'created_at:datetime',
// ...
],
]) ?>
Add checkbox action column in gridView like
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($data) {
return ['value' => $data->id];
},
],
'id',
'name',
'created_at:datetime',
// ...
],
]) ?>
And Now Access the selected id in your controller like
class YourController extends Controller
{
public function actionHear()
{
if(isset($_REQUEST['selection']))
{
".........Your Code Hear.........."
}
}
}