Commit 2ac3e53d authored by Tino Goratsch's avatar Tino Goratsch

- reworked the error box handling so that the code provides a better testability

  --> TODO: Add the actual unit tests
- minor code optimizations
parent 060757a4
......@@ -9,10 +9,6 @@ use ACP3\Core;
*/
class Alerts
{
/**
* @var \ACP3\Core\Helpers\Output
*/
protected $outputHelper;
/**
* @var \ACP3\Core\View
*/
......@@ -25,16 +21,13 @@ class Alerts
/**
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\View $view
* @param \ACP3\Core\Helpers\Output $outputHelper
*/
public function __construct(
Core\Http\RequestInterface $request,
Core\View $view,
Core\Helpers\Output $outputHelper
Core\View $view
) {
$this->request = $request;
$this->view = $view;
$this->outputHelper = $outputHelper;
}
/**
......@@ -141,17 +134,7 @@ class Alerts
}
$this->view->assign('CONTENT_ONLY', $contentOnly);
$content = $this->view->fetchTemplate($this->errorBoxContent($errors));
if ($this->request->isAjax() === true) {
$return = [
'success' => false,
'content' => $content,
];
$this->outputHelper->outputJson($return);
}
return $content;
return $this->view->fetchTemplate($this->errorBoxContent($errors));
}
/**
......
<?php
namespace ACP3\Core\Helpers;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class Output
* @package ACP3\Core\Helpers
*/
class Output
{
/**
* @param array $data
*/
public function outputJson(array $data)
{
$response = new JsonResponse($data);
$response->send();
exit;
}
}
......@@ -8,6 +8,7 @@ namespace ACP3\Core\Modules\Helper;
use ACP3\Core;
use ACP3\Core\Controller\FrontendAction;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class Action
......@@ -83,10 +84,22 @@ class Action
$path
);
} catch (Core\Validation\Exceptions\ValidationFailedException $e) {
return [
'error_msg' => $this->alerts->errorBox($e->getMessage())
];
return $this->renderErrorBoxOnFailedFormValidation($e);
}
}
/**
* @param \Exception $exception
* @return array|JsonResponse
*/
private function renderErrorBoxOnFailedFormValidation(\Exception $exception)
{
$errors = $this->alerts->errorBox($exception->getMessage());
if ($this->request->isAjax()) {
return new JsonResponse(['success' => false, 'content' => $errors]);
}
return ['error_msg' => $errors];
}
/**
......
......@@ -42,7 +42,7 @@ class Redirect
public function toNewPage($url)
{
if ($this->request->isAjax() === true) {
return $this->ajax($url);
return $this->ajaxRedirect($url);
}
return new RedirectResponse($url);
......@@ -73,7 +73,7 @@ class Redirect
$path = $this->router->route($path, true);
if ($this->request->isAjax() === true) {
return $this->ajax($path);
return $this->ajaxRedirect($path);
}
$status = 302;
......@@ -85,13 +85,13 @@ class Redirect
}
/**
* Outputs a JSON response with redirect url
* Outputs a JSON response with a redirect url
*
* @param string $path
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
protected function ajax($path)
protected function ajaxRedirect($path)
{
$return = [];
if ($this->request->isAjax() === true) {
......
......@@ -11,7 +11,6 @@ services:
arguments:
- '@core.request'
- '@core.view'
- '@core.helpers.output'
core.helpers.date:
class: ACP3\Core\Helpers\Date
......@@ -33,9 +32,6 @@ services:
arguments:
- '@core.mailer'
core.helpers.output:
class: ACP3\Core\Helpers\Output
core.helpers.redirect:
class: ACP3\Core\Helpers\RedirectMessages
arguments:
......
......@@ -41,12 +41,12 @@ class ForgotPwd extends Core\Controller\FrontendAction
/**
* ForgotPwd constructor.
*
* @param \ACP3\Core\Controller\Context\FrontendContext $context
* @param \ACP3\Core\Helpers\FormToken $formTokenHelper
* @param \ACP3\Core\Helpers\Secure $secureHelper
* @param \ACP3\Modules\ACP3\Users\Model\UserRepository $userRepository
* @param \ACP3\Core\Controller\Context\FrontendContext $context
* @param \ACP3\Core\Helpers\FormToken $formTokenHelper
* @param \ACP3\Core\Helpers\Secure $secureHelper
* @param \ACP3\Modules\ACP3\Users\Model\UserRepository $userRepository
* @param \ACP3\Modules\ACP3\Users\Validation\AccountForgotPasswordFormValidation $accountForgotPasswordFormValidation
* @param \ACP3\Core\Helpers\SendEmail $sendEmail
* @param \ACP3\Core\Helpers\SendEmail $sendEmail
*/
public function __construct(
Core\Controller\Context\FrontendContext $context,
......@@ -95,44 +95,9 @@ class ForgotPwd extends Core\Controller\FrontendAction
function () use ($formData) {
$this->accountForgotPasswordFormValidation->validate($formData);
// Neues Passwort und neuen Zufallsschlüssel erstellen
$newPassword = $this->secureHelper->salt(Core\User::SALT_LENGTH);
$host = $this->request->getHostname();
// Je nachdem, wie das Feld ausgefüllt wurde, dieses auswählen
if ($this->get('core.validation.validation_rules.email_validation_rule')->isValid($formData['nick_mail']) === true &&
$this->userRepository->resultExistsByEmail($formData['nick_mail']) === true
) {
$user = $this->userRepository->getOneByEmail($formData['nick_mail']);
} else {
$user = $this->userRepository->getOneByNickname($formData['nick_mail']);
}
$seoSettings = $this->config->getSettings('seo');
// E-Mail mit dem neuen Passwort versenden
$subject = $this->translator->t(
'users',
'forgot_pwd_mail_subject',
[
'{title}' => $seoSettings['title'],
'{host}' => $host
]
);
$body = $this->translator->t(
'users',
'forgot_pwd_mail_message', [
'{name}' => $user['nickname'],
'{mail}' => $user['mail'],
'{password}' => $newPassword,
'{title}' => $seoSettings['title'],
'{host}' => $host
]
);
$settings = $this->config->getSettings('users');
$mailIsSent = $this->sendEmail->execute(substr($user['realname'], 0, -2), $user['mail'],
$settings['mail'], $subject, $body);
$user = $this->fetchUserByFormFieldValue($formData['nick_mail']);
$mailIsSent = $this->sendPasswordChangeEmail($user, $newPassword);
// Das Passwort des Benutzers nur abändern, wenn die E-Mail erfolgreich versendet werden konnte
if ($mailIsSent === true) {
......@@ -148,12 +113,70 @@ class ForgotPwd extends Core\Controller\FrontendAction
$this->formTokenHelper->unsetFormToken();
$this->setTemplate($this->get('core.helpers.alerts')->confirmBox(
$this->translator->t('users',
$mailIsSent === true && isset($bool) && $bool !== false ? 'forgot_pwd_success' : 'forgot_pwd_error'),
$this->translator->t(
'users',
$mailIsSent === true && isset($bool) && $bool !== false ? 'forgot_pwd_success' : 'forgot_pwd_error'
),
$this->appPath->getWebRoot()
));
},
$this->request->getFullPath()
);
}
/**
* @param string $nickNameOrEmail
* @return array
*/
protected function fetchUserByFormFieldValue($nickNameOrEmail)
{
if ($this->get('core.validation.validation_rules.email_validation_rule')->isValid($nickNameOrEmail) === true &&
$this->userRepository->resultExistsByEmail($nickNameOrEmail) === true
) {
$user = $this->userRepository->getOneByEmail($nickNameOrEmail);
} else {
$user = $this->userRepository->getOneByNickname($nickNameOrEmail);
}
return $user;
}
/**
* @param array $user
* @param string $newPassword
* @return bool
*/
protected function sendPasswordChangeEmail(array $user, $newPassword)
{
$host = $this->request->getHostname();
$seoSettings = $this->config->getSettings('seo');
$subject = $this->translator->t(
'users',
'forgot_pwd_mail_subject',
[
'{title}' => $seoSettings['title'],
'{host}' => $host
]
);
$body = $this->translator->t(
'users',
'forgot_pwd_mail_message', [
'{name}' => $user['nickname'],
'{mail}' => $user['mail'],
'{password}' => $newPassword,
'{title}' => $seoSettings['title'],
'{host}' => $host
]
);
$settings = $this->config->getSettings('users');
return $this->sendEmail->execute(
substr($user['realname'], 0, -2),
$user['mail'],
$settings['mail'],
$subject,
$body
);
}
}
......@@ -22,27 +22,37 @@ class Login extends Core\Controller\FrontendAction
*/
public function execute()
{
// Falls der Benutzer schon eingeloggt ist, diesen zur Startseite weiterleiten
if ($this->user->isAuthenticated() === true) {
return $this->redirect()->toNewPage($this->appPath->getWebRoot());
} elseif ($this->request->getPost()->isEmpty() === false) {
$result = $this->user->login(
$this->get('core.helpers.secure')->strEncode($this->request->getPost()->get('nickname', '')),
$this->request->getPost()->get('pwd', ''),
$this->request->getPost()->has('remember')
);
if ($result == 1) {
if ($this->request->getParameters()->has('redirect')) {
return $this->redirect()->temporary(base64_decode($this->request->getParameters()->get('redirect')));
}
return $this->executePost();
}
}
return $this->redirect()->toNewPage($this->appPath->getWebRoot());
/**
* @return array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost()
{
$result = $this->user->login(
$this->get('core.helpers.secure')->strEncode($this->request->getPost()->get('nickname', '')),
$this->request->getPost()->get('pwd', ''),
$this->request->getPost()->has('remember')
);
if ($result == 1) {
if ($this->request->getParameters()->has('redirect')) {
return $this->redirect()->temporary(
base64_decode($this->request->getParameters()->get('redirect'))
);
}
return [
'error_msg' => $this->get('core.helpers.alerts')->errorBox($this->translator->t('users',
$result == -1 ? 'account_locked' : 'nickname_or_password_wrong'))
];
return $this->redirect()->toNewPage($this->appPath->getWebRoot());
}
return [
'error_msg' => $this->get('core.helpers.alerts')->errorBox(
$this->translator->t('users', $result == -1 ? 'account_locked' : 'nickname_or_password_wrong')
)
];
}
}
......@@ -10,42 +10,12 @@ use ACP3\Installer\Core;
class Alerts extends \ACP3\Core\Helpers\Alerts
{
/**
* Gibt eine Box mit den aufgetretenen Fehlern aus
*
* @param string|array $errors
* @param bool $contentOnly
*
* @return string
* @inheritdoc
*/
public function errorBox($errors, $contentOnly = true)
{
$hasNonIntegerKeys = false;
$this->setErrorBoxData($errors);
if (is_string($errors) && ($data = @unserialize($errors)) !== false) {
$errors = $data;
}
if (is_array($errors) === true) {
foreach (array_keys($errors) as $key) {
if (is_numeric($key) === false) {
$hasNonIntegerKeys = true;
break;
}
}
} else {
$errors = (array)$errors;
}
$this->view->assign('error_box', ['non_integer_keys' => $hasNonIntegerKeys, 'errors' => $errors]);
$content = $this->view->fetchTemplate('error_box.tpl');
if ($this->request->isAjax() === true) {
$return = [
'success' => false,
'content' => $content,
];
$this->outputHelper->outputJson($return);
}
return $content;
return $this->view->fetchTemplate('error_box.tpl');
}
}
......@@ -4,7 +4,6 @@ services:
arguments:
- '@core.request'
- '@core.view'
- '@core.helpers.output'
core.helpers.date:
class: ACP3\Core\Helpers\Date
......@@ -21,9 +20,6 @@ services:
- '@core.lang'
- '@core.request'
core.helpers.output:
class: ACP3\Core\Helpers\Output
core.helpers.redirect:
class: ACP3\Core\Helpers\RedirectMessages
arguments:
......
......@@ -15,6 +15,7 @@ use ACP3\Installer\Core\Date;
use ACP3\Installer\Modules\Install\Controller\AbstractAction;
use ACP3\Installer\Modules\Install\Helpers\Install as InstallerHelpers;
use ACP3\Installer\Modules\Install\Validation\FormValidation;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class Index
......@@ -81,7 +82,7 @@ class Index extends AbstractAction
public function execute()
{
if ($this->request->getPost()->isEmpty() === false && !$this->request->getPost()->get('languages')) {
$this->executePost($this->request->getPost()->all());
return $this->executePost($this->request->getPost()->all());
}
$defaults = [
......@@ -104,6 +105,7 @@ class Index extends AbstractAction
/**
* @param array $formData
* @return array|JsonResponse
*/
private function executePost(array $formData)
{
......@@ -121,12 +123,26 @@ class Index extends AbstractAction
$this->setTemplate('install/install.result.tpl');
} catch (ValidationFailedException $e) {
$this->view->assign('error_msg', $this->get('core.helpers.alerts')->errorBox($e->getMessage()));
return $this->renderErrorBoxOnFailedFormValidation($e);
} catch (\Exception $e) {
$this->setTemplate('install/install.error.tpl');
}
}
/**
* @param \Exception $exception
* @return array|JsonResponse
*/
private function renderErrorBoxOnFailedFormValidation(\Exception $exception)
{
$errors = $this->get('core.helpers.alerts')->errorBox($exception->getMessage());
if ($this->request->isAjax()) {
return new JsonResponse(['success' => false, 'content' => $errors]);
}
return ['error_msg' => $errors];
}
/**
* @param array $formData
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment