I am new to Zend
I searched it and got the following link, how to keep track of currently logged user in zend framework.
But my requirement is different.
My situation is, I am having seperate controller called AuthenticationController to store the logged user details in session using Zend_Auth and doing other stuff,Now i have to get the logged user info, when every time logged in user access every page of the site.
so i have to do some logic in common file that fires every time to get user info in zend.
How can i do that in zend ?
Following is sample piece of code ..
if ($form->isValid($request->getPost())) {
if ($this->_process($form->getValues())) {
$authentication = Zend_Auth::getInstance();
if ($authentication ->getIdentity()->myfirstlogin == 0) {
$this->_helper->redirector('index', 'test1');
} else {
$this->_helper->redirector('index', 'test2');
}
} else {
$myUserStatus = new Zend_Session_Namespace('myuserStatus');
if ($myUserStatus ->status == 2) {
$this->_helper->FlashMessenger('Incorrect Username / Password');
} else if ($myUserStatus ->delete == 1) {
$this->_helper->FlashMessenger('Your **** approval.');
} else {
$this->_hel开发者_如何学运维per->FlashMessenger('Please ****** administrator');
}
unset($myUserStatus);
$this->_helper->redirector('index', 'auth');
}
}
.......
I believe you have used Database Table Authentication & you have stored your results like this. If this is correct.
The, you should be able to validate the session and get the details like this (in any controller you want):
To Validate:
$sessionvars = Zend_Auth::getInstance()->getIdentity();
if (!Zend_Auth::getInstance()->hasIdentity()) {
$this->_redirect('/');
}
...
...
To retrieve the data:
$userData = Zend_Auth::getInstance()->getIdentity();
print_r($userData);
I believe the OP wanted the auth check in a common file. I think the best option for this is making a Plugin
Create a new class in the library folder. You may choose to have a different folder structure, but the folder structure I have used for this example is library/Custom/Plugins/AuthCheck.php
class Custom_Plugins_AuthCheck extends Zend_Controller_Plugin_Abstract
{
public function preDispatch (Zend_Controller_Request_Abstract $request)
{
// checking for identity. We want to avoid the check if this is the login form.
if (($request->getModuleName() == 'default' && $request->getControllerName() == 'login')){
return;
}
else if(!Zend_Auth::getInstance()->hasIdentity()){ // if identity does not exist..redirect to login form
$request->setModuleName('default')->setControllerName('login')->setActionName('index');
$request->setDispatched(false);
}
}
}
Note that I have used a function called preDispatch. This is not a random name choice. I want to call this piece of code every time before my action is dispatched. And Zend Framework has an in-built plugin call during this stage and it is called preDispatch()
You can read more about plugins here in the official manual.
Now our plugin is ready. We have to register this plugin so that Zend Framework knows that it has to call this plugin every time there is a preDispatch() plugin call.
the simplest way that I know to register a plugin is adding this piece of line to your application.ini
resources.frontController.plugins.AuthCheck = "Custom_Plugins_AuthCheck"
Now your plugin will be called automatically for every request.
I hope I have answered your question. Please let me know if I missed something in the post.
精彩评论