ZF:Ert Acl Loader
Z Wiki Rafał (ert16) Trójniak
Aby szybko ładować,i łatwo konfigurować Zend_Acl w Zend_Framework, proponuję następującą skłądnię konfiguracji pliku ini
Spis treści |
Dokumentacja
Plik dzielimy na kilka sekcji, podobnie do zasady na jakiej pracuje Zend_Acl :
- roles - definicja ról
- resources - definicja zasobów
- allow - definicja zezwoleń
- deny - definicja zabraniania
roles
Przyporządkowanie ról, myślę że przykłady wytłumaczą wszystko :
roles.user =
$this->addRole(new Zend_Acl_Role("user");
roles.vip = user
$this->addRole(new Zend_Acl_Role("vip"),"user");
roles.admin = vip,user
$this->addRole(new Zend_Acl_Role("admin),array("vip","user");
Resources
Tutaj podobnie jak wyżej, ale że zasoby nie mogą dziedziczyć po wielu, więc wartość docelowa nie jest rozbijana po przecinkach na pojedyncze zasoby
resources.news =
this->add(new Zend_Acl_Resource('news'));
resources.blog = news
$this->add(new Zend_Acl_Resource('blog'),'news');
Błędny przykład
(no chyba, że zasób nazywa się 'news,blog' ) :
resources.fail = news,blog
$this->add(new Zend_Acl_Resource('fail'),'news,blog');
Allow/Deny
Ttuaj proponuję nastpujące przykłady. Dla zmiany z allow na deny, wystarczy zmienić pierwszą część z allow na deny
Pojedyńcze zezwolenie :
allow.user.article = show
$this->allow('user', 'article', 'show');
Zezwolenie jednemu uzytkownikowi na wykonanie kilku akcji na jednym zasobie
allow.user.profile = login,create
$this->allow('user', 'profile', array('login','create') );
Zezwolenie na zrobienie wszystkiego z danym zasobem danemu uzytkownikowi (słowo kluczowe all)
allow.user.blog = all
$this->allow('user', 'blog', null);
Zezwolenie użytkownikowi na wykonanie danej akcji na wszystkich zasobach
allow.admin = delete
$this->allow('admin', null, 'delete');
Zezwolenie anonimowemu użytkownikowi na wyknanie danej akcji na zasobie
allow.all.category = show
$this->allow(null, 'category', 'show');
Błędne przykłady
Niema to sensu ani ze strony Zend_Acl, ani ze strony Zend_Config_Ini ani logicznie, a może powodować zakłopotanie (tak jak u mnie przed chwilą)
allow.all = all allow.all.category = show
Kod
/* * Copyright (C) 2009 Rafal Trojniak (rafal@trojniak.net) http://trojniak.net/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * Skrypt pisany na zlecenie http://www.media77.co.uk/ */ class Model_Acl extends Zend_Acl { /** * Config is path to configration file, or configuration data * @param string|Zend_Config_Ini|array config - configuration * */ public function __construct( $config = null ) { if(is_string($config)) $config=new Zend_Config_Ini($config); if($config instanceof Zend_Config) $config=$config->toArray(); if(is_array($config)) $this->parseConfig($config); } /** * Parse configuration data * @param array config configuration array * */ public function parseConfig($config) { if(array_key_exists('roles',$config)) $this->addRoles($config['roles']); if(array_key_exists('resources',$config)) $this->addResources($config['resources']); if(array_key_exists('allow',$config)) $this->addAllow($config['allow']); if(array_key_exists('deny',$config)) $this->addDeny($config['deny']); } /** * Adds roles to ACL, values are coma-separated parents or arrays * @param string|array $roles Array of parrents * */ public function addRoles($roles) { foreach($roles as $role => $parent) if($parent) { if(is_string($parent)) $parent=explode(',',$parent); $this->addRole(new Zend_Acl_Role($role),$parent); } else { $this->addRole(new Zend_Acl_Role($role)); } } /** * Adds resources * @param array resources array of resources - parents pairs * */ public function addResources($resources) { if(! is_array($resources)) return; foreach($resources as $new => $parent) { if(empty($parent)){ $this->add(new Zend_Acl_Resource($new)); } else { $this->add(new Zend_Acl_Resource($new),$parent); } } } /** * Function add permissions readed form array * @param array all * */ public function addAllow($all) { // For all users in array foreach($all as $user=>$opt) { // Magic user all=all users = null if($user == 'all') $user=null; if(is_array($opt)) { foreach($opt as $resource=>$perms) { // Add resource if needed if(!$this->has($resource)) $this->add(new Zend_Acl_Resource($resource)); $this->addAllowPerm($user, $resource, $perms); } } else { $this->addAllowPerm($user, null, $opt); } } } /** * Function parse permissions separeted by coma, and grant them * @param string|null user * @param string|null resource * @param string|array|null perms coma-separated string or array, or null fr all * */ public function addAllowPerm($user, $resource, $perms) { // Magic permission if($perms=='all') { $this->allow($user, $resource, null); } else { // Explode and grant permissions if(is_string($perms)) $perms=explode(',',$perms); foreach($perms as $perm) $this->allow($user, $resource, $perm); } } /** * Function add permissions readed form array * @param array all * */ public function addDeny($all) { // For all users in array foreach($all as $user=>$opt) { // Magic user all=all users = null if($user == 'all') $user=null; if(is_array($opt)) { foreach($opt as $resource=>$perms) { // Add resource if needed if(!$this->has($resource)) $this->add(new Zend_Acl_Resource($resource)); $this->addDenyPerm($user, $resource, $perms); } } else { $this->addDenyPerm($user, null, $opt); } } } /** * Function parse permissions separeted by coma, and grant them * @param string|null user * @param string|null resource * @param string|array|null perms coma-separated string or array, or null fr all * */ public function addDenyPerm($user, $resource, $perms) { // Magic permission if($perms=='all') { $this->deny($user, $resource, null); } else { // Explode and grant permissions if(is_string($perms)) $perms=explode(',',$perms); foreach($perms as $perm) $this->deny($user, $resource, $perm); } } }