Security 如何在PHP 1.3中验证非标准操作的帖子
使用CakePHP1.3 我有一个非标准动作名称的控制器-比如:Security 如何在PHP 1.3中验证非标准操作的帖子,security,cakephp,components,cakephp-1.3,Security,Cakephp,Components,Cakephp 1.3,使用CakePHP1.3 我有一个非标准动作名称的控制器-比如: class WidgetsController extends AppController { function modifyColor($id = null) { // Some code that modifies the background color of a widget } } 和一个附带的视图views/widgets/modifyColor.ctp modifyColor模板将发布到操作: echo $t
class WidgetsController extends AppController {
function modifyColor($id = null) {
// Some code that modifies the background color of a widget
}
}
和一个附带的视图views/widgets/modifyColor.ctp
modifyColor模板将发布到操作:
echo $this->Form->create('User',array('url' => array('controller' => 'widgets', 'action' => 'modifyColor')));
因为CakePHP安全组件试图验证表单,所以我在帖子中得到了404
我希望能够验证这篇文章
我能让它工作的唯一方法似乎是关闭后验证
if ($this->action == 'modifyColor') {
$this->Security->validatePost = false;
}
这似乎是一个糟糕的解决方案
如何在非标准操作上使用安全组件?
允许的行为似乎不起作用
谢谢
丹尼
回答我自己的问题
A.在CakePHP中使用任何命名操作都没有问题
B.CakePHP的一个概念性错误,与表单GET和表单POST使用相同的函数有关
在表格上,我有这样一个:
if (empty($this->data)) {
$this->data = $this->Widget->read(null, $id);
}
表单本身有如下代码:
echo $this->Form->input('id');
echo $this->formView('Current color', 'CurrentColor');
echo $this->formView('New color', 'NewColor');
echo $this->formView('New background color', 'BackgrdColor');
这很好,只是这些字段都没有出现在小部件模型中——CakePHP安全组件将其解释为一种XSRF攻击——因为它正在查找不属于该模型的字段。这就是为什么:
$this->Security->validatePost = false;
解决了这个问题
正确的解决方案只是不在控制器操作中使用模型填充$this->data,并处理POST上的字段分配:
function modcolor () {
$this->layout = 'app_ui_listview';
if (!empty($this->data)) {
$id = $this->Session->read('Auth.User.id');
$u = $this->Widget->read(null, $id);
// Assign fields from the form to the model....
}
}
问题是你正在试图传递url,你也在试图传递控制器的东西 echo$this->Form->create'Widget',array'controller'=>'widgets',action'=>'modifyColor' 或
echo$this->Form->create'Widget',array'url'=>'/widgets/modifyColor' 问题是您正在尝试传递url,并且您也在尝试传递控制器内容 echo$this->Form->create'Widget',array'controller'=>'widgets',action'=>'modifyColor' 或
echo$this->Form->create'Widget',array'url'=>'/widgets/modifyColor' 您应该使用echo$this->Form->create'Widget',array'url'=>array'controller'=>'widgets',action'=>'modifyColor';而不是echo$this->Form->create'User',array'url'=>array'controller'=>widgets',action'=>modifyColor';您应该使用echo$this->Form->create'Widget',array'url'=>array'controller'=>'widgets',action'=>'modifyColor';而不是echo$this->Form->create'User',array'url'=>array'controller'=>widgets',action'=>modifyColor';在第一个版本中,控制器键将被忽略,只有操作键会产生任何效果,第二个版本相当于问题中的代码:echo$this->Form->create'User',array'url'=>array'controller'=>widgets',action'=>modifyColor';。或者:不幸的是,您的答案不正确-url键是有效的,在CakePHP中,将url指定为数组是正常的。不管问题的状态如何,这个问题已经解决了——参见回答我自己的问题。在问题中。在第一个版本中,控制器键将被忽略,只有操作键才会生效,第二个版本相当于问题中的代码:echo$this->Form->create'User',array'url'=>array'controller'=>widgets',action'=>modifyColor';。或者:不幸的是,您的答案不正确-url键是有效的,在CakePHP中,将url指定为数组是正常的。不管问题的状态如何,这个问题已经解决了——参见回答我自己的问题。在这个问题上。