public function loggedInAs() { $auth = Zend_Auth::getInstance(); if ($auth->hasIdentity()) { $user = $auth->getIdentity(); if (!isset($user->username)) { $auth->clearIdentity(); $info = 'logout'; return $info; } $logoutUrl = $this->view->url(array('controller' => 'auth', 'action' => 'logout'), null, true); $url = $this->view->url(array('controller' => 'user', 'action' => 'edit', 'id' => $user->id)); $info = '<div class ="menuButton"><span class="menu">' . $user->username . '</span>'; $info .= '<ul> <li><a href="' . $url . '">Mon profil</a></li> <li class="separator"></li> <li><a href="' . $logoutUrl . '" class="logout">se déconnecter</a></li> </ul></div>'; return $info; } $request = Zend_Controller_Front::getInstance()->getRequest(); $controller = $request->getControllerName(); $action = $request->getActionName(); if ($controller == 'auth' && $action == 'index') { return ''; } $form = new Application_Form_Login(); $loginUrl = $this->view->url(array('controller' => 'auth', 'action' => 'index'), null, true); $info = '<div class ="menuButton"><span class="menu"> Se connecter </span><ul><li class="form">' . $form->setAction($loginUrl) . '</li></ul></div>'; return $info; //$loginUrl = $this->view->url(array('controller'=>'auth', 'action'=>'index')); //return '<a href="'.$loginUrl.'">Login</a>'; }
public function indexAction() { $form = new Application_Form_Login(); $form->setAction('/auth'); if ($this->_request->isPost()) { $formData = $this->_request->getPost(); if ($form->isValid($formData)) { $auth = Zend_Auth::getInstance(); $db = Zend_Registry::get('db'); $authAdapter = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password'); $authAdapter->setIdentity($formData['username']); $authAdapter->setCredential($formData['password']); $result = $authAdapter->authenticate(); $session = Zend_Registry::get('session'); switch ($result->getCode()) { case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID: $session->erro = 'senha'; $form->populate($formData); break; case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND: $session->erro = 'Usuario Inválido'; $form->populate($formData); break; case Zend_Auth_Result::SUCCESS: $data = $authAdapter->getResultRowObject(); $session->role = $data->role; $this->_redirect('/post/retrieve'); break; } } else { $form->populate($formData); } } $this->view->form = $form; }
public function formAuth() { if (Zend_Auth::getInstance()->hasIdentity()) { $auth = Zend_Auth::getInstance()->getIdentity(); echo 'Olá, <strong>' . $this->view->escape($auth->nome) . '</strong>' . ' | <a href="' . $this->view->baseUrl('auth/logout') . '">Sair</a>'; } else { $form = new Application_Form_Login(); $form->setAction($this->view->baseUrl('auth/login')); echo $form; } }
public function loginAction() { /* $manager = $this->getFrontController() ->getParam('bootstrap') ->getPluginResource('cachemanager') ->getCacheManager(); //Zend_Debug::dump($manager); $cache = $manager->getCache('database'); //$cache = Zend_Controller_Front::getInstance()->getParam("bootstrap")->getPluginResource('cachemanager')->getCache('database'); //Zend_Debug::dump($cache); $count=0; if ($count = $cache->load('count') ){ $cache->save($count+1, 'count'); }else{ $cache->save($count+1, 'count'); } Zend_Debug::dump($count); */ $message = ""; $session = new Zend_Session_Namespace(); $loginForm = new Application_Form_Login(); $redirect = $this->getRequest()->getParam('redirect', 'index/index'); $loginForm->setAttrib('redirect', $redirect); $loginForm->setAction('login'); $loginForm->setMethod('post'); $loginForm->setDecorators(array('FormElements', array('HtmlTag', array('tag' => 'dl', 'class' => 'formUl')), 'Form')); $this->view->loginForm = $loginForm; $auth = Zend_Auth::getInstance(); if ($auth->hasIdentity()) { $this->_redirect('/index/'); } else { if ($this->getRequest()->isPost()) { if ($this->getRequest()->getParam('source')) { //Redirected from Other Actions return; } if ($loginForm->isValid($this->getRequest()->getPost())) { $username = $this->getRequest()->getPost('username'); $pwd = $this->getRequest()->getPost('pass'); //Check if Block $user = new Application_Model_DbTable_Mstuser(); if ($user->isBlocked($username) == 1) { $auth = Zend_Auth::getInstance(); $auth->clearIdentity(); Zend_Session::destroy(true); $this->view->errors = "Invalid username or password."; return; } //Authenticate now $authAdapter = new Application_Model_AuthAdapter($username, $pwd); $result = $auth->authenticate($authAdapter); if (!$result->isValid()) { //Get how many times user has tried in this session and block if applicable $sysVar = new Application_Model_DbTable_SysVariables(); $login_try_count_max = $sysVar->get(Rgm_Constants::SYS_VAR_BLOCK_USER_AT_NUMBER_OF_TRY_TO_LOGIN); $login_try_count_max = intval($login_try_count_max); $login_try_count = 0; $login_try_login = ''; if (isset($session->login_try_count)) { $login_try_count = $session->login_try_count; } $login_try_count = intval($login_try_count); if (isset($session->login_try_login)) { $login_try_login = $session->login_try_login; } if ($login_try_login == $username) { $login_try_count = $login_try_count + 1; } else { $login_try_count = 1; $login_try_login = $username; } if ($login_try_count >= $login_try_count_max) { $remarks = 'Blocked by system while trying to login more than ' . $login_try_count_max . ' times'; if ($this->blockAccount($username, $remarks)) { Rgm_UserServices::log(0, 'mst_user', $remarks . '(' . $username . ')', ''); unset($session->login_try_count); unset($session->login_try_login); } unset($session->login_try_count); unset($session->login_try_login); if ($login_try_count == $login_try_count_max) { $message = "Warning:: Your account is blocked. Please contact concerned authorities."; } } else { if ($login_try_count > 1) { $message = "Warning:: You have tried " . $login_try_count . " attempts to login. Your account will be blocked after " . ($login_try_count_max - $login_try_count) . " more attempts."; } $session->login_try_count = $login_try_count; $session->login_try_login = $username; } switch ($result->getCode()) { case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID: $message = 'Error:: User credentials not found' . ($message == '' ? "" : "<br/>") . $message; } $this->view->errors = $message; } else { //Successfully logged in //Clear the login try session variable if (isset($session->login_try_count)) { unset($session->login_try_count); unset($session->login_try_login); } $authArray = $result->getIdentity(); $userid = $authArray['user_id']; Rgm_UserServices::log($userid, 'mst_user', 'Loged in by ' . $username, ''); $this->_redirect($redirect); } } } } }