CakePHP - 禁止在脚手架中添加操作

CakePHP - Disallow add action in scaffolding

我想禁止未经许可的用户从某个模型中添加和删除记录,但仍允许他们查看和编辑记录。

我知道如何禁用 "add" 和 "delete" 操作,但在脚手架中,仍然会有标记为 "New X" 和 "Delete X"[=12= 的按钮]

是的,我知道这个问题的一个可能的解决方案是 "don't use scaffolding, bake the views." 但我想知道:单独使用脚手架是否可行?

我通过根据用户权限修改 $this->paginate$this->Model->hasAndBelongsToMany 来使用脚手架做了一些非常巧妙的技巧,我以前的工作让我认为这可能是可能的,但是 Internet 和文档搜索让我无处可去。

一种方法是 customize scaffolding Views。您可以从 cake core Cake\View\Scaffolds 获取这些模板所用的源代码,并将它们放在文档中指定的位置,然后编辑它们以隐藏您想要的内容。

例如: 在 index.ctp 中有以下生成操作列表的片段:

echo '<td class="actions">';
        echo $this->Html->link(__d('cake', 'View'), array('action' => 'view', ${$singularVar}[$modelClass][$primaryKey]));
        echo ' ' . $this->Html->link(__d('cake', 'Edit'), array('action' => 'edit', ${$singularVar}[$modelClass][$primaryKey])); 

从中删除编辑或您要修改的任何其他内容。

您也可以使用,确实应该使用控制器的 beforeScaffold 回调方法,如下所示:

class MyController extends AppController{
public function beforeScaffold(){
  parent::beforeScaffold();
  if ($this->request->params['action'] == 'edit'){
    $this->redirect('/?msg=notallowed');
  }

}

}