【发布时间】:2011-09-24 17:56:52
【问题描述】:
我正在尝试研究保护用户数据的最佳方式。 示例:一个应用程序有一个表“widgets”,每个用户可以拥有任意数量的“widgets”。应用程序通过“userId”列识别“小部件”,该列引用了登录用户的 ID。
目前,如果通过在我的模型中用我自己的方法覆盖 fetchAll() 方法,并在将参数传递给 parent::fetchAll 之前添加 WHERE userId = X,那么目前我能够保护小部件数据不被访问的最佳方法() 像这样:
class Model_Widgets extends Zend_Db_Table_Abstract {
protected $_name = 'widgets';
/**
* Abstracted function to ensure data security
* Adds in a WHERE to the SELECT to check if this user is the datas owner
*
* @see Zend_Db_Table_Abstract::fetchAll()
*/
public function fetchAll($where = null, $order = null, $count = null, $offset = null)
{
// Handle the additional security check
$userId = 'userId = ' . Model_Users::getUser()->id;
// Merge the WHERE userId statement with the rest
if($where)
{
if(is_array($where))
$where[] = $userId;
else
$where = array($where, $userId);
}
else
$where = $userId;
return parent::fetchAll($where, $order, $count, $offset);
}
这个方法很好用,但我不禁想到一定有更好的方法,我最近发现了 $_rowClass 但仍然不确定我是否理解这个概念。如果覆盖具体函数是应用这些安全检查的唯一方法,有没有办法覆盖它们一次,而不是通过帮助器在每个模型中覆盖它们,然后简单地将如下函数添加到需要检查用户的每个模型中反对行:
public function fetchAll(...)
{
return SecurityCheckHelper::fetchAll(...);
我希望这是有道理的,实际上我所做的只是确保用户无法通过在 URL 等中玩弄 ID 来访问其他用户的数据。 谢谢大家
【问题讨论】:
标签: zend-framework select model zend-db zend-db-table