La ACL básica tal como se define en la
sección anterior muestra cómo se pueden
permitir varios privilegios sobre toda la ACL (todos los recursos). En la
práctica, sin embargo, los controles de acceso tienden a tener excepciones y distintos
grados de complejidad. Zend_Acl le permite llevar a cabo estos refinamientos
de una manera sencilla y flexible.
Para el CMS de ejemplo, se ha determinado que aunque el grupo 'staff' cubre las necesidades de la gran mayoría de los usuarios, existe la necesidad de un nuevo grupo 'marketing' que requiere acceso al boletín de noticias y a las últimas noticias en el CMS. El grupo es bastante autosuficiente y tendrá la capacidad de publicar y archivar tanto boletines de noticias como las últimas noticias.
Además, también se ha solicitado que al grupo 'staff' se le permita ver las noticias pero no revisar las últimas noticias. Por último, debería ser imposible para cualquiera (incluidos los administradores) archivar cualquier noticia de tipo 'announcement', ya que solo tienen una vida útil de 1-2 días.
Primero revisamos el registro de roles para reflejar estos cambios. Hemos determinado que el grupo 'marketing' tiene los mismos permisos básicos que 'staff', por lo que definimos 'marketing' de tal manera que herede los permisos de 'staff':
// The new marketing group inherits permissions from staff
$acl->addRole(new Zend_Acl_Role('marketing'), 'staff');
A continuación, observe que los controles de acceso anteriores hacen referencia a recursos específicos (por ejemplo, "newsletter", "latest news", "announcement news"). Ahora añadimos estos recursos:
// Create Resources for the rules
// newsletter
$acl->addResource(new Zend_Acl_Resource('newsletter'));
// news
$acl->addResource(new Zend_Acl_Resource('news'));
// latest news
$acl->addResource(new Zend_Acl_Resource('latest'), 'news');
// announcement news
$acl->addResource(new Zend_Acl_Resource('announcement'), 'news');
Entonces, simplemente es cuestión de definir estas reglas más específicas sobre las áreas de destino de la ACL:
// Marketing must be able to publish and archive newsletters and the
// latest news
$acl->allow('marketing',
array('newsletter', 'latest'),
array('publish', 'archive'));
// Staff (and marketing, by inheritance), are denied permission to
// revise the latest news
$acl->deny('staff', 'latest', 'revise');
// Everyone (including administrators) are denied permission to
// archive news announcements
$acl->deny(null, 'announcement', 'archive');
Ahora podemos consultar la ACL con respecto a los últimos cambios:
echo $acl->isAllowed('staff', 'newsletter', 'publish') ?
"allowed" : "denied";
// denied
echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
"allowed" : "denied";
// allowed
echo $acl->isAllowed('staff', 'latest', 'publish') ?
"allowed" : "denied";
// denied
echo $acl->isAllowed('marketing', 'latest', 'publish') ?
"allowed" : "denied";
// allowed
echo $acl->isAllowed('marketing', 'latest', 'archive') ?
"allowed" : "denied";
// allowed
echo $acl->isAllowed('marketing', 'latest', 'revise') ?
"allowed" : "denied";
// denied
echo $acl->isAllowed('editor', 'announcement', 'archive') ?
"allowed" : "denied";
// denied
echo $acl->isAllowed('administrator', 'announcement', 'archive') ?
"allowed" : "denied";
// denied
Para eliminar una o más reglas de acceso de la ACL, simplemente use los
métodos disponibles removeAllow() o
removeDeny(). Al igual que con allow()
y deny(), puede proporcionar un valor NULL
para indicar que se aplica a todos los roles, recursos y/o privilegios:
// Remove the denial of revising latest news to staff (and marketing,
// by inheritance)
$acl->removeDeny('staff', 'latest', 'revise');
echo $acl->isAllowed('marketing', 'latest', 'revise') ?
"allowed" : "denied";
// allowed
// Remove the allowance of publishing and archiving newsletters to
// marketing
$acl->removeAllow('marketing',
'newsletter',
array('publish', 'archive'));
echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
"allowed" : "denied";
// denied
echo $acl->isAllowed('marketing', 'newsletter', 'archive') ?
"allowed" : "denied";
// denied
Los privilegios pueden modificarse de forma incremental como se indica arriba, pero un
valor NULL para los privilegios anula dichos cambios incrementales:
// Allow marketing all permissions upon the latest news
$acl->allow('marketing', 'latest');
echo $acl->isAllowed('marketing', 'latest', 'publish') ?
"allowed" : "denied";
// allowed
echo $acl->isAllowed('marketing', 'latest', 'archive') ?
"allowed" : "denied";
// allowed
echo $acl->isAllowed('marketing', 'latest', 'anything') ?
"allowed" : "denied";
// allowed