0
votes

Im new with yii. Im working on a project where no guest is allowed. Whenever a user will try to access the app if user is not logged in the login page will show up. If login is successful it will redirect to home page. Iv figure out how to do user authentication through database. But if I want to redirect all guests to the login page what is the best way to do it? And also how to get the role_id of a logged in user in a controller ?

1

1 Answers

1
votes

Read this part http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#access-control-filter, Yii can handle that automatically for you.

    array('allow', // Allow all actions for logged in users ("@")
        'actions' => array(....),
        'users' => array('@'),
    ),

Means allow access to those actions in the controller only to the logged in users.

Example of the class

class MyController extends CController {
    public function filters()
    {
        return array( 'accessControl' ); // perform access control for CRUD operations
    }

    public function accessRules()
    {
        return array(
            array('allow', // allow authenticated users to access all actions
                'users'=>array('@'),
            ),
            array('deny'),
        );
    }
}

Then you can do for all your controllers (except the login one)

class SomeController extends MyController {