No mapowanie ścieżek jest takie (przykład): Jakas_Tam_Sobie_Klasa <-- nazwa klasy i ścieżka, jakas/tam/sobie/klasaClass.php. Ale co mi to daje? Chciałbym się dowiedzieć jak mogę Zend_Acl zintegrować ze swoim systemem logowania.
W końcu wiem, że to (patrz niżej) trzeba umieścić w index.php (bootstraperze):
<?php
$acl = new Zend_Acl();
$acl->add(new Zend_Acl_Resource('index'));
$acl->add(new Zend_Acl_Resource('account'));
$acl->add(new Zend_Acl_Resource('announcements'));
$acl->add(new Zend_Acl_Resource('admin'));
$acl->add(new Zend_Acl_Resource('contact'));
$acl->addRole(new Zend_Acl_Role('guest'));
$acl->addRole(new Zend_Acl_Role('member'), 'guest');
$acl->addRole(new Zend_Acl_Role('admin'), 'member');
$acl->allow('guest', 'index');
$acl->allow('guest', 'contact');
$acl->allow('member', 'account');
$acl->allow('member', 'announcements');
$acl->allow('admin');
?>
Co dalej muszę zrobić? Jakie pliki i gdzie utworzyć? Proszę o pomoce, nakierowanie - nie gotowce.
Poczytałem jeszcze trochę i już więcej wykombinowałem:
W library dwa pliczki:
<?php
class My_Controller_Helper_Acl {
public $acl;
public function __construct() {
$this->acl = new Zend_Acl();
}
public function setRoles() {
$this->acl->addRole(new Zend_Acl_Role('guest'));
$this->acl->addRole(new Zend_Acl_Role('user'));
$this->acl->addRole(new Zend_Acl_Role('admin'));
}
public function setResources() {
$this->acl->add(new Zend_Acl_Resource('index'));
$this->acl->add(new Zend_Acl_Resource('account'));
$this->acl->add(new Zend_Acl_Resource('announcements'));
$this->acl->add(new Zend_Acl_Resource('admin'));
$this->acl->add(new Zend_Acl_Resource('contact'));
}
public function setPrivilages() {
$this->acl->allow('guest', null, 'index');
$this->acl->allow('user', array('account', 'announcements', 'contact')); $this->acl->allow('admin');
}
public function setAcl() {
Zend_Registry::set('acl', $this->acl);
}
}
?>
oraz
<?php
class My_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$application = new Zend_Session_Namespace('myApplication');
$acl = Zend_Registry::get('acl');
if($application->currentRole == '') {
$roleName = 'guest'; // jezeli w sesji obecna role (typ) uzytkownika jest pusty, zostanie ustawione guest
} else {
$roleName = $application->currentRole; // w przeciwnym wypadku zapisujemy w zmiennej $roleName nasza role (typ) uzytkownika
}
$privilageName = $application->loggedUser;
if(!$acl->isAllowed($roleName, null, $privilageName)) {
// cos tam, nie wejdziesz tutaj, bla bla...
}
}
}
?>
i w bootstraper (index.php):
<?php
// acl
$helper= new My_Controller_Helper_Acl();
$helper->setRoles();
$helper->setResources();
$helper->setPrivilages();
$helper->setAcl();
$frontController->registerPlugin(new My_Controller_Plugin_Acl());
?>
No i to ani nie popsuło strony, ani nie działa. Pewnie trzeba jakoś do tego acl wysłać role (typ) użytkownika (czy to użytkownik, czy to admin).
@edit: poprawiłem ten drugi plik Acl.php, starałem się zrobić, żeby dostawał w sesji role i nazwę, no ale nadal coś nie śmiga mi to. Może teraz mnie ktoś nakieruje?
Aha i dodałem też w logowaniu w kontrolerze:
<?php
protected $_application; // przed funkcjami
$this->view->baseUrl = $this->_request->getBaseUrl(); // w init
$this->_application = new Zend_Session_Namespace('myApplication'); // w init
$this->_application->currentRole = $this->user->role; // w indexAction, jak juz sie zaloguje
$this->_application->loggedUser = $this->user->login; // w indexAction, jak juz sie zaloguje
?>
No i i tak po każdej stronie można latać jak się chce ;/