RBAC(基于角色的访问控制)中的 BizRules 的想法让我感到恶心。它基本上是一种定义脚本以在授权期间针对数据运行的方法。
例如 Yii 框架支持:http ://www.yiiframework.com/doc/api/1.1/CAuthManager#createRole-detail
public CAuthItem createRole(string $name, string $description='', string $bizRule=NULL, mixed $data=NULL)
以下是执行业务规则的源代码:
/**
* Executes the specified business rule.
* @param string $bizRule the business rule to be executed.
* @param array $params parameters passed to {@link IAuthManager::checkAccess}.
* @param mixed $data additional data associated with the authorization item or assignment.
* @return boolean whether the business rule returns true.
* If the business rule is empty, it will still return true.
*/
public function executeBizRule($bizRule,$params,$data)
{
return $bizRule==='' || $bizRule===null || ($this->showErrors ? eval($bizRule)!=0 : @eval($bizRule)!=0);
}
因此,您可以执行以下操作:
// Assume this bizRule: $bizRule='return Yii::app()->user->id==$params["post"]->authID;';
Yii::app()->user->checkAccess('createUser', array('post' => $post));
它基本上评估了在其上下文中将 $params 设置为给定数组的 bizRule。
在安全性方面,我不喜欢那些业务规则。有更好的方法吗?