phpyiiaccess-rules

Yii - Access Rules Issue


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 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)

Regards


Solution

  • Apparently, Anonymous functions only became available in PHP 5.3.

    The solution was:

        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'));
    }
    

    Kudos to the user Keith in the Yii Framework Forum

    Regards