Beispiel #1
0
 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);
                 }
             }
         }
     }
 }