首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用cakePHP写数据库

问用cakePHP写数据库
EN

Stack Overflow用户
提问于 2015-07-28 15:29:21
回答 1查看 58关注 0票数 1

我是cakePHP的新手,刚开始在我的新工作中使用它。我在Orders中创建了一个edit_company操作。我更新了acos表以允许此操作。现在的问题是,我不能访问任何类型的“编辑”操作。当我试图访问任何写入或更新数据库的操作时,它都会说“您无权访问该位置”。编辑,edit_products,edit_shipping等.

视图操作工作得很好。

以前没发生过这种事。

下面是一些代码:

代码语言:javascript
复制
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");
                }
        }

    }

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-28 16:34:53

您只授予非身份验证用户访问两个操作的权限:

代码语言:javascript
复制
    public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('permissions','gen_acos');   
    }

添加新操作或在添加操作之前登录用户:

授予未通过身份验证的用户对新操作的权限:

代码语言:javascript
复制
public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('permissions','gen_acos','edit_products','edit','cancel','create_shipping_label','edit_shipping');   
    }

如果您不想在尝试访问这些操作之前授予非身份验证用户对这些操作的访问权限,请登录。

您可以查看有关Auth 这里的更多信息。

还可以查看作为博客教程一部分的这示例。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31680838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档