public function loginAction() { $user = $this->identity(); $form = new LoginForm(); $form->get('submit')->setValue('Login'); $messages = null; $request = $this->getRequest(); if ($request->isPost()) { $registerFormFilters = new Register(); $form->setInputFilter($registerFormFilters->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $config = $this->getServiceLocator()->get('Config'); $staticSalt = $config['static_salt']; $authAdapter = new AuthAdapter($dbAdapter, 'registration', 'usr_name', 'usr_password', "MD5(CONCAT('{$staticSalt}', ?, usr_password_salt)) AND usr_active = 1"); $authAdapter->setIdentity($data['usr_name'])->setCredential($data['usr_password']); $auth = new AuthenticationService(); // or prepare in the globa.config.php and get it from there. Better to be in a module, so we can replace in another module. // $auth = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); // $sm->setService('Zend\Authentication\AuthenticationService', $auth); // You can set the service here but will be loaded only if this action called. $result = $auth->authenticate($authAdapter); switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: // do stuff for nonexistent identity break; case Result::FAILURE_CREDENTIAL_INVALID: // do stuff for invalid credential break; case Result::SUCCESS: $storage = $auth->getStorage(); $storage->write($authAdapter->getResultRowObject(null, 'usr_password')); $time = 1209600; // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days // if ($data['rememberme']) $storage->getSession()->getManager()->rememberMe($time); // no way to get the session if ($data['rememberme']) { $sessionManager = new \Zend\Session\SessionManager(); $sessionManager->rememberMe($time); } break; default: // do stuff for other failure break; } foreach ($result->getMessages() as $message) { $messages .= "{$message}\n"; } } } return new ViewModel(array('form' => $form, 'messages' => $messages)); }
public function loginAction() { $form = new LoginForm(); $form->get('submit')->setValue('Login'); /** @var \Zend\Http\PhpEnvironment\Request $request */ $request = $this->getRequest(); $referer = $request->getHeader('referer'); if ($referer === false) { return $this->redirect()->toRoute('home'); } else { $referer_path = $referer->uri()->getPath(); } $current_path = $request->getUri()->getPath(); $_SESSION['referer_path'] = $referer_path; if ($request->isPost()) { $validate = new LoginFilter(); $form->setInputFilter($validate->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $validate->exchangeArray($form->getData()); $ident = $validate->getInputFilter()->getValue('name'); $password = $validate->getInputFilter()->getValue('password'); $auth = new AuthAccess(); $authenticated = $auth->isAuthenticated($ident, $password); if ($authenticated) { $redirect_path = $_SESSION['referer_path']; if ($redirect_path == '/') { $this->redirect()->toRoute('home'); } elseif ($redirect_path == $current_path) { $this->redirect()->toRoute('home'); } $redirect_route = str_replace('/', '', $redirect_path); $this->redirect()->toRoute($redirect_route); } else { $this->redirect()->toRoute('login'); } } } return array('form' => $form); }
public function indexAction() { $api = new Api(); $auth = new FrontEndAuth(); $session = new Container('frontend'); $loginError = ""; $seturl = $this->getRequest()->getQuery('last_url'); //$redirectUrl = 'http://blog.ovessence.in/'; if ($auth->hasIdentity()) { if ($session->status_id != 3) { //$redirectUrl = array('controller' => 'practitioner', 'action' => 'list'):array('controller' => 'practitioner', 'action' => 'dashboard'); return $session->user_type_id == 4 ? $this->redirect()->toRoute('consumer', array('action' => 'dashboard')) : $this->redirect()->toRoute('practitioner', array('action' => 'dashboard')); } else { $loginError = "Sorry your are suspended to access this site ..!! "; $auth->logout($redirectUrl); } } $form = new LoginForm(); $register_form = new RegisterForm(); //$forWishlist = new Container('last_url'); $request = $this->getRequest(); if ($request->isPost()) { $login = new Login(); $form->setInputFilter($login->getInputFilter()); $data = $request->getPost()->toArray(); $form->setData($data); if ($form->isValid()) { //unset($data['rememberme'], $data['submit']); $bookingData = new Container('bookingData'); $api_url = $this->getServiceLocator()->get('Config')['api_url']['value']; $url = $api_url . "/api/useractivity/"; //$data = array("username" => "sazid1s", "password" => "123456", "op" => "login"); $data['op'] = 'login'; $res = $api->curl($url, $data, "POST"); //var_dump($res); die; if ($res->getStatusCode() == 200) { $content = json_decode($res->getBody(), true); if ($content['status_id'] != 3) { //Get verifiy status $url = $api_url . "/api/userverification/?user_id=" . $content['id']; $res = $api->curl($url, array(), "GET"); $result = json_decode($res->getBody(), true); $emailStatus = 0; $smsStatus = 0; if (count($result) > 0) { // retrieving verification code foreach ($result as $userid) { // email validation where 1:- email if ($userid['verification_type_id'] == 1) { $emailStatus = $userid['verification_status']; } // sms validation where 2:- for sms if ($userid['verification_type_id'] == 2) { $smsStatus = $userid['verification_status']; } } } //End:- Get verifiy status $session->userid = $content['id']; $session->first_name = $content['first_name']; $session->last_name = $content['last_name']; $session->email = $content['email']; $session->user_name = $content['user_name']; $session->user_type_id = $content['user_type_id']; $session->user_data = $content; $session->status_id = $content['status_id']; $session->last_login = $content['last_login_prev']; $session->email_verification_status = $emailStatus; $session->sms_verification_status = $smsStatus; // SET Cookies $time = $data['rememberme'] == 'yes' ? time() + 365 * 60 * 60 * 24 : time() - 4; $cookie = new SetCookie('username', $content['user_name'], $time); // now + 1 year $cookie1 = new SetCookie('password', $data['Pass'], $time); // now + 1 year $cookie2 = new SetCookie('rememberme', $data['rememberme'], $time); // now + 1 year $response = $this->getResponse()->getHeaders(); $response->addHeader($cookie); $response->addHeader($cookie1); $response->addHeader($cookie2); // End set cookies if ($data['rememberme'] == 'yes') { setcookie("user_name", $content['user_name'], time() + 60 * 60 * 1); setcookie("password", $data['Pass'], time() + 60 * 60 * 1); /* expire in 1 hour */ } $auth->wordpress_login($session->user_name); $redirectUrl = $session->user_type_id == 4 ? array('controller' => 'consumer', 'action' => 'dashboard') : array('controller' => 'practitioner', 'action' => 'dashboard'); if (isset($bookingData->bookingData)) { return $this->redirect()->toRoute('booking', array('action' => 'schedule', 'id' => $bookingData->sp)); } else { if ($this->getRequest()->getQuery('lasturl') != '') { return $this->redirect()->toUrl($this->getRequest()->getQuery('lasturl')); } else { return $session->user_type_id == 4 ? $this->redirect()->toRoute('consumer', array('action' => 'dashboard')) : $this->redirect()->toRoute('practitioner', array('action' => 'dashboard')); } } } //Status check $loginError = "Sorry your are suspended to access this site ..!! "; //return $this->redirect()->toUrl($redirectUrl); } else { $loginError = "Username or Password is incorrect"; } } } else { $username = $this->getRequest()->getHeaders()->get('Cookie')->username ? $this->getRequest()->getHeaders()->get('Cookie')->username : ''; $password = $this->getRequest()->getHeaders()->get('Cookie')->password ? $this->getRequest()->getHeaders()->get('Cookie')->password : ''; $rememberme = $this->getRequest()->getHeaders()->get('Cookie')->password ? $this->getRequest()->getHeaders()->get('Cookie')->rememberme : ''; $form->get('Pass')->setValue($password); $form->get('user_name')->setValue($username); $form->get('rememberme')->setValue($rememberme); } return new ViewModel(array('form' => $form, 'register_form' => $register_form, 'loginError' => $loginError, 'setUrl' => $seturl)); }