我已经实现了 Zend_Acl,它似乎正在工作。我的资源是链接:
模块名称。“::”。控制器名称。“::”。动作名称;
我在我的代码中添加了一些破坏性的东西,似乎我被重定向到通常的错误页面,但是 Acl 进来说
致命错误:在第 365 行的 F:\work\php\zendworkspace\myproject\library\Zend\Acl.php 中未捕获的异常 'Zend_Acl_Exception' 和消息 'Resource 'default::error::error' not found'
我已添加default::error::error
到资源中,但错误仍然相同。当我删除破坏整个事情的代码时,它再次起作用。所以当我的代码出现问题时,我肯定会遇到同样的错误。
我想知道如何解决这个问题。感谢您阅读和分享您的经验。
编辑:
实现它的代码有点长。这是一个带有原则的数据库驱动 ACL。我已经修改了这个教程来实现我的。我已经删除了 myACL 类,看起来和教程中的一样,ACL 插件也是一样的。我已经在 application.ini 中注册了它。
// this class build all the roles and resouces and add 2 users to 2 differents roles and so on
class CMS_Util_AddResourcesAndRoles {
private $arrModules = array();
private $arrControllers = array();
public $arrActions = array();
private $arrIgnores = array('.', '..', '.svn');
public function BuildMCAArrays() {
$this->BuildModuleArray();
$this->BuildControllersArray();
$this->BuildActionArray();
return $this;
}
public function CheckData() {
if (count($this->arrModules) < 1)
throw new CMS_Exception_ResourceNotFound("No Modules found ..");
if (count($this->arrControllers) < 1)
throw new CMS_Exception_ResourceNotFound("No Controllers found ..");
if (count($this->arrActions) < 1)
throw new CMS_Exception_ResourceNotFound("No Actions found ..");
}
public function BuildModuleArray() {
$cmsApplicationModules = opendir(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules');
while (false !== ($cmsFile = readdir($cmsApplicationModules))) {
if (!in_array($cmsFile, $this->arrIgnores)) {
if (is_dir(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $cmsFile)) {
$this->arrModules[] = $cmsFile;
}
}
}
closedir($cmsApplicationModules);
return $this->arrModules;
}
public function BuildControllersArray() {
if (count($this->arrModules) > 0) {
foreach ($this->arrModules as $strModuleName) {
$cmsControllerFolder = opendir(APPLICATION_PATH . DIRECTORY_SEPARATOR . "modules" . DIRECTORY_SEPARATOR . $strModuleName . DIRECTORY_SEPARATOR . "controllers");
while (false !== ($cmsFile = readdir($cmsControllerFolder))) {
if (!in_array($cmsFile, $this->arrIgnores)) {
if (preg_match('/Controller/', $cmsFile)) {
// if(strtolower(substr($cmsFile, 0, -14)) != "error")
// $this->arrControllers[$strModuleName][] = strtolower(substr($cmsFile, 0, -14));
$this->arrControllers[$strModuleName][] = strtolower (substr($cmsFile, 0, -14));
}
}
}
closedir($cmsControllerFolder);
}
}
return $this->arrControllers;
}
private function BuildActionArray() {
// $arrMethods = array();
if (count($this->arrControllers) > 0) {
foreach ($this->arrControllers as $strModule => $strController) {
foreach ($strController as $strController) {
if ($strModule == "default") {
$strClassName = ucfirst($strController . 'Controller');
} else {
$strClassName = ucfirst($strModule) . '_' . ucfirst($strController . 'Controller');
}
if (!class_exists($strClassName)) {
Zend_Loader::loadFile(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $strModule . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR . ucfirst($strController) . 'Controller.php');
}
$objReflection = new Zend_Reflection_Class($strClassName);
$arrMethods = $objReflection->getMethods();
foreach ($arrMethods as $arrMethod) {
if (preg_match('/Action/', $arrMethod->name)) {
$this->arrActions[$strModule][$strController][] = substr($arrMethod->name, 0, -6);
// $this->arrActions[$strModule][$strController][] = substr($this->_camelCaseToHyphens($objMethods->name), 0, -6);
}
}
}
}
}
return $this->arrActions;
}
private function _camelCaseToHyphens($string) {
if ($string == 'currentPermissionsAction') {
$found = true;
}
$length = strlen($string);
$convertedString = '';
for ($i = 0; $i < $length; $i++) {
if (ord($string[$i]) > ord('A') && ord($string[$i]) < ord('Z')) {
$convertedString .= '-' . strtolower($string[$i]);
} else {
$convertedString .= $string[$i];
}
}
return strtolower($convertedString);
}
public function WriteResourcesToDb() {
$this->BuildMCAArrays();
$this->CheckData();
$resources = array();
foreach ($this->arrModules as $strModuleName) {
if (array_key_exists($strModuleName, $this->arrControllers)) {
foreach ($this->arrControllers[$strModuleName] as $strControllerName) {
if (array_key_exists($strControllerName, $this->arrActions[$strModuleName])) {
foreach ($this->arrActions[$strModuleName][$strControllerName] as $strActionName) {
$res = new CMS_Model_Resource();
$res->module_name = $strModuleName;
$res->controller_name = $strControllerName;
$res->action_name = $strActionName;
$res->name = $strModuleName . "_" . $strControllerName . "_" . $strActionName;
$resources[] = $res;
$this->PersistResource($resources);
}
}
}
}
}
return $this;
}
private function PersistResource(array $resourceobject) {
try {
$collection = new Doctrine_Collection("CMS_Model_Resource");
foreach ($resourceobject as $resource) {
$collection->add($resource);
}
$collection->save();
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
}
public function WriteRoleAndUserstoDb(){
$guest = new CMS_Model_Role();
$guest->name = "guest";
$guest->description = "simple user";
$guest->canbedeleted = true;
$member = new CMS_Model_Role();
$member->name = "member";
$member->description = "member with limited privileges,can access member reserved resources";
$member->canbedeleted = true;
$publisher = new CMS_Model_Role();
$publisher->name = "publisher";
$publisher->description = "publisher with publish an unpublished privileges";
$publisher->canbedeleted = true;
$manager = new CMS_Model_Role();
$manager->name = "manager";
$manager->description = "manager with privileges to publish, to unpublish, general manager of the site";
$manager->canbedeleted = true;
$admin = new CMS_Model_Role();
$admin->name = "administrator";
$admin->description = "admin with all privileges";
$admin->canbedeleted = false;
$superadmin = new CMS_Model_Role();
$superadmin->name = "superadmin";
$superadmin->description = "superadmin to rule them all";
$superadmin->canbedeleted = false;
$superadmin->Parents[0] = $admin;
$admin->Parents[0] = $manager;
$manager->Parents[0] = $publisher;
$publisher->Parents[0] = $member;
$member->Parents[0] = $guest;
$adminname = new CMS_Model_User();
$adminname->id = CMS_Util_Common::uuid();
$adminname->first_name = "adminname";
$adminname->last_name = "surname";
$adminname->full_name = "adminname surname";
$adminname->password = "password";
$adminname->email = "mister@somemail.com";
$adminname->is_active = true;
$adminname->is_verified = true;
$adminname->username ="superadmin";
$adminname->Role = $superadmin;
$adminname2 = new CMS_Model_User();
$adminname2->id = CMS_Util_Common::uuid();
$adminname2->first_name = "adminname2";
$adminname2->last_name = "adminsurname";
$adminname2->email="shallom@someemail.fr";
$adminname2->full_name = "adminname2 adminsurname";
$adminname2->password = "adminadmin";
$adminname2->is_active = true;
$adminname2->is_verified = true;
$adminname2->username ="admin";
$adminname2->Role = $admin;
$thepublisher = new CMS_Model_User();
$thepublisher->id = CMS_Util_Common::uuid();
$thepublisher->first_name = "one publisher";
$thepublisher->last_name = "lastname";
$thepublisher->full_name = "something something";
$thepublisher->email = "user@somegmail.com";
$thepublisher->password = "password";
$thepublisher->username = "publisher";
$thepublisher->is_active = true;
$thepublisher->is_verified = true;
$thepublisher->Role = $publisher;
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
$conn->flush();
return $this;
}
public function AssignResourcesToRoles(){
$guestcollection = new Doctrine_Collection("CMS_Model_RoleResource");
$guestroles = Doctrine_Core::getTable("CMS_Model_Role")->GetRoleByName("guest");
$defautresources = Doctrine_Core::getTable("CMS_Model_Resource")->GetResourceByModule("default");
foreach($defautresources as $resource){
$guestroleresource = new CMS_Model_RoleResource();
$guestroleresource->Role = $guestroles;
$guestroleresource->Resource = $resource;
$guestcollection->add($guestroleresource);
}
$guestcollection->save();
$admincollection = new Doctrine_Collection("CMS_Model_RoleResource");
$adminroles = Doctrine_Core::getTable("CMS_Model_Role")->GetRoleByName("superadmin");
$adminresources = Doctrine_Core::getTable("CMS_Model_Resource")->GetResourceByModule("admin");
foreach($adminresources as $resource){
$adminroleresource = new CMS_Model_RoleResource();
$adminroleresource->Role = $adminroles;
$adminroleresource->Resource = $resource;
$admincollection->add($adminroleresource);
}
$admincollection->save();
return $this;
}
public function SetAclUp(){
$this->WriteResourcesToDb();
$this->WriteRoleAndUserstoDb();
$this->AssignResourcesToRoles();
return $this;
}
}
如您所见,我已将默认情况下的所有链接授予角色guest
含义guest
,可以default::error::error page
在出现问题时看到。
我还可以向您保证,当我的代码没有任何问题时,我可以使用publisher
凭据登录并在我尝试进入管理面板的任何时候被退回。