开发者

How to handle Exception when Zend_Acl is On? it gives Resource 'default::error::error' not found'

开发者 https://www.devze.com 2023-03-17 07:40 出处:网络
i\'ve implemeted the Zend_Acl and its seems to be working.my resources are links : module_name . \"::\" . controller_name . \"::\" . action_name;

i've implemeted the Zend_Acl and its seems to be working.my resources are links :

module_name . "::" . controller_name . "::" . action_name;

i've added something in my code that's breaking and it seems that's i'm redirected to the usual error page but that the Acl comes in saying

Fatal error: Uncaught exception 'Zend_Acl_Exception' with message 'Resource 'default::error::error' not found' in F:\work\php\zendworkspace\myproject\library\Zend\Acl.php on line 365

i have added the default::error::error to the resources but the error is still the same. when i remove the code that's breaking the whole thing works again. So i would definitely have the same error when something breaks in my code.

I would like to find out how to solve this. thanks for reading and sharing your experience.

Edit:

the code to implement that is kind of long. this is a db driven ACL with doctrine. i've modified this tutorial to implement mine.i've cut out the myACL class, looks the same as the one in the tutorial, and the ACL plugin is kind of the same.i've registerd it in the 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->开发者_JAVA百科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;
 }
}

as you can see i've granted all links under default to role guest meaning guest can see the default::error::error page when there is a problem.

I can also assure you that, when nothing is broken in my code, i can login with the publisher credential and get bounced anytime i'm trying to go to the admin panel.


The most popular error is that you have not added the resource for any instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号