无法使用cakePHP写入数据库

时间:2015-07-28 15:29:22

标签: php sql-server database cakephp

我是CakePHP的新手,我刚刚开始将它用于我的新工作。 我在Orders Controller中创建了一个edit_company动作。我更新了acos表以允许此操作。现在问题是,我无法访问任何类型的编辑'行动。它说"您无权访问该位置"每当我尝试执行任何写入或更新数据库的操作时。 edit,edit_products,edit_shipping等......

视图操作正常。

之前没有发生这种情况。

下面是一些代码:

class OrdersController extends AppController{
        public $uses = array('Order');
    public $hideActions = array('campaign','customer','shipping','review_order','place_order','products','payment','confirmation','cancel','edit_status','edit_order_type','edit_products','edit_tax','add_product','cancel_shipping_label','track_label','view_label','reprint_label','edit_shipping','create_shipping_label');
    public $components = array('Payflow','Printer');
    public $actionMap = array(
        'create' => array('add','create','campaign','customer','shipping','review_order','place_order','payment','products'),
                'read'=> array('index', 'view', 'display','confirmation','track_label','search'),
        'update' => array('edit','cancel','edit_status','edit_order_type','edit_products','edit_company','edit_tax','add_product','cancel_shipping_label','reprint_label','edit_shipping','create_shipping_label'),
        'delete' => array('delete','back_orders_by_state')
    );
    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('permissions','gen_acos');   
    }
    public function permissions(){
        $this->Acl->allow('Admin','Controllers/Orders');
        $this->Acl->allow("Sales","Controllers/Orders",'read');
        $this->Acl->allow("Sales","Controllers/Orders",'create');
        $this->Acl->allow("Sales","Controllers/Orders",'update');
        $this->Acl->deny("Shipping","Controllers/Orders",'update');
        $this->Session->setFlash("Permissions Updated.");
        $this->redirect("/orders/");
    }
    public function edit_shipping($id){
        $sm_conditions = array();
        if(!$this->Acl->check(array('User' => array('UserID' => $this->Auth->user("UserID"))), 'Controllers/Orders','delete')){
            $sm_conditions['Restricted'] = 1;
        }
        $shipping_method_ids = $this->Order->ShippingMethod->find("list",array("conditions"=>$sm_conditions,"fields"=>array("ShippingMethodID","ShippingMethodName")));
        $order = $this->Order->read(null,$id);

        $this->set("order",$order);
        $this->set("shipping_method_ids",$shipping_method_ids);
        if($this->request->is('put')){
            if($this->Order->save($this->data,null,array("ShippingAddress","ShippingMethodID"))){
                $this->Session->setFlash("Order Shipping Updated.");
                $this->Order->Note->create();
                $this->Order->Note->save(
                    array("Note"=>array('OrderID'=>$id,"UserID"=>$this->Auth->user("UserID"),"NoteBody"=>"Order Shipping Information updated.","CreatedDate"=>date("Y-m-d H:i:s")))
                );
                $this->redirect("/orders/view/$id");
            }
        }else{
            $this->request->data = $order;
        }
    }


    public function create_shipping_label($id){
        $order = $this->Order->read(null,$id);
        $this->set("order",$order);
        if($this->request->is('put')){
            $this->Order->save(array(
                "Order"=>array(
                    "OrderID"=>$id,
                    "LabelPrinted"=>false,
                    "OrderStatusID"=>2,
                    "Notes"=>(!empty($this->data['Order']['Notes']))?$this->data['Order']['Notes']:null
                )
            ));
            $this->Session->setFlash("A new shipping label will be created momentarily.");
            $this->Order->Note->create();
            $this->Order->Note->save(
                array("Note"=>array('OrderID'=>$id,"UserID"=>$this->Auth->user("UserID"),"NoteBody"=>"New shipping label will be created. ".((!empty($this->data['Order']['Notes']))?$this->data['Order']['Notes']:null),"CreatedDate"=>date("Y-m-d H:i:s")))
            );
            $this->redirect("view/".$id);
        }else{
            $this->request->data = $order;
        }
    }
    public function cancel($id){
        $order = $this->Order->read(null,$id);
        if($this->request->is('post')){
            //Check if note given
            $this->Order->Note->data = $this->data;
            if($this->Order->Note->validates()){

                //Delete from Call table
                $this->loadModel("Call");
                $this->Call->deleteAll(array('Call.OrderID'=>$id));
                //Add a note
                $user_id = $this->Auth->user("UserID");
                $this->Order->Note->create();
                $this->Order->Note->save(
                    array("Note"=>array('OrderID'=>$id,"UserID"=>$user_id,"NoteBody"=>"Order Canceled. ","CreatedDate"=>date("Y-m-d H:i:s")))
                );
                $this->Order->Note->create();
                $this->Order->Note->save(
                    array("Note"=>array('OrderID'=>$id,"UserID"=>$user_id,"NoteBody"=>"Reason For Cancellation: ".$this->data['Note']['NoteBody'],"CreatedDate"=>date("Y-m-d H:i:s")))
                );
                //Create a refund request if payment type is in TxType (1,2,3,7,11,9)
                $txTypes = array(1,2,3,7,11,9);
                $paid = 0;
                foreach($txTypes as $txType){
                    $payments = Set::extract("/Payment[TransactionTypeID=$txType]/PaymentAmount",$order);
                    $paid += array_sum($payments);
                }
                if($paid>0){
                    $this->Order->refund($id,$paid);
                }
                //Change Status to Cancel (4) & LabelPrinted = 0
                $this->Order->save(array("Order"=>array("OrderID"=>$id,"LabelPrinted"=>0,"OrderStatusID"=>4)));
                //Update the total price
                $this->Order->updateOrderTotal($id);
                $this->Session->setFlash("Order was successfully canceled.");
                $this->redirect("/orders/view/".$id);
            }
        }
        $this->set("order",$order);
    }
    public function edit_products($id){
        $order = $this->Order->read(null,$id);
        $this->set("order",$order);
        if($this->request->is("post")){
            $error = false;
            while($error==false && ($oe=array_shift($this->request->data['OrderEntry']))){
                if(!$this->Order->OrderEntry->save(array("OrderEntry"=>$oe))){
                    $error = true;
                }
            }
            if($error==false){
                $this->Session->setFlash("Products Updated.");
                $this->Order->updateOrderTotal($id);
                $this->redirect("/orders/view/$id");
            }
        }


}
    public function edit_company () {

    }

    public function edit ($id=null) {
        $order = $this->Order->read(null,$id);
        $this->set("order",$order);
        if($this->request->is("post")){
                $error = false;
                while($error==false && ($oe=array_shift($this->request->data['OrderEntry']))){
                        if(!$this->Order->OrderEntry->save(array("OrderEntry"=>$oe))){
                                $error = true;
                        }
                }
                if($error==false){
                        $this->Session->setFlash("Products Updated.");
                        $this->Order->updateOrderTotal($id);
                        $this->redirect("/orders/view/$id");
                }
        }

    }

有人可以帮我解决这个问题吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

You are only giving non authenticaded users permission to access two actions:

    public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('permissions','gen_acos');   
    }

Add the new actions or log the user in before accesing the actions:

Giving permission to not authenticated users to your new actions:

public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('permissions','gen_acos','edit_products','edit','cancel','create_shipping_label','edit_shipping');   
    }

If you don't want to grant access to non authenticated users to these actions login before trying to access them.

You can check more about Auth here

Also check this example that is part of the Blog Tutorial