Expression Access Control In 2.0

Is there a different technique to use ‘expression’=> ‘\Yii::$app->user->isAdmin()’ in 2.0

Thnx

Dinesh

I what context are you here? can you elaborate on what you want to do?

I believe this is Yii2.0 Design discussion board. Have downloaded the Yii2.0 and playing with it. In Yii1.1 I have been using accessRules() function


	public function accessRules()

	{

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update','create2','manager','update2'),

                                'expression'=>'$user->isManager'

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}

And in my user class I have


public function getIsManager()

    {

        return ($this->_user && $this->_user->accessLevel <= 2);        

    }    

How do I achieve ‘expression’=>’$user->isManager’ as above in Yii2.0

As per the examples in 2.0 the access rules are in the behavior function. When I try to use it in this function I get

[b]Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: yii\web\AccessRule::expression[/b]

Thnx

Dinesh

I guess I could solve it by using ‘matchCallBack’ instead of ‘expression’

On the access value in the behaviors function I added the following




			'access' => [

				'class' => \yii\web\AccessControl::className(),

				'rules' => [

					[

						'actions' => ['index'],

						'allow' => false,

						'roles' => ['?'],

					],

					[

						'actions' => ['index', 'update','view','create','delete'],

						'allow' => true,

                                                'matchCallback' => function() {

                                                return \Yii::$app->user->identity->role <= \Yii::$app->user->identity->MANAGER;                    

                                                }

					],

				],

			],                    



In the User class I added MANAGER value




        public $ADMINISTRATOR = 1;

        public $MANAGER = 2;

        public $MEMBER = 3;



Please let me know if this is not the correct way of doing it.

Thnx

1 Like

It is correct.