Tôi đã thực hiện những điều tương tự cách đây không lâu. Khái niệm cơ bản sau trong một mã ví dụ.
Tôi đã tạo tệp configAcl.php của riêng mình được tải trong tệp bootstrap, trong trường hợp của tôi là tệp index.php. Dưới đây là cách thực hiện theo trường hợp của bạn:
$acl = new Zend_Acl();
$roles = array('admin', 'normal');
// Controller script names. You have to add all of them if credential check
// is global to your application.
$controllers = array('auth', 'index', 'news', 'admin');
foreach ($roles as $role) {
$acl->addRole(new Zend_Acl_Role($role));
}
foreach ($controllers as $controller) {
$acl->add(new Zend_Acl_Resource($controller));
}
// Here comes credential definiton for admin user.
$acl->allow('admin'); // Has access to everything.
// Here comes credential definition for normal user.
$acl->allow('normal'); // Has access to everything...
$acl->deny('normal', 'admin'); // ... except the admin controller.
// Finally I store whole ACL definition to registry for use
// in AuthPlugin plugin.
$registry = Zend_Registry::getInstance();
$registry->set('acl', $acl);
Trường hợp khác là nếu bạn muốn cho phép người dùng bình thường "liệt kê" hành động trên tất cả các bộ điều khiển của bạn. Rất đơn giản, bạn sẽ thêm dòng như sau:
$acl->allow('normal', null, 'list'); // Has access to all controller list actions.
Tiếp theo bạn nên tạo plugin mới tự động kiểm tra thông tin xác thực khi có yêu cầu cho một số hành động điều khiển. Việc kiểm tra này diễn ra trong phương thức preDispatch() được gọi trước mỗi cuộc gọi đến hành động điều khiển.
Đây là AuthPlugin.php:
class AuthPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$loginController = 'auth';
$loginAction = 'login';
$auth = Zend_Auth::getInstance();
// If user is not logged in and is not requesting login page
// - redirect to login page.
if (!$auth->hasIdentity()
&& $request->getControllerName() != $loginController
&& $request->getActionName() != $loginAction) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoSimpleAndExit($loginAction, $loginController);
}
// User is logged in or on login page.
if ($auth->hasIdentity()) {
// Is logged in
// Let's check the credential
$registry = Zend_Registry::getInstance();
$acl = $registry->get('acl');
$identity = $auth->getIdentity();
// role is a column in the user table (database)
$isAllowed = $acl->isAllowed($identity->role,
$request->getControllerName(),
$request->getActionName());
if (!$isAllowed) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoUrlAndExit('/');
}
}
}
}
Các bước cuối cùng đang tải configAcl.php của bạn và đăng ký AuthPlugin trong file bootstrap (có thể là index.php).
require_once '../application/configAcl.php';
$frontController = Zend_Controller_Front::getInstance();
$frontController->registerPlugin(new AuthPlugin());
Vì vậy, đây là khái niệm cơ bản. Tôi đã không kiểm tra mã ở trên (sao chép và dán và viết lại chỉ cho mục đích giới thiệu) để nó không được chống đạn. Chỉ để đưa ra một ý tưởng.
EDIT
Đối với sự rõ ràng. Đoạn mã trên trong AuthPlugin giả sử rằng đối tượng $ identity được điền đầy dữ liệu người dùng (cột "role" trong cơ sở dữ liệu). Điều này có thể được thực hiện trong quá trình đăng nhập như sau:
[...]
$authAdapter = new Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('Users');
$authAdapter->setIdentityColumn('username');
$authAdapter->setCredentialColumn('password');
$authAdapter->setIdentity($username);
$authAdapter->setCredential(sha1($password));
$authAdapter->setCredentialTreatment('? AND active = 1');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
$data = $authAdapter->getResultRowObject(null, 'password'); // without password
$auth->getStorage()->write($data);
[...]
Cấu trúc trang web của tôi ở đâu? ĐƠN khiển mô hình xem CÔNG css hình ảnh js đèn flash THƯ VIỆN zend lời khuyên nào? – Udders
configAcl.php có thể ở bất kỳ đâu trong thư mục Ứng dụng của bạn, nó chỉ để đưa vào index.php. AuthPlugin phải nằm trong Thư viện của bạn - do đó Zend có thể tự động tải nó và bạn không phải sử dụng require_once. Các câu lệnh cuối cùng giữa [...] phụ thuộc vào quá trình đăng nhập của bạn ... –