Authorization Problem When Call A New Action

Hi everyone,

I’m new to Yii, and I need to add an action inside a controller. I’m working on a project already started by other people, and I’ve seen there are some custom actions but when I’m going to add a new one, I always get to a page telling me I’m not authorized to access the view. I’m tryin’ an action called “actionTest” where I’m just rendering to another view. To call the action I’m typing the address 172.16.102.200/index.php/plc/test. Inside the controller PlcController there’s another custom action that works but I didn’t notice any difference of implementation ( I’ve also tried to copy the code of the other action ). I’ve also checked the access rules and the code inside is a simple “return array();”. Is there another place where I need to specify authorization rules for calling the new action?

I’m still searching some information in the forum but nothing has solved my problem yet :(

Stev

In your controller file there is a function named as " public function accessRules()", please add your action in that. If nothing is written inside it then please tell me your controller extends which class??( written on first line).

Thank you for your reply :)

In the function accessRules() there’s only a row: return array();

I’ve tried to add an access rule for the new action but nothing changed. The other custom action inside the same controller works correctly.

The controller extends the class Controller.

Please make sure that you have not used any predefined function name which is not valid.

Or if you do not have any problem then please put your code here.

It’s not a predefined function name. Also tried changin’ it. So here’s some code.

This is the accessRules() function:




public function accessRules()

{

        return array();

}



My new action nuovaAzione:




public function actionNuovaAzione(){

        $this->render('done');

}



And also the working custom action aggiornaOrario:




public function actionAggiornaOrario(){

        $modelInsComando=new Wscommands();

        $modelInsComando->id_prog=time()+10;

        $modelInsComando->n_plc=0;

        $modelInsComando->command="richAggOraPLC";

        $modelInsComando->p1=0;

        $modelInsComando->p2=0;

        $modelInsComando->p3=0;

        $modelInsComando->p4=0;

        $modelInsComando->p5=0;

        $modelInsComando->p6=0;

        $modelInsComando->p7=0;

        $modelInsComando->p8=0;

        $modelInsComando->p9=0;

        $modelInsComando->p10=0;

        $modelInsComando->p11=0;

        $modelInsComando->p12=0;

        $modelInsComando->p13=0;

        $modelInsComando->p14=0;

        $modelInsComando->p15=0;

        $modelInsComando->save();

	$this->render('done');

}

in your controller class override the accessRules() method like this:




    class YourController extends Controller

    {

        public function accessRules()

        {

            return array(

                array('allow',

                    'actions'=>array('index','view','nuovaAzione','aggiornaOrario'),

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

                ),

                array('allow'

                    'actions'=>array('create','update','delete','admin'),

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

                ),

            );

        }

    

        // othe code

    }



If you want to limit NuovaAzione and AggiornaOrario actions to registered users, the accessRules() should like this:




    class YourController extends Controller

    {

        public function accessRules()

        {

            return array(

                array('allow',

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

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

                ),

                array('allow'

                    'actions'=>array('create','update','delete','admin','nuovaAzione','aggiornaOrario'),

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

                ),

            );

        }

    

        // othe code

    }