我正在尝试实施一个足够聪明的登录系统,可以在用户决定(或被迫)进入登录页面之前将用户重定向回他们所在的页面。
我知道这似乎与this one和this one类似,但它们并未解决我的两种情况。
这里有两种情况:
用户明确决定进入登录页面:
<a href="<?php echo $this->url(array(
'controller'=>'auth',
'action'=>'login'), 'default', true); ?>">Log In</a>
用户被重定向,因为他们试图访问受保护的内容:
if (!Zend_Auth::getInstance()->hasIdentity()) {
$this->_helper->redirector('login', 'auth');
}
如何在地址栏中不显示“重定向到”网址的情况下为此实施解决方案?
答案 0 :(得分:3)
将目标网址保存在会话中。我想你有一些访问预发送插件。在那里做。然后,在登录表单处理程序中,检查会话中的目标URL,并在成功进行身份验证后重定向到该目标URL。
我项目的示例代码:
class Your_Application_Plugin_Access extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
foreach (self::current_roles() as $role) {
if (
Zend_Registry::get('bootstrap')->siteacl->is_allowed(
$role,
new Site_Action_UriPath($request->getPathInfo())
)
) return; // Allowed
}
$this->not_allowed($request);
}
private function not_allowed(Zend_Controller_Request_Abstract $request) {
$destination_url = $request->getPathInfo();
// If the user is authenticted, but the page is denied for his role, show 403
// else,
// save $destination_url to session
// redirect to login page, with $destination_url saved:
$request
->setPathInfo('/login')
->setModuleName('default')
->setControllerName('login')
->setActionName('index')
->setDispatched(false);
}
...
}
此处,current_roles()
始终包含“来宾”,这是未经身份验证的用户,Zend_Auth::hasIdentity(
}为false
。