0

I have following code in my controller to delete files:

 public function actionDelete($id)
   {
       $current_user_id=Yii::app()->user->id;
       $condition = 'user_id=:user_id';
       $params = array(':user_id' => $current_user_id);
       $idExists = UserGroup::model()->exists($condition,$params);
       if($idExists){
           $list = UserGroup::model()->find($current_user_id);
           $getgroup= $list->user_group_id;
           $getgroupright=UserRights::model()->find($getgroup);
           $getuserRule=$getgroupright->user_rule_id;
           $getuserprivilege=$getgroupright->user_privilege_id;
           if($getuserprivilege=='1' and $getuserRule=='3'){
                $this->loadModel($id)->delete();
                // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
                if (!isset($_GET['ajax']))
                    $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

           }
             }

        }

Thid code if($getuserprivilege=='1' and $getuserRule=='3'){works for viewing files by id(100%). However, it did not word for deleting files. If I remove this code, it starts working. How can I fix this error?

phpdev
  • 511
  • 4
  • 22

2 Answers2

0

First add the below code in your controller at top and check if the request is POST or GET

if(Yii::app()->request->isPostRequest()) 
   echo "POST"; 
else 
   echo "NOT POST";

if the output is "NOT POST" then

Check filters() in your controller and see if the deletion is allowed via POST request only.

public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
}

'postOnly + delete', -> comment this line.

to allow deletion via GET request.

Alternate (more secured and better) solution is that you use POST method to pass the 'id' to the controller.

-1

Try replacing

$this->loadModel($id)->delete();

with

$this->findModel($id)->delete();

Of course you need a findModel($id) method in the controller

meaulnes
  • 303
  • 2
  • 13