/**
  * Create action
  *
  * Method to create an user
  *
  * @return Zend\View\Model\ViewModel
  */
 public function createUserAction()
 {
     if (!$this->identity()) {
         return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
     }
     try {
         $user = new User();
         $form = $this->getUserFormHelper()->createUserForm($user, 'CreateUser');
         $request = $this->getRequest();
         if ($request->isPost()) {
             $form->setValidationGroup('username', 'email', 'firstName', 'lastName', 'password', 'passwordVerify', 'language', 'state', 'role', 'question', 'answer', 'csrf');
             $form->setData($request->getPost());
             if ($form->isValid()) {
                 $entityManager = $this->getEntityManager();
                 $user->setEmailConfirmed(false);
                 $user->setRegistrationDate(new \DateTime());
                 $user->setRegistrationToken(md5(uniqid(mt_rand(), true)));
                 $user->setPassword(UserCredentialsService::encryptPassword($user->getPassword()));
                 $entityManager->persist($user);
                 $entityManager->flush();
                 $this->flashMessenger()->addSuccessMessage($this->getTranslatorHelper()->translate('User created Successfully'));
                 return $this->redirect()->toRoute('user-admin');
             }
         }
     } catch (\Exception $e) {
         return $this->getServiceLocator()->get('csnuser_error_view')->createErrorView($this->getTranslatorHelper()->translate('Something went wrong during user creation! Please, try again later.'), $e, $this->getOptions()->getDisplayExceptions(), false);
     }
     $viewModel = new ViewModel(array('form' => $form));
     $viewModel->setTemplate('csn-user/admin/new-user-form');
     return $viewModel;
 }
 /**
  * Confirm Email Change Action
  *
  * Confirms password change through given token
  *
  * @return Zend\View\Model\ViewModel
  */
 public function confirmEmailChangePasswordAction()
 {
     $token = $this->params()->fromRoute('id');
     try {
         $entityManager = $this->getEntityManager();
         if ($token !== '' && ($user = $entityManager->getRepository('CsnUser\\Entity\\User')->findOneBy(array('registrationToken' => $token)))) {
             $user->setRegistrationToken(md5(uniqid(mt_rand(), true)));
             $password = $this->generatePassword();
             $user->setPassword(UserCredentialsService::encryptPassword($password));
             $email = $user->getEmail();
             $fullLink = $this->getBaseUrl() . $this->url()->fromRoute('user-index', array('action' => 'login'));
             $this->sendEmail($user->getEmail(), 'Your password has been changed!', sprintf($this->getTranslatorHelper()->translate('Hello again %s. Your new password is: %s. Please, follow this link %s to log in with your new password.'), $user->getUsername(), $password, $fullLink));
             $entityManager->persist($user);
             $entityManager->flush();
             $viewModel = new ViewModel(array('email' => $email, 'navMenu' => $this->getOptions()->getNavMenu()));
             return $viewModel;
         } else {
             return $this->redirect()->toRoute('user-index');
         }
     } catch (\Exception $e) {
         return $this->getServiceLocator()->get('csnuser_error_view')->createErrorView($this->getTranslatorHelper()->translate('An error occured during the confirmation of your password change! Please, try again later.'), $e, $this->getOptions()->getDisplayExceptions(), $this->getOptions()->getNavMenu());
     }
 }