public function testVerifyEmailActionWithValidToken() { $user = UserService::findOneByUsername('testuser'); $user->setActive(false); UserService::update(); $this->assertFalse($user->getActive()); $verificationToken = sha1(mt_rand() . $user->getEmail() . mt_rand()); $emailVerification = UserEmailVerificationService::create(array('user' => $user, 'token' => $verificationToken, 'requestDate' => new DateTime())); $this->dispatch('/user/verifyemail?token=' . $verificationToken); $this->assertRedirect('/login', 'Failed to redirect after verifying email.'); $this->assertTrue($user->getActive()); }
/** * Send email address verification email to user * * @param User $user * @param Zend_Mail_Transport_Abstract $transport [Optional] Zend mail transport class * @return void */ public static function sendVerificationEmail(User $user, Zend_Mail_Transport_Abstract $transport = null) { $serverUrlHelper = new Zend_View_Helper_ServerUrl(); $urlHelper = HelperBroker::getStaticHelper('url'); $siteDomain = preg_replace('/^https?:\\/\\//', '', $serverUrlHelper->serverUrl()); $siteName = Zend_Registry::get('siteName'); $config = Zend_Registry::get('config'); $from = 'noreply@' . $siteDomain; if (!empty($config->mail) && !empty($config->mail->from)) { $from = $config->mail->from; } if (null === $transport) { if (Zend_Session::$_unitTestEnabled) { $transport = new MockMailTransport(); } else { if (!empty($config->mail) && !empty($config->mail->smtp) && !empty($config->mail->smtp->host)) { $options = $config->mail->smtp->toArray(); unset($options['host']); $transport = new Zend_Mail_Transport_Smtp($config->mail->smtp->host, $options); } } } UserEmailVerificationService::collectGarbage(); // @todo cronjob?; should also remove any unverified user accounts $verificationToken = sha1(mt_rand() . $user->getEmail() . mt_rand()); if (APPLICATION_ENV === 'testing') { $verificationLink = $serverUrlHelper->serverUrl() . '/verifyEmail/' . $verificationToken; } else { // @codeCoverageIgnoreStart $verificationLink = $serverUrlHelper->serverUrl() . $urlHelper->url(array('token' => $verificationToken), 'verifyEmail'); } // @codeCoverageIgnoreEnd UserEmailVerificationService::create(new UserEmailVerification(array('user' => $user, 'token' => $verificationToken, 'requestDate' => new DateTime()))); $text = 'Hello ' . $user->getUsername() . ', Thank you for registering with ' . $siteName . '. To activate your account and complete the registration process, please click the following link: ' . $verificationLink . '. You are receiving this email because someone recently registered on our site and provided <' . $user->getEmail() . '> as their ema il address. If you did not recently register at ' . $siteDomain . ', then please ignore this email. Your information will be remov ed from our system within 24 hours. Thank you, The ' . $siteName . ' Team '; $html = '<p>Hello ' . $user->getUsername() . ',</p> <p>Thank you for registering with ' . $siteName . '. To activate your account and complete the registration process, please click the following link: <a href="' . $verificationLink . '" title="Verify your email address">' . $verificationLink . '</a>.</p> <p>You are receiving this email because someone recently registered on our site and provided <' . $user->getEmail() . '> as their email address. If you did not recently register at ' . $siteDomain . ', then please ignore this email. Your information will be removed from our system within 24 hours.</p> <p>Thank you,<br> The ' . $siteName . ' Team</p> '; try { Logger::info('Attempting to send email to \'' . $user->getEmail() . '\'.'); $mail = new Zend_Mail('utf-8'); $mail->setFrom($from, $siteName)->setSubject('[' . $siteName . '] Email Verification')->setBodyText($text)->setBodyHtml($html)->addTo($user->getEmail()); $mail->send($transport); } catch (Exception $e) { Logger::crit($e->getMessage()); throw $e; } }
public function testGarbageCollection() { $testEmailVerification = UserEmailVerificationTest::createOldTestEmailVerification(); $user = $testEmailVerification->getUser(); #self::$entityManager->persist($user->getRole()); #self::$entityManager->persist($user); #self::$entityManager->flush(); UserEmailVerificationService::create($testEmailVerification); $entities = self::$entityManager->createQuery('SELECT e FROM Rexmac\\Zyndax\\Entity\\UserEmailVerification e')->execute(); $this->assertEquals(1, count($entities)); UserEmailVerificationService::collectGarbage(); $entities = self::$entityManager->createQuery('SELECT e FROM Rexmac\\Zyndax\\Entity\\UserEmailVerification e')->execute(); $this->assertEquals(0, count($entities)); }
/** * Email verification action * * @return void */ public function verifyemailAction() { $verificationToken = $this->getRequest()->getParam('token', null); if (null === $verificationToken || '' == $verificationToken) { throw new UserControllerException('Invalid verification token'); } if (null == ($emailVerification = UserEmailVerificationService::findOneByToken($verificationToken))) { throw new UserControllerException('Unknown verification token'); } // Update user $user = $emailVerification->getUser(); $user->setActive(true); UserService::update(); // Track changes UserEditEventService::create(array('user' => $user, 'editor' => $user, 'ip' => $this->getRequest()->getServer('REMOTE_ADDR'), 'date' => new DateTime(), 'description' => 'Email verification: ' . $user->getEmail())); // Delete sender verification record UserEmailVerificationService::delete($emailVerification); // Redirect to login page $this->_helper->sessionMessenger('Email address verified successfully. You may now login.', 'success'); return $this->getHelper('Redirector')->gotoRoute(array(), 'login'); }