Yii Authorized action - yii

$criteria = new CDbCriteria;
$criteria->addCondition(array('where' => 'book_id = ' . $id));
$dataProvider = new CActiveDataProvider('Copy', array(
'criteria' => $criteria
));
$this->render('specificCopy', array(
'dataProvider' => $dataProvider,
));
//------//
array('allow',
'actions' => array('specificCopy'),
'users' => array('*'),
),
like this ?
or something wrong ?
by these code, i want to retrieve data from 'Copy' mdoel with some criteria as shown.
but, when i render it to view (specificCopy), my apps show this error :
Error 403
You are not authorized to perform this action.
can anybody help me ?

in your controllerAdd 'specificCopy' in rules
public function accessRules()
{
return array(
array('allow',
'actions'=>array('index','view','specificCopy'),
'users'=>array('*'),
),
array('allow',
'actions'=>array('create','update','captcha','specificCopy'),
'users'=>array('#'),
),
array('allow',
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}

Related

form goes blank on use of TbExtendedGridView

I am new to yii1. In my project ,I have used TblExtendedGridView to display data in the table .The form shows the data in my local computer.But when the project is uloaded in server,the file is blank and doesnot show any error.
What is the problem?
'<?php
$uniqid=md5(uniqid());
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'marketing-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'type' => 'striped bordered',
'type' => 'striped bordered condensed',
'columns'=>array(
array(
'header'=>'#',
'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
array(
'name'=>'client_id',
'header'=>'Company',
'value'=>'$data->clientName->client_name',
'htmlOptions' => array('style'=>'width:200px'),
),
array(
'name'=>'client_contact_id',
'header'=>'Contacted',
'value'=>'$data->contactPerson->contact_name',
'htmlOptions' => array('style'=>'width:180px'),
),
array(
'name'=>'visited_date',
'header'=>'Visit Date',
'htmlOptions' => array('style'=>'width:100px'),
),
array(
'name'=>'possibility',
'header'=>'Probability',
'htmlOptions' => array('style'=>'width:100px'),
),
'remarks',
array(
'name'=>'next_visited_date',
'header'=>'Next Contact Date',
'htmlOptions' => array('style'=>'width:100px'),
),
array(
'name'=>'follow_up_by',
'header'=>'Follow Up By',
'value'=>'$data->followPerson->user_name',
'htmlOptions' => array('style'=>'width:180px'),
),
),
),
),
)); ?>'
'My controller is:
<?php
class MarketingController extends Controller
{
public $layout='//layouts/column1';
public function actionIndex()
{
$this->actionAdmin();
}
// Uncomment the following methods and override them if needed
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','DynamicContact'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('admin','delete','create','update','DynamicContact'),
'users'=>array('#'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete','DynamicContact'),
'users'=>array('admin','#'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='marketing-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
public function actionView($id)
{
EQuickDlgs::render('view',array('model'=>$this->loadModel($id)));
}
public function actionAdmin()
{
$model=new Marketing('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Marketing']))
$model->attributes=$_GET['Marketing'];
$this->render('admin',array(
'model'=>$model,
));
}
public function actionCreate()
{
$model=new Marketing;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Marketing']))
{
$model->attributes=$_POST['Marketing'];
//print_r($_POST['User']);
//die;
if($model->save())
{
EQuickDlgs::checkDialogJsScript();
$this->redirect(array('marketing/admin','id'=>$model->marketing_id));
}
}
EQuickDlgs::render('create',array(
'model'=>$model,
));
}
public function loadModel($id)
{
$model=Marketing::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Marketing']))
{
$model->attributes=$_POST['Marketing'];
if($model->save())
{
//$this->redirect(array('view','id'=>$model->user_id));
EQuickDlgs::checkDialogJsScript();
$this->redirect(array('marketing/admin','id'=>$model->marketing_id));
}
}
EQuickDlgs::render('update',array(
'model'=>$model,
));
}
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
}'
'And Model is :
<?php
class Marketing extends PMActiveRecord
{
/**
* #return string the associated database table name
*/
public function tableName()
{
return 'pm_mar_marketing';
}
public function rules()
{
return array(
array('client_id, client_contact_id, follow_up_by, visited_date, visit_type, next_visited_date, possibility, remarks', 'required'),
array('client_id, client_contact_id, follow_up_by, visit_type, crtd_by, updt_by, updt_cnt', 'numerical', 'integerOnly'=>true),
array('possibility', 'length', 'max'=>20),
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('marketing_id, client_id, client_contact_id, follow_up_by, visited_date, visit_type, next_visited_date, possibility, remarks', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'clientName' => array(self::BELONGS_TO, 'Client', 'client_id'),
'contactPerson' => array(self::BELONGS_TO, 'ClientContact', 'client_contact_id'),
'followPerson' => array(self::BELONGS_TO, 'User', 'follow_up_by'),
'visitType' => array(self::BELONGS_TO, 'CodeValue', 'visit_type')
);
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'marketing_id' => 'Marketing',
'client_id' => 'Client',
// 'client_id'=> 'Client',
'client_contact_id' => 'Contact Person',
//'client_contact_id' => 'Contact Person',
'follow_up_by' => 'Follow Up By',
'visited_date' => 'Visited Date',
'visitType.code_lbl' => 'Visit Type',
'next_visited_date' => 'Next Contact Date',
'possibility' => 'Probability',
'remarks' => 'Remarks',
'crtd_by' => 'Crtd By',
'crtd_dt' => 'Crtd Dt',
'updt_by' => 'Updt By',
'updt_dt' => 'Updt Dt',
'updt_cnt' => 'Updt Cnt',
);
}
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('clientName','contactPerson','followPerson');
$criteria->compare('marketing_id',$this->marketing_id);
$criteria->compare('clientName.client_name',$this->client_id,true);
$criteria->compare('contactPerson.contact_name',$this->client_contact_id,true);
$criteria->compare('followPerson.user_name',$this->follow_up_by,true);
$criteria->compare('visited_date',$this->visited_date,true);
//$criteria->compare('visit_type',$this->visit_type);
$criteria->compare('next_visited_date',$this->next_visited_date,true);
$criteria->compare('possibility',$this->possibility,true);
$criteria->compare('remarks',$this->remarks,true);
$criteria->compare('crtd_by',$this->crtd_by);
$criteria->compare('crtd_dt',$this->crtd_dt,true);
$criteria->compare('updt_by',$this->updt_by);
$criteria->compare('updt_dt',$this->updt_dt,true);
$criteria->compare('updt_cnt',$this->updt_cnt);
//$criteria -> join = 'INNER JOIN pm_marketing_user followPerson on t.follow_up_by= followPerson.user_id';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
'
If This happend only whit this view check for the lowercase/uppercase of the class code file (Marketing.php i guess)
If your development enviroment is windows and your produdction enviroment is Unix like, can happen yuo have marketing.php in your model and you referer to Marketing.php or to the contrary this work in windows/dos because in case insentisitve but not in Unix like O.S.

Yii cgridview cactivedataprovider doesnt work properly

I configured the Yii cactivedataprovider as the documentation writes:
$criteria = new CDbCriteria();
$criteria->together = true;
$criteria->with = array(
'relationId0',
'relationId1',
...
);
$criteria->compare('"relationId0".property0', $this->relationId0_property0, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => array(
'attributes' => array(
...
'relationId0.property0' => array(
'asc' => '"relationId0".property0',
'desc' => '"relationId0".property0 DESC',
),
...
)
)
));
so, when the ->together is false, then the gridview works properly and gets all rows what the pagination allowed, but in this case the compare (so the search) doesnt work (because this way doesnt use the related objects in the sql query),
but when ->together is true (and it is the solution supposedly) the compare is working but the gridview gets random number of rows in each page.
Thank you for helping.
Try to add pagination into your dataprovider.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination'=>array('pageSize'=>10),
'sort' => array(
'attributes' => array(
...
'relationId0.property0' => array(
'asc' => '"relationId0".property0',
'desc' => '"relationId0".property0 DESC',
),
...
)
)
));
This should solve your problem.

