public function passwordChangeAction()
 {
     $credentialRepo = $this->entityManager->getRepository($this->config['credentialClass']);
     /** @var UserInterface $user */
     $user = $this->identity();
     $form = new PasswordChangeForm();
     $form->setAttribute('action', $this->url()->fromRoute($this->routes['password-change']['name'], $this->routes['password-change']['params'], $this->routes['password-change']['options'], $this->routes['password-change']['reuseMatchedParams']));
     /** @var Request $request */
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = $request->getPost();
         $form->setData($post);
         if ($form->isValid()) {
             $data = $form->getData();
             $credential = $credentialRepo->findOneBy(array($this->config['credentialIdentityProperty'] => $user, 'type' => $this->config['credentialType']));
             $passwordOld = sha1(sha1($data['password-old']));
             $passwordNew = sha1(sha1($data['password-new']));
             $password = $credential->getValue();
             if ($password == $passwordOld) {
                 $credential->setValue($passwordNew);
                 $this->entityManager->flush();
                 $this->flashMessenger()->addSuccessMessage(_('Your password has been changed successfully!'));
                 return $this->redirect()->toRoute('tssAuthentication/default', array('controller' => 'account'));
             } else {
                 $this->flashMessenger()->addErrorMessage(_('Your current password is incorrect.'));
             }
         } else {
             $this->flashMessenger()->addErrorMessage(_('Form with errors!'));
         }
     }
     $form->prepare();
     $viewModel = new ViewModel(array('form' => $form, 'user' => $user, 'routes' => $this->routes));
     return $viewModel;
 }
 public function passwordRecoverAction()
 {
     $identityRepo = $this->entityManager->getRepository($this->config['identityClass']);
     $credentialRepo = $this->entityManager->getRepository($this->config['credentialClass']);
     $token = $this->params()->fromRoute('token', 0);
     if ($this->identity()) {
         $this->authenticationService->getStorage()->forgetMe();
         $this->authenticationService->clearIdentity();
     }
     $qb = $identityRepo->createQueryBuilder('i');
     $qb->where('i.token = :token');
     $qb->setParameter('token', $token);
     /** @var UserInterface $identity */
     $identity = $qb->getQuery()->getOneOrNullResult();
     if ($identity == null) {
         $this->flashMessenger()->addErrorMessage(_('Token invalid or you already confirmed this link.'));
         return $this->redirect()->toRoute($this->routes['signin']['name'], $this->routes['signin']['params'], $this->routes['signin']['options'], $this->routes['signin']['reuseMatchedParams']);
     }
     $form = new PasswordChangeForm();
     $this->routes['password-recover']['params']['token'] = $token;
     $form->setAttribute('action', $this->url()->fromRoute($this->routes['password-recover']['name'], $this->routes['password-recover']['params'], $this->routes['password-recover']['options'], $this->routes['password-recover']['reuseMatchedParams']));
     $form->getInputFilter()->get('password-old')->setRequired(false);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $data = $form->getData();
             $credential = $credentialRepo->findOneBy(array($this->config['credentialIdentityProperty'] => $identity, 'type' => $this->config['credentialType']));
             $passwordNew = sha1(sha1($data['password-new']));
             $identity->setToken(sha1(uniqid(mt_rand(), true)));
             $credential->setValue($passwordNew);
             $this->entityManager->flush();
             $this->flashMessenger()->addSuccessMessage(_('Your password has been changed successfully!'));
             return $this->redirect()->toRoute($this->routes['signin']['name'], $this->routes['signin']['params'], $this->routes['signin']['options'], $this->routes['signin']['reuseMatchedParams']);
         } else {
             $this->flashMessenger()->addErrorMessage(_('Form with errors!'));
         }
     }
     $form->prepare();
     $viewModel = new ViewModel(['form' => $form, 'routes' => $this->routes]);
     $viewModel->setTemplate($this->templates['password-recover']);
     $this->layout($this->layoutView);
     return $viewModel;
 }