1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| Quote
class Model_Acl extends Zend_Acl
{
private $_roles;
private $_rolesArray = array();
private $_roleInheritance;
private $_modules = array();
public function __construct()
{
$this->_roles = new Model_DbTable_Roles();
$this->_roleInheritance = new Model_DbTable_RoleInheritance();
$frontController = Zend_Controller_Front::getInstance();
$this->_modules = array_keys($frontController->getControllerDirectory());
$this->_initRessources();
$this->_initRoles();
$this->_initRights();
Zend_Registry::set('Acl', $this);
}
protected function _initRessources()
{
$frontController = Zend_Controller_Front::getInstance();
foreach($frontController->getControllerDirectory() as $module => $controllerPath) {
foreach(glob($controllerPath.'/*Controller.php') as $controller) {
$this->addResource(new Zend_Acl_Resource($module.'_'.strtolower(basename($controller,'Controller.php'))));
}
}
}
protected function _initRoles()
{
/* get roles from db */
foreach ($this->_roles->fetchAll() as $role) {
$this->_rolesArray[$role->idroles] = $role;
}
foreach ($this->_roles->fetchAll() as $role) {
if(!$this->hasRole($role->idroles)) $this->setNewRole($role);
}
}
protected function setNewRole($role) {
$inheritance = $this->_roleInheritance->fetchAll('roleId="'.$role->idroles.'"');
if(count($inheritance) > 0) {
foreach ($inheritance as $i) {
if(!$this->hasRole($i->parentRoleId)) $this->setNewRole($this->_rolesArray[$i->parentRoleId]);
$this->addRole(new Zend_Acl_Role($role->idroles),$this->_rolesArray[$i->parentRoleId]->idroles);
}
} else {
$this->addRole(new Zend_Acl_Role($role->idroles));
}
}
protected function _initRights()
{
// $this->allow(null,"default_error");
$acl = new Model_DbTable_RoleAcl();
foreach ($acl->fetchAll() as $value) {
try {
if($this->has($value->resource)):
if($value->permission == 'allow') $this->allow($value->idrole,$value->resource,!empty($value->actions)?explode(", ",$value->actions):null);
else $this->allow($value->idrole,$value->resource,!empty($value->actions)?explode(", ",$value->actions):null);
elseif($value->resource == '*'):
if($value->permission == 'allow') $this->allow($value->idrole,NULL);
else $this->allow($value->idrole,NULL);
elseif(in_array($value->resource, $this->_modules)):
$frontController = Zend_Controller_Front::getInstance();
$dir = $frontController->getControllerDirectory();
foreach(glob($dir[$value->resource].'/*Controller.php') as $controller) {
$this->allow($value->idrole,$value->resource.'_'.strtolower(basename($controller,'Controller.php')));
}
endif;
$auth = Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session('auth'));
if($auth->hasIdentity()) {
$this->allow($auth->getIdentity()->idroles,'book','edit');
$this->allow($auth->getIdentity()->idroles,'books','liste');
}
} catch (Exception $e) { /** @todo log errors and display them in admin */ }
}
}
public function isAllowed($role = null, $resource = null, $privilege = null) {
if($this->hasRole($role) and $this->has($resource)) {
return parent::isAllowed($role, $resource, $privilege);
}
$frontController = Zend_Controller_Front::getInstance();
$modules = $frontController->getControllerDirectory();
if(isset($modules[$resource])) {
foreach(glob($modules[$resource].'/*Controller.php') as $controller) {
if($this->has($resource.'_'.strtolower(basename($controller,'Controller.php'))) and !parent::isAllowed($role,$resource.'_'.strtolower(basename($controller,'Controller.php')))) return false;
}
return true;
}
return false;
}
}
<?php
class Model_DbTable_Book extends Zend_Db_Table_Abstract
{
protected $_name = 'books';
public function GetBookByID($id){
$db=Zend_Db_Table::getDefaultAdapter();
$query=$db->select()->from('books',array('id','name','auteur'))
->where('books.id=?',$id);
return $db->fetchRow($query);
}
public function getBook($id)
{
$id = (int)$id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Could not find row $id");
}
return $row->toArray();
}
public function addBook($name, $auteur)
{
$data = array(
'name' => $name,
'auteur' => $auteur);
$this->insert($data);
}
public function updateBook($id, $name,$auteur)
{
$data = array(
'name' => $name,
'auteur' => $auteur
);
$this->update($data, 'id = '. (int)$id);
}
public function deleteBook($id)
{
$this->delete('id =' . (int)$id);
}
}
?>
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of RoleAcl
*
* @author root
*/
class Model_DbTable_RoleAcl extends Zend_Db_Table_Abstract
{
protected $_name = 'role_acl';
}
?>
<?php
/*
class Model_DbTable_RoleInheritance extends Zend_Db_Table_Abstract
{
protected $_name = 'role_inheritance';
}
?>
<?php
class Model_DbTable_Roles extends Zend_Db_Table_Abstract
{
protected $_name = 'roles';
}
?>
<?php
class Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
private $_noauth = array(
//'module' => 'client',
'controller' => 'authentification',
'action' => 'login'
);
private $_noacl = array(
// 'module' => 'books',
'controller' => 'books',
'action' => 'liste'
);
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$acl = null;
$role = null;
if(Zend_Registry::isRegistered('Acl')) {
$acl = Zend_Registry::get('Acl');
} else {
throw new Zend_Controller_Exception("Acl not defined !");
}
$auth = Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session('auth'));
if($auth->hasIdentity()) {
$user = $auth->getIdentity() ;
$role = $user->idroles;
} else {
$role = 5;
}
// $module = strtolower($request->getModuleName());
$controller = strtolower($request->getControllerName());
$action = strtolower($request->getActionName());
$ressource = $acl->has(($controller)?$controller:null);
//$module.'_'.$controller)?$module.'_'.$controller:($acl->has
if(!$acl->isAllowed($role, $ressource, $action)) {
if(!$auth->hasIdentity()) {
// $module = $this->_noauth['module'];
$controller = $this->_noauth['controller'];
$action = $this->_noauth['action'];
} else {
// $module = $this->_noacl['module'];
$controller = $this->_noacl['controller'];
$action = $this->_noacl['action'];
}
}
// $request->setModuleName($module);
$request->setControllerName($controller);
$request->setActionName($action);
}
}
?> |
Partager