Sie sind auf Seite 1von 2

Gio trnh: Zend Framework Zend _Acl

Trung Tm Tin Hc Hong Nguyn http://www.hoangnguyen.edu.vn

Zend_Acl
Acl l ch vit tt ca access control list (danh sch iu khin truy cp). Zend_Acl gip chng ta phn quyn cho ng dng. N gip chng ta c th phn quyn chi tit trn tng action, controller v module Group
(Zend_Acl_Role)

Pemissions
($privileges)

Parent
($parents)

Guest Member Moderator Administrator

View Comment Add Edit Delete - Active (Granted all access)

N/A Guest Member N/A

A. Cc bc c bn s dng Zend_Acl 1. Khi to Zend_Acl


$acl = new Zend_Acl();

2. To cc Role(vai tr). Khai bo cc nhm thnh vin c trong h thng


$acl->addRole(new Zend_Acl_Role('Guest')) ->addRole(new Zend_Acl_Role('Member')) ->addRole(new Zend_Acl_Role('Moderator')) ->addRole(new Zend_Acl_Role('Administrator'));

3. Khai bo s ph thuc ln nhau ca cc nhm ( S tha k $privileges)


$acl->addRole(new ->addRole(new ->addRole(new ->addRole(new Zend_Acl_Role('Guest')) Zend_Acl_Role('Member'),'Guest') Zend_Acl_Role('Moderator'),'Member') Zend_Acl_Role('Administrator'));

Khai bo quyn cho cc nhm

$guestPrivileges = array('view'); $memberPrivileges = array('comment'); $moderatorPrivileges = array('add','edit','delete','active');

4. Cp quyn truy cp cho cc nhm


$acl->allow('Guest', null, $guestPrivileges); $acl->allow('Member', null, $memberPrivileges); $acl->allow('Moderator', null, $moderatorPrivileges); $acl->allow('Administrator');

- Trong phng thc allow() nu tham s $privileges khng xut hin ngha l nhm c ton quyn 5. Cm truy cp - Ngc li vi phng thc allow() l phng thc deny(). Phng thc ny s cp quyn cm truy cp vo
$acl->allow('Guest', null, $guestPrivileges); $acl->allow('Member', null, $memberPrivileges); $acl->allow('Moderator', null, $moderatorPrivileges); $acl->deny('Moderator', null, 'delete'); $acl->allow('Administrator');
GV: Phm V Khnh Email: zendvn@yahoo.com

Gio trnh: Zend Framework Zend _Acl

Trung Tm Tin Hc Hong Nguyn http://www.hoangnguyen.edu.vn

6. Kim tra quyn truy cp - kim tra quyn truy cp vo mt phn no ca ng dng ta s s dng phng thc kim tra isAllowed
if($acl->isAllowed('Moderator', null, 'delete')){ echo 'Ban co quyen truy cap action nay'; }else{ echo 'Ban khong quyen truy cap action nay'; }

7. Resource - Trong Acl ny xut hin mt tham s l $resource v s dng n chng ta thng dng phng thc add().
$acl->add(new Zend_Acl_Resource('admin')); $acl->add(new Zend_Acl_Resource('admin:author'),'admin'); $acl->add(new Zend_Acl_Resource('admin:member'),'admin');

8. Kim tra quyn truy cp theo Resource


$module = 'admin'; $controller = 'author'; $action = 'view'; $resource = $module . ':' . $controller; if($acl->isAllowed('Moderator', $resource, $action)){ echo 'Ban co quyen truy cap action nay'; }else{ echo 'Ban khong quyen truy cap action nay'; }

GV: Phm V Khnh Email: zendvn@yahoo.com

Das könnte Ihnen auch gefallen