Hi, 

I have posted my current code below and I was wondering as to whether what I
have done is the most efficient and sensible way of doing it.  I basically
need the quickest and fastest method for ACL queries:

I have also posted the db structure as well:

class My_Acl extends Zend_ACL {

    private $_db;
    
    public function __construct()
    {
        $params = array('dbname' => ROOT . DS . 'database' . DS .
'acl.db3');
        $this->_db= Zend_Db::factory('PDO_SQLITE', $params);
    }
    
    private function initRoles()
    {
        $roles = $this->_db->fetchAll(
        $this->_db->select()
        ->from('acl_roles')
        ->order(array('role_order DESC')));
        
        $this->addRole(new Zend_Acl_Role($roles[0]['role_name']));
        
        for ($i = 1; $i < count($roles); $i++) {
            $this->addRole(new Zend_Acl_Role($roles[$i]['role_name']),
$roles[$i-1]['role_name']);
        }
    }
    
    private function initResources()
    {
        $this->initRoles();

        $resources = $this->_db->fetchAll(
        $this->_db->select()
        ->from('acl_resources'));

        foreach ($resources as $key=>$value){
            $this->add(new Zend_Acl_Resource($value['resource_name']));
        }
    }
    
    private function roleResource()
    {
        $this->initResources();
        $acl = $this->_db->fetchAll(
        $this->_db->select()
        ->from('acl_roles')
        ->from('acl_resources')
        ->where('acl_roles.role_id = acl_resources.role_id'));

        foreach ($acl as $key=>$value) {
            $this->allow($value['role_name'], $value['resource_name'] );

        }
    }

    public function queryUser($user)
    {
        $this->roleResource();

        $getUserRole = $this->_db->fetchRow(
        $this->_db->select()
        ->from('acl_roles')
        ->from('acl_users')
        ->where('acl_users.user_name = "' . $user . '"')
        ->where('acl_users.role_id = acl_roles.role_id'));

        $this->addRole(new Zend_Acl_Role($user), $getUserRole['role_name']);
    }
    
    public function getRole($user)
    {
        $getUserRole = $this->_db->fetchRow(
        $this->_db->select()
        ->from('acl_roles')
        ->from('acl_users')
        ->where('acl_users.user_name = "' . $user . '"')
        ->where('acl_users.role_id = acl_roles.role_id'));

        return $getUserRole['role_name'];        
    }
}

Build database:

    $db->query('CREATE TABLE `acl_users` (
                `uid` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                `role_id` INTEGER(4) NOT NULL,
                `user_name` VARCHAR(64) NOT NULL UNIQUE ON CONFLICT IGNORE);
               ');

    echo "\n>>> Re-creating acl_roles table\n";
    $db->query('CREATE TABLE `acl_roles` (
                `role_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                `role_order` INTEGER[4] NOT NULL UNIQUE,
                `role_name` VARCHAR(64) NOT NULL UNIQUE ON CONFLICT IGNORE);
               ');
    echo "\n>>> Re-creating acl_resouces table\n";
    $db->query('CREATE TABLE `acl_resources` (
                `uid` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                `role_id` INTEGER(4) NOT NULL,
                `resource_name` VARCHAR(64) NOT NULL UNIQUE ON CONFLICT
IGNORE);
               ');


I can then test this by using:

$dbParams = array('dbname' => ROOT . DS . 'database' . DS . 'acl.db3');
$db= Zend_Db::factory('PDO_SQLITE', $dbParams);

$params = new Zend_Controller_Request_Http();

$acl = new My_Acl();

echo "<pre>";

echo $params->user . " - " . $acl->getRole($params->user);

$acl->queryUser($params->user);

$resources = $db->fetchAll(
$db->select()
->from('acl_resources')
);

foreach ($resources as $key=>$r) {
    try {
        echo "<br>Can user " . $params->user . " use " . $r['resource_name']
. "? ";
        echo $acl->isAllowed($params->user, $r['resource_name']) ? 'allowed'
: 'denied';
    } catch (Zend_Acl_Exception $e) {
        print_r ($e->getMessage());
    }
}

Many thanks in advance,

- Robert

-- 
View this message in context: 
http://www.nabble.com/Help-with-Zend_ACL-and-using-a-Database---is-my-method-OK--tf4928641s16154.html#a14106576
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to