public function routeShutdown(Zend_Controller_Request_Abstract $request) { $params = $request->getParams(); $auth = Zend_Auth::getInstance(); Zend_Registry::set('Zend_Auth', $auth); if ($auth->hasIdentity()) { $view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view'); $identity = $auth->getIdentity(); $userDb = new Users_Model_DbTable_User(); $user = array('id' => $identity->id, 'username' => $identity->username, 'name' => $identity->name, 'email' => $identity->email, 'clientid' => $identity->clientid); $authNamespace = new Zend_Session_Namespace('Zend_Auth'); $authNamespace->user = $user['username']; if ($_SESSION['__ZF']['Zend_Auth']['ENT'] - time() < 3600) { $authNamespace->setExpirationSeconds(3600); } Zend_Registry::set('User', $user); $view->user = $user; $clientDb = new Application_Model_DbTable_Client(); $client = $clientDb->getClient($user['clientid']); Zend_Registry::set('Client', $client); } elseif ($params['module'] != 'users' && $params['action'] != 'login') { $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); if (isset($params['id']) && $params['id']) { $redirector->gotoSimple('login', 'user', 'users', array('url' => $params['module'] . '|' . $params['controller'] . '|' . $params['action'] . '|' . $params['id'])); } else { $redirector->gotoSimple('login', 'user', 'users', array('url' => $params['module'] . '|' . $params['controller'] . '|' . $params['action'])); } } }
public function loginAction() { $auth = Zend_Registry::get('Zend_Auth'); if ($auth->hasIdentity()) { $this->_helper->redirector->gotoSimple('index', 'index', 'index'); } $this->_helper->getHelper('layout')->setLayout('plain'); $form = new Users_Form_User(); $form->submit->setLabel('USERS_LOGIN'); $form->id->removeDecorator('Label'); $this->view->form = $form; //Clients $clientsDb = new Application_Model_DbTable_Client(); $clients = $clientsDb->fetchAll(); foreach ($clients as $client) { $form->client->addMultiOption($client->id, $client->company); } if ($this->getRequest()->isPost()) { $formData = $this->getRequest()->getPost(); if ($form->isValid($formData)) { $username = $formData['username']; $password = $formData['password']; $client = $formData['client']; $stayLoggedIn = $formData['stayLoggedIn']; $authNamespace = new Zend_Session_Namespace('Zend_Auth'); $authNamespace->user = $username; if ($stayLoggedIn) { $authNamespace->setExpirationSeconds(864000); } else { $authNamespace->setExpirationSeconds(3600); } $db = Zend_Db_Table::getDefaultAdapter(); $authAdapter = new Zend_Auth_Adapter_DbTable($db); $authAdapter->setTableName('user'); $authAdapter->setIdentityColumn('username'); $authAdapter->setCredentialColumn('password'); $authAdapter->setCredentialTreatment('MD5(?)'); $authAdapter->setIdentity($username); $authAdapter->setCredential($password); $auth = Zend_Auth::getInstance(); $result = $auth->authenticate($authAdapter); if ($result->isValid()) { $storage = $auth->getStorage(); $userInfo = $authAdapter->getResultRowObject(array('id', 'username', 'name', 'email')); $userInfo->clientid = $client; $storage->write($userInfo); //Store into session if ($this->_getParam('url', null)) { $url = explode("|", $this->_getParam('url', null)); if (isset($url[3]) && $url[3]) { $this->_helper->redirector->gotoSimple($url[2], $url[1], $url[0], array('id' => $url[3])); } else { $this->_helper->redirector->gotoSimple($url[2], $url[1], $url[0]); } } $this->_helper->redirector->gotoSimple("index", "index"); } else { echo "error"; } } else { $form->populate($formData); } } }