public function testInvoke_WithIncludeCurrentMessages() { $this->flashMessenger->addErrorMessage('ErrorMessage1')->addInfoMessage('InfoMessage2')->addSuccessMessage('SuccessMessage3')->addWarningMessage('WarningMessage4')->addMessage('Message5'); $result = $this->testedObject->__invoke(true); $expected = [FlashMessenger::NAMESPACE_ERROR => ['ErrorMessage1'], FlashMessenger::NAMESPACE_SUCCESS => ['SuccessMessage3'], FlashMessenger::NAMESPACE_INFO => ['InfoMessage2'], FlashMessenger::NAMESPACE_WARNING => ['WarningMessage4'], FlashMessenger::NAMESPACE_DEFAULT => ['Message5']]; $this->assertSame($expected, $result); }
/** * @return \Zend\Http\Response|ViewModel */ public function loginAction() { $data = $this->getRequest()->getPost(); $form = new Form\LoginForm(null, $this->getServiceLocator()); $flashMessenger = new FlashMessenger(); if ($this->getRequest()->isPost()) { // If you used another name for the authentication service, change it here $form->setData($this->getRequest()->getPost()); if ($form->isValid()) { /** * @var \User\Service\Auth $userAuth */ $userAuth = $this->getServiceLocator()->get('\\User\\Service\\Auth'); try { $userAuth->authenticateEquals($data['email'], $data['password']); $flashMessenger->addSuccessMessage('You\'re successfully logged in'); $session = new Container('location'); $location = $session->location; if ($location) { $session->getManager()->getStorage()->clear('location'); return $this->redirect()->toUrl($location); } return $this->redirect()->toRoute('home'); } catch (AuthException $exception) { $flashMessenger->addErrorMessage($exception->getMessage()); } } } return new ViewModel(array('form' => $form, 'serviceLocator' => $this->getServiceLocator())); }
public function seedMessages() { $helper = new FlashMessenger(); $helper->setSessionManager($this->session); $helper->addMessage('foo'); $helper->addMessage('bar'); $helper->addInfoMessage('bar-info'); $helper->addSuccessMessage('bar-success'); $helper->addErrorMessage('bar-error'); unset($helper); }
/** * This method represents an exception flashing system for backend without access to * rendering the template * * Just add an array value to $_ENV['exceptions'] inside any Backend class to flash it later .. * * If the key of appended array value will match any of User's registered institutions, it'll be * shown as related to that institution. * * @param \Zend\Mvc\Controller\Plugin\FlashMessenger $flashMessenger * * @return void */ protected function flashExceptions(\Zend\Mvc\Controller\Plugin\FlashMessenger $flashMessenger) { if (isset($_ENV['exceptions'])) { foreach ($_ENV['exceptions'] as $source => $exception) { // We actually cannot print multi-lined exception -> divide it into separate ones .. $exceptions = explode("\n", $exception); if ($exceptions == null) { // It is probably an array $exceptions = $exception; } foreach ($exceptions as $exception) { if (!is_numeric($source)) { $exception = $source . ':' . $exception; } $flashMessenger->addErrorMessage($exception); } } unset($_ENV['exceptions']); } }
public function handleErrorMessages($messages, FlashMessenger $messenger, $parentEl = null, $prefix = '') { if ($parentEl === null) { $parentEl = $this; } foreach ($messages as $name => $message) { if (is_string($message)) { $messenger->addErrorMessage($message); } else { $targetEl = $parentEl->get($name); if ($targetEl instanceof Fieldset) { $this->handleErrorMessages($message, $messenger, $targetEl, $prefix . $targetEl->getName() . ", "); } else { if ($targetEl instanceof Element) { $messenger->addErrorMessage($prefix . $targetEl->getLabel() . ": " . implode(", ", $message)); } } } } }
/** * @return \Zend\Http\Response|ViewModel */ public function changePasswordAction() { $currentPasswordElement = false; if ($this->identity()->getUser()->getEmail()) { $form = new ChangePasswordForm(null, ['serviceLocator' => $this->getServiceLocator()]); $currentPasswordElement = true; } else { $form = new ChangePasswordAndEmailForm(null, ['serviceLocator' => $this->getServiceLocator()]); } $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $flashMessenger = new FlashMessenger(); /** @var \User\Service\Auth $userAuth */ $userAuth = $this->getServiceLocator()->get('\\User\\Service\\Auth'); try { $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); /** @var \User\Entity\User $user */ $user = $objectManager->getRepository('User\\Entity\\User')->find($this->identity()->getUser()->getId()); if ($this->identity()->getUser()->getEmail()) { $userAuth->checkCredentials($this->identity()->getUser()->getEmail(), $form->getData()['currentPassword']); } else { $user->setEmail($form->getData()['email']); } $objectManager->persist($user); $objectManager->flush(); $userAuth->generateEquals($user, $form->getData()['password']); $flashMessenger->addSuccessMessage("You have successfully changed your password!"); return $this->redirect()->toRoute('home'); } catch (AuthException $exception) { $flashMessenger->addErrorMessage($exception->getMessage()); } } } return new ViewModel(['form' => $form, 'currentPasswordElement' => $currentPasswordElement]); }
/** * @return \Zend\Http\Response|ViewModel * @throws \Exception */ public function addAction() { $form = $this->getServiceLocator()->get('Comment\\Service\\Comment')->createForm(); // for POST data if ($this->getRequest()->isPost()) { $data = $this->getRequest()->getPost(); // for GET (or query string) data if ($this->getRequest()->getQuery('alias') && ($entityId = intval($this->getRequest()->getQuery('id')))) { $data->set('alias', $this->getRequest()->getQuery('alias')); $data->set('entityId', $this->getRequest()->getQuery('id')); } if (!isset($data['alias']) || !isset($data['entityId'])) { $this->getResponse()->setStatusCode(400); throw new \Exception('Bad request'); } $comment = $this->getServiceLocator()->get('Comment\\Service\\Comment')->add($form, $data); $flashMessenger = new FlashMessenger(); if ($comment) { $flashMessenger->addSuccessMessage('Comment created'); if (!$this->getRequest()->isXmlHttpRequest()) { if ($this->getRequest()->getHeader('Referer')) { return $this->redirect()->toUrl($this->getRequest()->getHeader('Referer')->getUri()); } else { return $this->redirect()->toUrl('/'); } } return; } else { $flashMessenger->addErrorMessage('Comment is not created'); } } $viewModel = new ViewModel(['form' => $form, 'title' => 'Add comment']); $viewModel->setTerminal($this->getRequest()->isXmlHttpRequest()); return $viewModel; }
/** * @param MvcEvent $event * @param Request $request * @param Response $response * @param FlashMessenger $flashMessenger * @param RouteInterface $router * @param Translator $translator * * @return bool */ public function checkAcl(MvcEvent $event, Request $request, Response $response, FlashMessenger $flashMessenger, RouteInterface $router, Translator $translator) { $role = $this->guestRoleName; if ($this->auth->hasIdentity()) { $role = $this->auth->getIdentity()->getRole(); if (!in_array($role, $this->allowedRoles)) { $role = $this->guestRoleName; } } $matchedRoute = $this->router->match($request); if (is_null($matchedRoute)) { return; } $params = $matchedRoute->getParams(); $module = array_key_exists('__NAMESPACE__', $params) ? $params['__NAMESPACE__'] : null; $controller = array_key_exists('controller', $params) ? $params['controller'] : null; $action = array_key_exists('action', $params) ? $params['action'] : null; if ($module && $controller) { $module .= '.'; } if ($action) { $controller .= '.'; } $resource = str_replace('\\Controller', '', $module) . $controller . $action; $resource = strtolower(str_replace('\\', '.', $resource)); if ($controller) { unset($params['controller']); } if ($action) { unset($params['action']); } if ($module) { unset($params['__NAMESPACE__']); } if (!$this->hasResource($resource)) { return true; if ($role == $this->guestRoleName) { $url = $router->assemble([], ['name' => 'aclGuestResourceNotFound']); } else { $url = $router->assemble([], ['name' => 'aclUserResourceNotFound']); } if (DEBUG) { $flashMessenger->addErrorMessage(sprintf($translator->translate('The requested resource %s does not exist'), $resource)); } else { $flashMessenger->addErrorMessage($translator->translate('The requested resource does not exist')); } $response->setStatusCode(302); if (DEBUG) { echo '<div style="margin:150px auto; width: 50%; text-align:center; font-size:18px;"><h3>DEBUG IS ENABLED' . '</h3><br />Normally would auto-redirect to:<br /><br /><a style="font-size:24px;" href="' . $url . '">' . $url . '</a>'; echo '</div>'; die; } header('location: ' . $url); $event->stopPropagation(); $event->setError('x'); return; } if (!$this->isAllowed($role, $resource)) { if (DEBUG) { $flashMessenger->addErrorMessage(sprintf($translator->translate('You (%s) are not allowed to access this resource: %s'), $role, $resource)); } else { $flashMessenger->addErrorMessage($translator->translate('You are not allowed to access this resource')); } if ($role == $this->guestRoleName) { $url = $router->assemble([], ['name' => 'aclGuestNotAllowed']); } else { $url = $router->assemble([], ['name' => 'aclUserNotAllowed']); } $response->setStatusCode(302); if (DEBUG) { echo '<div style="margin:150px auto; width: 50%; text-align:center; font-size:18px;"><h3>DEBUG IS ENABLED' . '</h3><br />Normally would auto-redirect to:<br /><br /><a style="font-size:24px;" href="' . $url . '">' . $url . '</a>'; echo '</div>'; die; } header('location: ' . $url); $event->stopPropagation(); $event->setError('x'); return; } return true; }
/** * onFailure * * Add a message to the flash messenger. * * @param AuthenticationEvent $event */ public function onFailure(AuthenticationEvent $event) { $this->flashMessenger->addErrorMessage($this->failureMessage); }