public function checkTokenAction() { $service = $this->getEmailVerificationService(); $events = $this->getServiceLocator()->get('SharedEventManager'); $service->cleanExpiredVerificationRequests(); // Pull and validate the Request Key $token = $this->plugin('params')->fromRoute('token'); $validator = new \Zend\Validator\Hex(); if (!$validator->isValid($token)) { throw new \InvalidArgumentException('Invalid Token!'); } // Find the request key in ze database $model = $service->findByRequestKey($token); if (!$model instanceof EvrModel) { throw new \InvalidArgumentException('Invalid Token!'); } // Listen for registration completion and delete the email verification record if the // user account was registered successfully $events->attach('ZfcUser\\Service\\User', 'register.post', function ($e) use($service, $model) { $user = $e->getParam('user'); if ($user instanceof \ZfcUser\Entity\UserInterface && !is_null($user->getID())) { $service->remove($model); } }); // Ensure that the email address wasn't changed on the client side before POSTing if ($this->getRequest()->isPost()) { $this->getRequest()->getPost()->set('email', $model->getEmailAddress()); } // Hook into existing form processing logic $vm = $this->forward()->dispatch('zfcuser', array('action' => 'register')); if ($vm instanceof Response) { $zfcUserAction = $this->url()->fromRoute('zfcuser/register'); $stepTwoRoute = $this->url()->fromRoute('zfcuser/register/step2', array('token' => $token)); // Intercept form validation failure redirects from ZfcUser and change the URI // to point to this controller action $allHeaders = $this->getResponse()->getHeaders(); $locationHeader = $allHeaders->get('Location'); if ($locationHeader->getUri() == $zfcUserAction) { $locationHeader->setUri($stepTwoRoute); } return $vm; } // Defeat ZfcUser's attempt to render it's own view script // (necessary because it doesn't allow changing the form action) $vm->setVariable('model', $model); $vm->setTemplate('cdli-twostagesignup/register'); return $vm; }
public function checkTokenAction() { $model = new ViewModel(); $model->setTemplate('zfc-user/user/checktoken'); $userService = $this->getSkelletonUserService(); // remove old, not verified records $userService->cleanExpiredVerificationRequests(); // Pull and validate the Request Key $token = $this->getEvent()->getRouteMatch()->getParam('token', false); if (!$token) { $model->setVariables(array('success' => false, 'message' => gettext_noop('Invalid Token!'))); return $model; } $validator = new \Zend\Validator\Hex(); if (!$validator->isValid($token)) { $model->setVariables(array('success' => false, 'message' => gettext_noop('Invalid Token!'))); return $model; } // Find the token in DB $users = $userService->findByToken($token); if (count($users) !== 1) { $model->setVariables(array('success' => false, 'message' => gettext_noop('Invalid Token!'))); return $model; } $user = $users[0]; if (!$user instanceof User) { $model->setVariables(array('success' => false, 'message' => gettext_noop('Invalid Token!'))); return $model; } if ($user->isEmailVerified()) { $model->setVariables(array('success' => true, 'activated' => $user->isActive())); return $model; } /* @var $options SiteRegistrationOptions */ $options = $this->getServiceLocator()->get(SiteRegistrationOptions::class); $flag = $options->getRegistrationMethodFlag(); $user->setEmailIsVerified(true); $variables = array('success' => true, 'activated' => false); if (!($flag & SiteRegistrationOptions::REGISTRATION_METHOD_MODERATOR_CONFIRM) || $flag & SiteRegistrationOptions::REGISTRATION_METHOD_AUTO_ENABLE) { $user->setIsActive(true); $variables['activated'] = true; } $this->getEntityManager()->flush(); // send user notifications $notificationService = $this->getNotificationService(); $notificationService->notifyUser($user, UserNotificationService::EVENT_TOKEN); $model->setVariables($variables); return $model; }