Nap*_*eon 1 zend-framework partial-views zend-auth zend-acl
根据随机性将无处不在.Ryan的博客 Zend Framework的动作堆栈组件是不需要的,并且部分视图可以与Zend_Acl和Zend_Auth结合用于验证和控制资源.
我还没有在谷歌上找到任何合适的例子来说明它是如何完成的.很高兴有人会善意地告诉我如何实现这一点.谢谢
干得好:
您可以使用Zend_Auth和Zend_Acl的组合.为了扩展其他答案,我举一个简短的例子说明如何使用zend框架管理身份验证:
首先,您需要设置一个插件来预先分配所有请求,并检查是否允许客户端访问某些数据.这个插件可能看起来像这样:
class Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract {
private $_acl = null;
public function __construct(Zend_Acl $acl) {
$this->_acl = $acl;
}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
//get request information
$module = $request->getModuleName ();
$resource = $request->getControllerName ();
$action = $request->getActionName ();
try {
if(!$this->_acl->isAllowed(Zend_Registry::get('role'),
$module . ':' . $resource, $action)){
$request->setControllerName ('authentication')
->setActionName ('login');
}
}catch(Zend_Acl_Exception $e) {
$request->setControllerName('index')->setActionName ('uups');
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,每个用户类型都具有您在acl库中定义的特定权限.在每个请求中,您都会检查是否允许用户访问资源.如果不是,您重定向到登录页面,否则preDispatch将用户传递给资源.
在Zend_Acl中,您可以定义允许或拒绝访问的角色,资源和权限,例如:
class Model_LibraryAcl extends Zend_Acl {
public function __construct() {
$this->addRole(new Zend_Acl_Role('guests'));
$this->addRole(new Zend_Acl_Role('users'), 'guests');
$this->addRole(new Zend_Acl_Role('admins'), 'users');
$this->add(new Zend_Acl_Resource('default'))
->add(new Zend_Acl_Resource('default:authentication'), 'default')
->add(new Zend_Acl_Resource('default:index'), 'default')
->add(new Zend_Acl_Resource('default:error'), 'default');
$this->allow('guests', 'default:authentication', array('login'));
$this->allow('guests', 'default:error', 'error');
$this->allow('users', 'default:authentication', 'logout');
}
}
Run Code Online (Sandbox Code Playgroud)
然后你必须在你的bootstrap文件中设置acl和auth:
private $_acl = null;
protected function _initAutoload() {
//...your code
if (Zend_Auth::getInstance()->hasIdentity()){
Zend_Registry::set ('role',
Zend_Auth::getInstance()->getStorage()
->read()
->role);
}else{
Zend_Registry::set('role', 'guests');
}
$this->_acl = new Model_LibraryAcl ();
$fc = Zend_Controller_Front::getInstance ();
$fc->registerPlugin ( new Plugin_AccessCheck ( $this->_acl ) );
return $modelLoader;
}
Run Code Online (Sandbox Code Playgroud)
最后,在您的身份验证控制器中,您必须使用自定义身份验证适配器并设置登录和注销操作:
public function logoutAction() {
Zend_Auth::getInstance ()->clearIdentity ();
$this->_redirect ( 'index/index' );
}
private function getAuthAdapter() {
$authAdapter = new Zend_Auth_Adapter_DbTable (
Zend_Db_Table::getDefaultAdapter ());
$authAdapter->setTableName('users')
->setIdentityColumn('email')
->setCredentialColumn ('password')
->setCredentialTreatment ('SHA1(CONCAT(?,salt))');
return $authAdapter;
}
Run Code Online (Sandbox Code Playgroud)
在登录操作中,您需要将登录数据传递给执行身份验证的身份验证适配器.
$authAdapter = $this->getAuthAdapter ();
$authAdapter->setIdentity ( $username )->setCredential ( $password );
$auth = Zend_Auth::getInstance ();
$result = $auth->authenticate ( $authAdapter );
if ($result->isValid ()) {
$identity = $authAdapter->getResultRowObject ();
if ($identity->approved == 'true') {
$authStorage = $auth->getStorage ();
$authStorage->write ( $identity );
$this->_redirect ( 'index/index' );
} else {
$this->_redirect ( 'authentication/login' );
}
Run Code Online (Sandbox Code Playgroud)
就这样.我建议您在此如何在YouTube上Zend的权威性和Zend的ACL.
| 归档时间: |
|
| 查看次数: |
927 次 |
| 最近记录: |