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
2
  • What other access rules have you defined? Commented Feb 16, 2013 at 11:30
  • no i havent defined any other rule Commented Feb 16, 2013 at 11:30

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

THANKS ankur ji bt, the thing which I was forgetting is the one which Willem Renzema has explained ...
0

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('*'),
),

7 Comments

thnx topher, I have done that also, now I am getting the same problem
while surfing I seen that I have to remove yii_debug from index.php, whats taht exactly
Could you post your accessRules method?
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('*'), ), ); }
It's hard to read in its current format. Update the question with this and your CDbException error message
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.