Yii Framework Forum: Access rule problem - Yii Framework Forum

Jump to content

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

Access rule problem Rate Topic: -----

#1 User is offline   karim gioca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 21-September 10

Posted 27 July 2011 - 07:47 AM

Hi everyone,
i want to make some dynamic things to accessrules function in yii's controllers :
i want to give access to dynamic list of users to specific views, this list is imported from database.
I give an example (in controller) :
public function accessRules()
	{
            $users=array();
            $usersFromDB= Account::model()->findAll();
            foreach ($usersFromDB as $key=>$val)
            {
                    $users[]=$val->name;
            }
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array(),
				'users'=>array('*'),
			),
			
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','deleteLogo'),
				'users'=>$users,
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}


But nothing works !
i thought that when i give a list of names to access rules it will take this list, but nothing happened.
P.S : controllers are generated with Gii
0

#2 User is offline   Layne 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-May 11
  • Location:Ukraine, Kiev

Posted 27 July 2011 - 09:42 AM

At first, don't write this code in accessRules method.
Create a dynamic property in controller, as example:
public function getusers(){
            $usersFromDB= Account::model()->findAll();
            foreach ($usersFromDB as $val)
            {
                    $users[]=$val->name;
            }
            retutn $users;
}

After that your method accessRules will work:
public function accessRules()
        {
            return array(
                        array('allow',  // allow all users to perform 'index' and 'view' actions
                                'actions'=>array(),
                                'users'=>array('*'),
                        ),
                        
                        array('allow', // allow admin user to perform 'admin' and 'delete' actions
                                'actions'=>array('admin','delete','deleteLogo'),
                                'users'=>$this->users,
                        ),
                        array('deny',  // deny all users
                                'users'=>array('*'),
                        ),
                );
        }

But if you do this to allow or deny ALL users to perform some actions, the best way to do this is 'users'=>array('*'), in your acces rule.
0

#3 User is offline   elbek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 134
  • Joined: 22-July 10
  • Location:MA, USA

Posted 27 July 2011 - 10:16 AM

the code
 array('allow',  // allow all users to perform 'index' and 'view' actions
                                'actions'=>array(),
                                'users'=>array('*'),
                        ),


means that all user have access to all action.

That is maybe problem.
Elbek
0

#4 User is offline   Layne 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-May 11
  • Location:Ukraine, Kiev

Posted 27 July 2011 - 10:53 AM

Oh, and I overlooked this bug in karim gioca's code ;)
That's a really bug)
0

#5 User is offline   haseeb2k9 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 19-November 12
  • Location:Pakistan

Posted 02 January 2013 - 07:03 AM

View Postkarim gioca, on 27 July 2011 - 07:47 AM, said:

Hi everyone,
i want to make some dynamic things to accessrules function in yii's controllers :
i want to give access to dynamic list of users to specific views, this list is imported from database.
I give an example (in controller) :
public function accessRules()
	{
            $users=array();
            $usersFromDB= Account::model()->findAll();
            foreach ($usersFromDB as $key=>$val)
            {
                    $users[]=$val->name;
            }
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array(),
				'users'=>array('*'),
			),
			
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','deleteLogo'),
				'users'=>$users,
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}


But nothing works !
i thought that when i give a list of names to access rules it will take this list, but nothing happened.
P.S : controllers are generated with Gii


you can use this for importing record from database

	public function accessRules()
	{
		$models = User::model()->findAll(array('order' => 'user_id'));
		$list = CHtml::listData($models,'user_id', 'user_name');

		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>$list,
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

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