我試圖暗示一些動態斷言到我的Zend代碼中,並且一直在使用[Ralph Schindler] [1]的一篇文章,但我無法讓它工作。我想要做的是在de Acl中制定一個「允許」規則,檢查登錄的人是否實際上是一段UserContent的所有者。Zend_Acl和一個動態Assert
我有一個用戶類和UserContent類(刪除所有不必要的位):
class User implements Zend_Acl_Role_Interface {
private $_roleId;
public function getRoleId() { return $this->_roleId; }
}
class UserContent implements Zend_Acl_Resource_Interface {
private $_resourceId;
private $_userId;
public function getResourceId() { return $this->_resourceId; }
public function getUserId() { return $this->_userId; }
}
現在在我的ACL類My_Acl我已經定義了一個「成員」的角色,一個「usercontent」資源一個「編輯」特權,並想創建以下允許規則:
$this->allow('member', 'usercontent', 'edit', new My_Acl_Assert_IsOwner());
其中斷言實現了類Zend_Acl_Assert_Interface:
class My_Acl_Assert_IsOwner implements Zend_Acl_Assert_Interface {
public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $role=null, Zend_Acl_Resource_Interface $resource=null, $privilege = null) {
[return true if the user logged in is owner of the userContent]
}
}
在哪裏我仍然掙扎在什麼放在實際的斷言方法。
比方說,我登錄爲會員(所以我的$ _roleId =「成員」),並要檢查,如果我被允許修改一段UserContent的,是這樣的:
$userContentMapper = new Application_Model_Mapper_UserContent();
$userContent = $userContentMapper->find(123);
if ($this->isAllowed($userContent, 'delete')) echo "You are allowed to delete this";
在斷言方法我想提出這樣的:
$resource->getUserId();
,但是這給了我的錯誤信息*調用未定義的方法Zend_Acl_Resource :: getUserId()*。奇怪,因爲如果我測試,如果資源是UserContent我得到一個確認的一個實例:添加以下行資產的方法:
if ($resource instanceof UserContent) echo "True!";
我得到真正的實在。出了什麼問題?
爲我增加了一個額外的公共變量OWNERID到UserContent類的測試,definied如下:
private $_id;
public $ownerId;
public function setId($id) {$this->_id = $id; $this->ownerId = $id;
現在,如果我的斷言方法添加$資源 - > OWNERID,我沒有得到任何錯誤信息,它只是讀取班級中的值。出了什麼問題? $ resource是UserContent的一個實例,但我不能調用方法getUserId,但我可以調用公共變量$ ownerId ??
[1] http://ralphschindler.com/2009/08/13/dynamic-assertions-for-zend_acl-in-zf
根據您提供的確切代碼,最可能的問題是,acl規則也會在您的應用程序中的另一個地方使用其他資源進行調用。 – Pieter 2012-03-14 03:25:15