Yii Framework Forum: Expression Access Control In 2.0 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Expression Access Control In 2.0 How to use expression in 2.0

#1 User is offline   dannythebestguy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 27-June 12

Posted 11 November 2013 - 01:45 PM

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

Thnx

Dinesh
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 475
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 12 November 2013 - 06:36 AM

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

#3 User is offline   dannythebestguy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 27-June 12

Posted 12 November 2013 - 11:56 AM

View PostCeBe, on 12 November 2013 - 06:36 AM, said:

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

Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: yii\web\AccessRule::expression


Thnx

Dinesh
0

#4 User is offline   dannythebestguy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 27-June 12

Posted 12 November 2013 - 05:29 PM

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
0

#5 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,334
  • Joined: 17-January 09
  • Location:Russia

Posted 13 November 2013 - 08:54 AM

It is correct.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users