/**
  * Hook that allows actions when data was saved
  *
  * When not rerouted, the form will be populated afterwards
  *
  * @param int $changed The number of changed rows (0 or 1 usually, but can be more)
  */
 protected function afterSave($changed)
 {
     if ($changed) {
         $this->accesslog->logChange($this->request, null, $this->formData);
         // Reload the current user data
         $user = $this->currentUser;
         $currentOrg = $user->getCurrentOrganizationId();
         $this->loader->getUserLoader()->unsetCurrentUser();
         $user = $this->loader->getUser($user->getLoginName(), $user->getBaseOrganizationId())->setAsCurrentUser();
         $user->setCurrentOrganization($currentOrg);
         // In case locale has changed, set it in a cookie
         \Gems_Cookies::setLocale($this->formData['gsf_iso_lang'], $this->basepath);
         $this->addMessage($this->_('Saved your setup data', $this->formData['gsf_iso_lang']));
     } else {
         $this->addMessage($this->_('No changes to save!'));
     }
     if ($this->cacheTags && $this->cache instanceof \Zend_Cache_Core) {
         $this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, (array) $this->cacheTags);
     }
 }
 public function changeUiAction()
 {
     $request = $this->getRequest();
     $lang = strtolower($request->getParam('language'));
     $url = base64_decode($request->getParam('current_uri'));
     if (!$url || '/' !== $url[0]) {
         throw new \Exception($this->_('Illegal language redirect url.'));
     }
     if (in_array($lang, $this->view->project->locales)) {
         $this->currentUser->setLocale($lang);
         if (\Gems_Cookies::setLocale($lang, $this->basepath->getBasePath())) {
             if ($url) {
                 $this->getResponse()->setRedirect($url);
             } else {
                 $this->currentUser->gotoStartPage($this->menu, $this->getRequest());
             }
             return;
         }
         throw new \Exception($this->_('Cookies must be enabled for setting the language.'));
     }
     throw new \Exception($this->_('Invalid language setting.'));
 }
 /**
  * Common handler utility to initialize tokens from parameters
  *
  * @return boolean True if there is a real token specified in the request
  */
 protected function _initToken()
 {
     if ($this->tracker) {
         return $this->token && $this->token->exists;
     }
     $this->tracker = $this->loader->getTracker();
     $this->tokenId = $this->tracker->filterToken($this->_getParam(\MUtil_Model::REQUEST_ID));
     if (!$this->tokenId) {
         return false;
     }
     $this->token = $this->tracker->getToken($this->tokenId);
     if (!$this->token->exists) {
         return false;
     }
     if (!($this->currentUser->isActive() || $this->token->getSurvey()->isTakenByStaff())) {
         $tokenLang = strtolower($this->token->getRespondentLanguage());
         // \MUtil_Echo::track($tokenLang, $this->locale->getLanguage());
         if ($tokenLang != $this->locale->getLanguage()) {
             $this->locale->setLocale($tokenLang);
             $this->translateAdapter->setLocale($this->locale);
             $this->currentUser->setLocale($tokenLang);
             \Gems_Cookies::setLocale($tokenLang, $this->basepath->getBasePath());
         }
         $currentOrg = $this->loader->getOrganization();
         $tokenOrgId = $this->token->getOrganizationId();
         if ($tokenOrgId != $currentOrg->getId()) {
             $this->loader->getOrganization($tokenOrgId)->setAsCurrentOrganization();
         }
     }
     return true;
 }
 /**
  * Default login page
  */
 public function loginAction()
 {
     $request = $this->getRequest();
     $form = $this->createLoginForm();
     // Retrieve these before the session is reset
     $staticSession = \GemsEscort::getInstance()->getStaticSession();
     $previousRequestParameters = $staticSession->previousRequestParameters;
     $previousRequestMode = $staticSession->previousRequestMode;
     if ($form->wasSubmitted()) {
         if ($form->isValid($request->getPost(), false)) {
             $user = $form->getUser();
             $user->setAsCurrentUser();
             if ($messages = $user->reportPasswordWeakness($request->getParam($form->passwordFieldName))) {
                 $user->setPasswordResetRequired(true);
                 $this->addMessage($this->_('Your password must be changed.'));
                 foreach ($messages as &$message) {
                     $message = ucfirst($message) . '.';
                 }
                 $this->addMessage($messages);
             }
             /**
              * Fix current locale in cookies
              */
             \Gems_Cookies::setLocale($user->getLocale(), $this->basepath->getBasePath());
             /**
              * Ready
              */
             $this->addMessage(sprintf($this->_('Login successful, welcome %s.'), $user->getFullName()), 'success');
             /**
              * Log the login
              */
             $this->accesslog->logChange($request);
             if ($previousRequestParameters) {
                 $this->_reroute(array('controller' => $previousRequestParameters['controller'], 'action' => $previousRequestParameters['action']), false);
             } else {
                 // This reroutes to the first available menu page after login.
                 //
                 // Do not user $user->gotoStartPage() as the menu is still set
                 // for no login.
                 $this->_reroute(array('controller' => null, 'action' => null), true);
             }
             return;
         } else {
             $errors = \MUtil_Ra::flatten($form->getMessages());
             // \MUtil_Echo::track($errors);
             //Also log the error to the log table
             //when the project has logging enabled
             $logErrors = join(' - ', $errors);
             $msg = sprintf('Failed login for : %s (%s) - %s', $request->getParam($form->usernameFieldName), $request->getParam($form->organizationFieldName), $logErrors);
             $this->accesslog->logChange($request, $msg);
         }
         // */
     } else {
         if ($request->isPost()) {
             $form->populate($request->getPost());
         }
     }
     $this->displayLoginForm($form);
 }