views:

102

answers:

1

Hey guys, I'm starting with Zend, and trying to understand the way it works (getting there), and with Acl classes, people seem to declare all the roles and resources in one file. Now to me this seems a bit of a waste of system resources if the person is only logging in as a basic user, or even just a guest/visitor to the site. So I was thinking is it possible to have different classes which set the roles dependant on the role/resource of the current user.

My current idea is to use switch/case on the role in the bootstrap, and load individual Acl classes based on the role name (for example sake, 'visitor' => 'Model_VisitorAcl', 'users' => 'Model_UserAcl' and 'admin' => 'Model_AdminAcl'), each with a corresponding class file. Then within the class files do something for each one. This is working, and currently I could define all my 'admin' acls in one (backtracking through the rights heirarchy of admin-user-visitor), then in 'user' have 'user'-'visitor' roles and resources.

However this doesn't seem the best way, as if I wanted to change the role permissions for 'user' I'd need to change it for 'user' and 'admin'. This is obv not a problem with 2 roles, but on bigger systems it obviously is.

So I tried something like

class Model_AdminAcl extends Zend_Acl{
  function __construct(){
    $d = new Model_UserAcl();

    //defining the admin role and permissions, with inheritance from the roles 
    //set in Model_UserAcl
  }
}

To try and get the Model_UserAcl construct function to run, thus setting the role 'user', however this does not seem to work, throwing "Uncaught exception 'Zend_Acl_Role_Registry_Exception' with message 'Parent Role id 'users' does not exist'" error.

So whats the next step? Having individual include files for each module, and have the required classes include them as needed?

Or am I just making mountain out of molehills with this issue?

Thanks, Psy

A: 

Ok, solved this, not sure why I didn't think to try this in the first place (Friday brain frazzle I think)

I just put the contents of the constructor into its own function, and then called that function in the constructor, and make the classes inherit from the class below it.


//Model/VisitorAcl.php
class Model_VisitorAcl extends Zend_Acl{
  function __construct(){
    $this->addVisitorRules();
  }

  function addVisitorRules(){
    //adding the role and permissions
  }
}

//Model/UserAcl.php
class Model_UserAcl extends Model_VisitorAcl{
  function __construct(){
    $this->addVisitorRules();
    $this->addUserRules();
  }

  function addUserRules(){
    //adding the role and permissions
  }
}

//Model/AdminAcl.php
class Model_AdminAcl extends Model_AdminAcl{
  function __construct(){
    $this->addVisitorRules();
    $this->addUserRules();
    $this->addAdminRules();
  }

  function addAdminRules(){
    //adding the role and permissions
  }
}

If this is not the way I should go about it, then by all means please let me know.

Psy

Psytronic