public function loginAction()
 {
     if (!$this->getRequest()->isPost()) {
         return;
     }
     $loginForm = new LoginForm($this->getRequest()->getPost('loginForm'));
     if (!$loginForm->isValid()) {
         $this->nwFlashMessenger()->addErrorMessage(MessageConstants::ERROR_INVALID_FORM);
         return;
     }
     $loginFormData = $loginForm->getData();
     $authAdapter = $this->getAuthAdapter();
     $authAdapter->setIdentity($loginFormData['email'])->setCredential(NovumWareHelpers::encryptPassword($loginFormData['password']));
     $authenticationResult = $authAdapter->authenticate();
     if (!$authenticationResult->isValid()) {
         $this->nwFlashMessenger()->addErrorMessage('Invalid email / password combination');
         return;
     }
     $memberDataPrefixed = $authAdapter->getResultRowObject();
     $membersMapper = $this->getMembersMapper();
     $memberData = $membersMapper->unprefixDataArray($memberDataPrefixed);
     $memberModel = $membersMapper->createModelFromData($memberData);
     /*@var $memberModel \Registration\Model\MemberModel */
     $this->getAuthSession()->write($memberModel->toArray());
     $this->nwFlashMessenger()->addSuccessMessage('You have successfully logged in');
     if ($memberModel->role == 'admin') {
         return $this->redirect()->toRoute('admin');
     }
     $returnUrl = $this->getReturnUrl();
     if ($returnUrl) {
         return $this->redirect()->toUrl($returnUrl);
     } else {
         return $this->redirect()->toRoute('account');
     }
 }
 /**
  * Register a new member and send an email verification email.
  *
  * @param \Registration\Model\MemberModel $memberModel
  * @return \NovumWare\Process\ProcessResult ->data = Security key to use to verify email.
  */
 protected function _registerNewMember(MemberModel $memberModel)
 {
     $membersMapper = $this->getMembersMapper();
     $existingMemberModel = $membersMapper->fetchOneForEmail($memberModel->email);
     if ($existingMemberModel) {
         throw new ProcessException('A member already exists with that email address');
     }
     $memberModel->password = NovumWareHelpers::encryptPassword($memberModel->password);
     $membersMapper->insertModel($memberModel);
     $this->createEmailVerificationEmail($memberModel);
     return new ProcessResult(true);
 }
 public function testResetPassword()
 {
     $data = array('email' => '*****@*****.**', 'security_key' => 'udKdSEiRgIF3T11q6S5o8MmW07NlAS6P', 'password' => 'newPassword');
     $passwordResetModel = $this->getMockMemberPasswordResetsMapper()->createModelFromData($data);
     $memberModel = $this->getMockMembersMapper()->createModelFromData($data);
     $memberModelEncryptedPassword = $this->getMockMembersMapper()->createModelFromData($data);
     $memberModelEncryptedPassword->password = NovumWareHelpers::encryptPassword($data['password']);
     $this->getMockMemberPasswordResetsMapper()->shouldReceive('fetchOneForEmailAndSecurityKey')->with($data['email'], $data['security_key'])->andReturn($passwordResetModel)->once();
     $this->getMockMembersMapper()->shouldReceive('fetchOneForEmail')->with($data['email'])->andReturn($memberModel)->once();
     $this->getMockMembersMapper()->shouldReceive('updateModel')->with($this->compareModel($memberModelEncryptedPassword))->once();
     $this->getMockMemberPasswordResetsMapper()->shouldReceive('deleteModel')->with($passwordResetModel)->once();
     $returnedProcessResult = $this->forgotPasswordProcess->resetPassword($data['email'], $data['security_key'], $data['password']);
     $this->assertEquals($this->getProcessResultSuccess(), $returnedProcessResult);
 }
 /**
  * @param string $email
  * @param string $securityKey
  * @param string $newPassword
  * @return \NovumWare\Process\ProcessResult
  */
 protected function _resetPassword($email, $securityKey, $newPassword)
 {
     $passwordResetsMapper = $this->getMemberPasswordResetsMapper();
     $passwordResetModel = $passwordResetsMapper->fetchOneForEmailAndSecurityKey($email, $securityKey);
     if (!$passwordResetModel) {
         throw new ProcessException('Could not verify your email address, please return to the email and click the link again');
     }
     $membersMapper = $this->getMembersMapper();
     $memberModel = $membersMapper->fetchOneForEmail($email);
     if (!$memberModel) {
         throw new \Exception("Could not find member with email: {$email}");
     }
     $memberModel->password = NovumWareHelpers::encryptPassword($newPassword);
     $membersMapper->updateModel($memberModel);
     $passwordResetsMapper->deleteModel($passwordResetModel);
 }
 public function testLoginActionValid()
 {
     $dataPost = array('loginForm' => array('email' => '*****@*****.**', 'password' => 'rightPassword'));
     $dataMember = array('status' => StatusConstants::MEMBER_PENDING_EMAIL_VERIFICATION, 'role' => RoleConstants::MEMBER, 'read_terms' => false, 'email' => $dataPost['loginForm']['email'], 'password' => NovumWareHelpers::encryptPassword($dataPost['loginForm']['password']));
     $authenticationResult = Mockery::mock('\\Zend\\Authentication\\Result');
     $tempMemberModel = $this->getMockMembersMapper()->createModelFromData($dataMember);
     $mockResultObject = (object) $this->getMockMembersMapper()->prefixDataArray($tempMemberModel->toArray());
     $expectedWriteData = $tempMemberModel->toArray();
     $this->mockAuthSession->shouldReceive('clear')->once();
     $this->mockAuthAdapter->shouldReceive('setIdentity')->with($dataPost['loginForm']['email'])->once()->andReturn($this->mockAuthAdapter);
     $this->mockAuthAdapter->shouldReceive('setCredential')->with(NovumWareHelpers::encryptPassword($dataPost['loginForm']['password']))->once();
     $this->mockAuthAdapter->shouldReceive('authenticate')->andReturn($authenticationResult)->once();
     $authenticationResult->shouldReceive('isValid')->andReturn(true)->once();
     $this->mockAuthAdapter->shouldReceive('getResultRowObject')->andReturn($mockResultObject)->once();
     $this->mockAuthSession->shouldReceive('write')->with($this->compareArray($expectedWriteData))->once();
     $this->mockFlashMessenger->shouldReceive('addSuccessMessage')->with('You have successfully logged in')->once();
     $this->dispatch('/login', 'POST', $dataPost);
     $this->assertResponseStatusCode(302);
     $this->assertRedirectTo('/account');
 }