Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass arguments to access rules expressions in yii framework controller

Tags:

php

yii

ow to pass arguments to accessRules experessions The code below doesn't work becouse $owner_id is not defined in class where expression is evaluated. Any ideas how to fix it?

public function accessRules(){
$owner_id = $this->loadModel()->owner_id;
return array(
...
        array('allow', 
                'actions'=>array('update'),
                'expression'=>'$user->id==$owner_id',
));
}
like image 724
liysd Avatar asked Dec 29 '22 14:12

liysd


2 Answers

It's very hard to tell what you're trying to do or what the problem is, but I would use "{}" and double quotes rather than single quotes when building your array so that your variables are interpreted correctly:


public function accessRules(){
$owner_id = $this->loadModel()->owner_id;
return array(
...
        array('allow', 
                'actions'=>array('update'),
                'expression'=>"{$user->id}=={$owner_id}",
));
}


like image 77
mmattax Avatar answered Jan 13 '23 14:01

mmattax


You can use

 array('allow',
            'actions'=>array('update'),
            'users'=>array(Yii::app()->user->name),
            'expression' => '(Yii::app()->user->id == ($_GET[\'id\']))',
                    ),
like image 28
Abhinav Avatar answered Jan 13 '23 14:01

Abhinav