Access Rules Issue

Hi all,

I’ve been using Yii some months and it it awesome, however I am having trouble implementing Access Rules in my controllers but weirdly, only in Production.

To begin with, inside Components i declared a class UserApplications, where im going to see if a user has or does not have permission to go to a certain page.

One method inside this class is for example:


class UserApplications extends CApplicationComponent{


    public function userEmail($id){

        

        $included = ApplicationUser::model()->find('id_app=:id_app AND id_user=:id_user', array('id_app' => 5, 'id_util' => $id));


        if (isset($included)){

            return TRUE;

        }else{

            return FALSE;

        }

        

    }

}

This way, I see if the user can access the Email application. If the record exists, it returns TRUE, otherwise, FALSE.

Then in my config->main.php I have:


...

'components'=>array(

    'user_apps'=>array('class'=>'UserApplications'),

...

Finally, in my Email controller, I have something like:


....

	public function accessRules()

	{   

            return array(

                array('allow',

                        'actions'=>array('index', 'logout'),

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

                        'expression'=>'Yii::app()->user_apps->userEmail('.Yii::app()->user->getId().')==TRUE',

                ),            

                array('deny',

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

                        'deniedCallback' => function() {

                            $this->redirect(array('/site/index'));                            

                        },

                ),

            );

	}

....

This works very good in development. I click the link, and if I am not allowed to go in the application, I get redirected to the website’s index.

However, in production, all I get is a HTTP 500 Internal Server Error. It does not help to enable


defined('YII_DEBUG') or define('YII_DEBUG',true);

in the index.php file as I still get the 500 error an no error message at all.

Like I said, in development, everything is fine.

Any ideas why this has this behaviour? Does the access rules internals rely in specific PHP functions that only work on a determined version of PHP itself and upwards? Yii’s requirements only say 5.1 and up, and my deployment server is 5.2.17 (old, I know, they’re working on it)

Any tips are appreciated.

Best regards,

SilverPT

Anonymous functions only became available in PHP 5.3.

Hum, thank you Keith :)

Now I’m doing


            return array(

                array('allow',

                        'actions'=>array('index', 'logout'),

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

                        'expression'=>'Yii::app()->user_apps->userEmail('.Yii::app()->user->getId().')==TRUE',

                ),            

                array('deny',  // deny all users

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

                        'deniedCallback' => $this->redirecting(),

                ),

            );

	}

        

        public function redirecting(){

            $this->redirect(array('/site/index'));

        }



However, now I’m always redirected to the site/index, even when the user has permission to access that application, even in localhost.

Any thoughts?

Regards

Try this:




            return array(

                array('allow',

                        'actions'=>array('index', 'logout'),

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

                        'expression'=>'Yii::app()->user_apps->userEmail('.Yii::app()->user->getId().')==TRUE',

                ),            

                array('deny',  // deny all users

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

                        'deniedCallback' => array($this, 'redirecting'),

                ),

            );

        }

        

        public function redirecting(){

            $this->redirect(array('/site/index'));

        }



If that doesn’t work, the following should:




            return array(

                array('allow',

                        'actions'=>array('index', 'logout'),

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

                        'expression'=>'Yii::app()->user_apps->userEmail('.Yii::app()->user->getId().')==TRUE',

                ),            

                array('deny',  // deny all users

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

                        'deniedCallback' => array('EmailController', 'redirecting'),

                ),

            );

        }

        

        public static function redirecting(){

            Yii::app()->request->redirect(array('/site/index'));

        }



This first option worked.

I didn’t realize they way we redirected could influence the outcome so much.

Thank you very much Keith, for the solution and for answering so quickly :)

Regards,

SilverPT