filter grideview work with having condition in search function in criteria?

i have a grideview by this code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'lecture-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header'=>'name',
'type' => 'raw',
// 'name'=>'name',
'value' => 'CHtml::link($data->name,Yii::app()->baseUrl . "/uploads/" . $data->name)',
),
array(
'header'=>'pages',
'value'=>'$data->slide_num'
),
array(
'header'=>'type',
'value'=>'$data->type'
),
array(
'header'=>'Size',
'value'=>'$data->size'
),
// 'subject.name'
array (
'header'=>'subject',
'value' => 'ucfirst($data->subject->name)',
'filter' => CHtml::dropDownList('Lecture[subject_id]',
$model->subject_id, Chtml::ListData(Subject::model()->findAll('department_id='.$department_id),'id','name'),
array('empty' => '(Select)'))
)
/* array(
'name'=>'subject',
'type'=>'raw',
'value'=>"Subject::model()->find('id=2')->name",
'filter'=>''
),*/
),
));
and in model the search function is
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('slide_num',$this->slide_num,true);
$criteria->compare('type',$this->type,true);
$criteria->compare('size',$this->size,true);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('subject_id',$this->subject_id);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
/*return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));*/
}
every thing is ok my target is filter that works in this search function correctly but i want to have a condition in criteria
$criteria->condition='subject_id=1 or subject_id=3';
when i put the condition in criteria its filter don't work any solution for having condition in search function and its filter work too
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('slide_num',$this->slide_num,true);
$criteria->compare('type',$this->type,true);
$criteria->compare('size',$this->size,true);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('subject_id',$this->subject_id);
$criteria->condition='subject_id=1 or subject_id=3';
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
/*return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));*/
}
Instead of $criteria->condition='subject_id=1 or subject_id=3';
try
$criteria->addCondition("subject_id = 1");
$criteria->addCondition("subject_id = 2", "OR");
OR JUST
$criteria->addInCondition("subject_id", array("1","2"));

