0

I am working on an application using yii. I have an action let acmanageappointments/index. I have defined its rule as follow

array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('index','create','update','delete','updatestatus'),
                'users'=>array('@'),

and its action is as follow :

public function actionIndex()
    {

        $user_id = Yii::app()->user->getId();   
        $criteria = new CDbCriteria();
        $criteria->condition = 'user_id='.$user_id;
        $count=AcAppointments::model()->count($criteria);
        $pages=new CPagination($count);

        //results per page 
        $pages->pageSize=10;
        $pages->applyLimit($criteria);
        $AllAppointments = AcAppointments::model()->findAll($criteria);

        // Applying Global Date Time Format 
        $condition = array('user_id' => $user_id);
        $DTFormat = CalendarSettings::model()->findByAttributes($condition);

        $this->render('index',array(
                'AllAppointments' => $AllAppointments,
                'pages' => $pages,
                'DTFormat' => $DTFormat,
        ));


    }

This action can only be accessed with authenticated persons. when I am logged in then this function is working properly. but when I am logged out and executing this action then it gives CDbException. How can I handle this exception, and when the user is logged out and if he is trying to access this url then he should be redirected on login page . How can I do this ?

update : Here is my accessrules :

public function accessRules()
    {
        return array(
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('index','create','update','delete','updatestatus'),
                'users'=>array('@'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

and here is the error :

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105

4 Answers4

1

You need to define another rule that will ensure that non-authenticated users are denied access. This must be the last rule.

array('allow', // allow authenticated user to perform 'create' and 'update' actions
    'actions'=>array('index','create','update','delete','updatestatus'),
    'users'=>array('@'),
),
array('deny',
    'users'=>array('*'),
),
topher
  • 14,790
  • 7
  • 54
  • 70
  • thnx topher, I have done that also, now I am getting the same problem – Rohitashv Singhal Feb 16 '13 at 11:34
  • while surfing I seen that I have to remove yii_debug from index.php, whats taht exactly – Rohitashv Singhal Feb 16 '13 at 11:35
  • 1
    Could you post your `accessRules` method? – topher Feb 16 '13 at 11:36
  • public function accessRules() { return array( array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=>array('view','admin'), 'users'=>array('admin'), ), array('allow', // allow authenticated user to perform 'create' and 'update' actions 'actions'=>array('index','create','update','delete','updatestatus'), 'users'=>array('@'), ), array('deny', // deny all users 'users'=>array('*'), ), ); } – Rohitashv Singhal Feb 16 '13 at 11:39
  • It's hard to read in its current format. Update the question with this and your CDbException error message – topher Feb 16 '13 at 11:47
  • ok. The `accessrules()` seem to be fine. Do you have a `filters` method? Also what is the browser url when the Exception is thrown? – topher Feb 16 '13 at 12:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24618/discussion-between-topher-and-lord-linus) – topher Feb 16 '13 at 12:16
1

As topher mentioned in comments, you need a filters method.

Make sure you have this in your controller, else your access rules will do nothing:

public function filters()
{
    return array(
        'accessControl', 
    );
}

If it works, give his answer credit when he updates it with this snippet.

Willem Renzema
  • 5,177
  • 1
  • 17
  • 24
1

You can have an errorHandler setting in your config file "main.php"

'components'=>array(
    ...............
    ...............
    'errorHandler'=>array(
    // use 'site/error' action to display errors
    'errorAction'=>'site/error',
    ),
    ...............
    ...............
)

this will redirect all the exceptions to the provided URL site/error in this case.

0

Check value $user_id. if you are not logged in, you get empty $user_id

$user_id = Yii::app()->user->getId();   
$criteria = new CDbCriteria();
$criteria->condition = 'user_id='.$user_id;

When you execute with this criteria you got SQL error

Sergey
  • 5,208
  • 25
  • 36