Ejemplo n.º 1
0
 public function signinAction()
 {
     $request = $this->getRequest();
     $NS = new Zend_Session_Namespace('Default');
     $translator = Shineisp_Registry::getInstance()->Zend_Translate;
     $customerparams = array();
     // Check the request of redirection of the user
     $redir = $request->getParam('redir');
     // Check if we have a POST request
     if (!$request->isPost()) {
         return $this->_helper->redirector('login', 'customer');
     }
     // Get our form and validate it
     $form = new Default_Form_LoginForm(array('action' => '/customer/signin/redir/' . $redir, 'method' => 'post'));
     if (!$form->isValid($request->getPost())) {
         // Invalid entries
         $this->view->form = $form;
         return $this->_helper->viewRenderer('login');
         // re-render the login form
     }
     // Get the values posted
     $params = $form->getValues();
     $auth = Zend_Auth::getInstance();
     $auth->setStorage(new Zend_Auth_Storage_Session('default'));
     // Get the customer
     $retval = Customers::login($params['email'], $params['password']);
     if (empty($retval)) {
         $result = new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, $params['email']);
         $NS->customer = null;
         $this->view->form = $form;
         $this->view->message = $translator->translate('User not found. Please check your credentials.');
         return $this->_helper->viewRenderer('login');
         // re-render the login form
     } else {
         $result = new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, null);
         $customer = Customers::getAllInfo($retval['customer_id'], "c.customer_id, a.address_id, cts.type_id, l.legalform_id, ct.country_id, cn.contact_id, s.status_id, c.*, a.*, l.*, cn.*, cts.*, s.*");
         $NS->customer = $customer;
         // Set the default control panel language
         if (!empty($retval['language'])) {
             $lang = $retval['language'];
         }
     }
     // We're authenticated! Redirect to the home page
     $auth->getStorage()->write($retval);
     // If exist a redirect request the client will be redirect after the login action
     if (!empty($NS->goto) && is_array($NS->goto)) {
         $this->_helper->redirector($NS->goto['action'], $NS->goto['controller'], $NS->goto['module'], $NS->goto['options']);
     }
     if (!empty($lang)) {
         $this->_helper->redirector('index', 'dashboard', 'default', array('lang' => $lang));
         // back to login page
     } else {
         $this->_helper->redirector('index', 'dashboard', 'default');
     }
 }
Ejemplo n.º 2
0
 /**
  *    loginAction
  *
  *    Contains login form for users. If user was redirected
  *    to login by AclManager the user is redirected back 
  *    to the page that was requested originally, 
  *    if user is already logged in redirects them to account/index page. 
  *    Writes login attemps to a log file.
  */
 public function loginAction()
 {
     // Check if user is logged in
     $auth = Zend_Auth::getInstance();
     // Get url helper
     $urlHelper = $this->_helper->getHelper('url');
     // if user is already logged in redirect away from here
     if ($auth->hasIdentity()) {
         $target = $urlHelper->url(array('controller' => 'index', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
         $this->_redirect($target);
     }
     // end if
     // login ajax functionality:
     // check where user came from (and use to redirect back later)
     if (isset($_SERVER['HTTP_REFERER'])) {
         $formOptions = $_SERVER['HTTP_REFERER'];
     } else {
         $formOptions = $urlHelper->url(array('controller' => 'index', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
     }
     // creata new LoginForm and set to view
     $form = new Default_Form_LoginForm($formOptions);
     $this->view->form = $form;
     // Get request
     $request = $this->getRequest();
     $formData = $this->_request->getPost();
     // process login if request method is post
     if ($request->isPost()) {
         // Check user authentity if form data is valid
         if ($form->isValid($formData)) {
             // Get username and password
             $data = $form->getValues();
             $users = new Default_Model_User();
             $result = $users->loginUser($data);
             // If user is authenticated
             if ($result == true) {
                 // Get user id
                 $id = $users->getIdByUsername($data['username']);
                 // record login attempt
                 $user = new Default_Model_User($id);
                 $user->loginSuccess();
                 // create identity data and write it to session
                 $identity = $user->createAuthIdentity();
                 $auth->getStorage()->write($identity);
                 //echo var_dump($auth); die;
                 // send user to front page (the old method)
                 /*$redirect = $urlHelper->url(array('controller' => 'index', 'action' => 'index', 
                   'language' => $this->view->language), 'lang_default', true);*/
                 //echo $data['returnurl']; die;
                 // Add login to log
                 $logger = Zend_Registry::get('logs');
                 if (isset($logger['login'])) {
                     $message = sprintf('Successful login attempt from %s user %s', $_SERVER['REMOTE_ADDR'], $identity->username);
                     $logger['login']->notice($message);
                 }
                 $redirect = $data['returnurl'];
                 $this->_redirect($redirect);
             } else {
                 $this->view->errormsg = $this->view->translate('account-login-not-successful');
             }
         }
         //end if
     }
     // end if
 }