Redirecting to login page to unauthorized user yii

access rules of pages controller:
public function accessRules()
{
$isadmin = User::loadUser(Yii::app()->user->id)->adminUser;
return array(
array('allow',
'actions'=>array('index','view','create','update'),
'expression'=>"{$isadmin}==1",
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
main config:
'user' => array( // Webuser for the admin area (admin)
'class' => 'WebUser',
'allowAutoLogin' => true,
'loginUrl' => array('/user/login'),
'stateKeyPrefix' => 'admin_',
),
Problem:
When I visit pages/create, it not redirected to user/login but throws the exception:
Error 404
The requested page does not exist.
How Can I redirect to login pages for unauthorized user?
You can use 'deniedCallback' to do this.
Method 1
'deniedCallback' => function() {Yii::app()->controller->redirect(array ('actionName'));},
//It will come As
array('allow',
'actions' => array('actionName1,actionName2,actionName3'),
'deniedCallback' => function() {Yii::app()->controller->redirect(array ('actionName'));},
'users' => array('#'),
),
Method 2:
You can also do the same by calling a function, see bellow code.
array('allow',
'deniedCallback' => array($this, 'goToLogin'),
'actions' => array('actionName1,actionName2,actionName3'),
'users' => array('#'),
),
Code for goToLogin method
public function goToLogin()
{
$this->redirect('/controller/actionName');
//For your program
//$this->redirect('/site/login');
}
Yii::app()->user->loginRequired();
add this funtion in every controller.
public function beforeAction($action) {
if (Yii::app()->user->isGuest && Yii::app()->controller->action->id != "login") {
Yii::app()->user->loginRequired();
}
//something code right here if user valid
return true;
}
site controller only needs Yii::app()->controller->action->id != "login" this condition. remove it for another controllers

CGridView filter ajax update is not happening

Im using CGridView to display records. Filter is not working properly in my application. Instead of Ajax update the complete page gets reloaded. I want to filter records using ajax call. Here is my code.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mage-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'ajaxUpdate'=>true,
'columns'=>array(
'entity_id',
'name',
'sku',
'type_id',
'price',
//'status',
array(
'name'=>'status',
'header'=>'Status',
'filter'=>array('1'=>'Enabled','2'=>'Disabled'),
'value'=>'($data->status=="1")?("Enabled"):("Disabled")'
),
array(
'class'=>'CButtonColumn',
'template' => '{update}{delete}',
// 'viewButtonUrl'=>'Yii::app()->controller->createUrl("/ad/view",array("id"=>$data["id"]))',
'buttons' =>array(
'update' => array(
'url'=>'Yii::app()->controller->createUrl("/mageproduct/update",array("pid"=>$data["entity_id"],"sid"=>SHOP_ID))',
'imageUrl'=>Yii::app()->request->baseUrl.'/images/icons/dark/create_write.png',
),
'delete' => array(
'url'=>'Yii::app()->controller->createUrl("/mageproduct/delete",array("pid"=>$data["entity_id"],"sid"=>SHOP_ID))',
'imageUrl'=>Yii::app()->request->baseUrl.'/images/icons/dark/trashcan.png',
// 'deleteConfirmation' => 'Delete?',
)
),
),
),
Here is my controller action
public function actionAdmin()
{
$model=new Mageproduct('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Mageproduct']))
$model->attributes=$_GET['Mageproduct'];
$this->render('admin' ,array(
'model' =>$model,
));
}
Note:
Im using CArrayDataProvider.