GcConfig\Controller\UserController::forgotPasswordAction PHP Method

forgotPasswordAction() public method

Forgot password action
public forgotPasswordAction ( ) : Zend\View\Model\ViewModel | array
return Zend\View\Model\ViewModel | array
    public function forgotPasswordAction()
    {
        $this->layout()->setTemplate('layouts/one-page.phtml');
        $forgotPasswordForm = new UserForgotForm();
        $id = $this->getRouteMatch()->getParam('id');
        $key = $this->getRouteMatch()->getParam('key');
        if (!empty($id) and !empty($key)) {
            $userModel = User\Model::fromId($id);
            if ($userModel->getRetrievePasswordKey() == $key and strtotime('-1 hour') < strtotime($userModel->getRetrieveUpdatedAt())) {
                $forgotPasswordForm->setAttribute('action', $this->url()->fromRoute('config/user/forgot-password-key', array('id' => $id, 'key' => $key)));
                $forgotPasswordForm->initResetForm();
                if ($this->getRequest()->isPost()) {
                    $post = $this->getRequest()->getPost();
                    $forgotPasswordForm->getInputFilter()->get('password_confirm')->getValidatorChain()->addValidator(new Identical($post['password']));
                    $forgotPasswordForm->setData($post->toArray());
                    if ($forgotPasswordForm->isValid()) {
                        $userModel->setPassword($forgotPasswordForm->getValue('password'));
                        $userModel->setRetrievePasswordKey(null);
                        $userModel->setRetrieveUpdatedAt(null);
                        $userModel->save();
                        $this->flashMessenger()->addSuccessMessage('Password changed!');
                    }
                    return $this->redirect()->toRoute('config/user/login');
                }
                return array('form' => $forgotPasswordForm);
            }
            return $this->redirect()->toRoute('admin');
        } else {
            $forgotPasswordForm->setAttribute('action', $this->url()->fromRoute('config/user/forgot-password'));
            $forgotPasswordForm->initEmail();
            if ($this->getRequest()->isPost()) {
                $post = $this->getRequest()->getPost();
                $forgotPasswordForm->setData($post->toArray());
                if ($forgotPasswordForm->isValid()) {
                    $userModel = new User\Model();
                    if ($userModel->sendForgotPasswordEmail($forgotPasswordForm->getValue('email'))) {
                        $this->flashMessenger()->addSuccessMessage('Message sent, you have one hour to change your password!');
                        return $this->redirect()->toRoute('config/user/login');
                    }
                }
            }
        }
        return array('form' => $forgotPasswordForm);
    }