Skip to content

Commit d864868

Browse files
committed
Cleaned up
1 parent 0b234e1 commit d864868

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

config/sync/symfony_mailer.mailer_policy.hoeringsportal_anonymous_edit.content_recover.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ id: hoeringsportal_anonymous_edit.content_recover
88
configuration:
99
email_body:
1010
content:
11-
value: "Gå til <a href=\"{{ url }}\">{{ url }}</a> for at finde dit eget indhold på deltag.aarhus.dk.\r\n"
11+
value: "Gå til <a href=\"{{ recover_url }}\">{{ recover_url }}</a> for at finde dit eget indhold på deltag.aarhus.dk.\r\n"
1212
format: email_html
1313
email_subject:
1414
value: 'Dit indhold på deltag.aarhus.dk'

web/modules/custom/hoeringsportal_anonymous_edit/src/Form/RecoverForm.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function getFormId(): string {
3030
* {@inheritdoc}
3131
*/
3232
public function buildForm(array $form, FormStateInterface $form_state): array {
33-
3433
$form['email'] = [
3534
'#type' => 'email',
3635
'#title' => $this->t('Confirm your email address', options: ['context' => 'hoeringsportal_anonymous_edit']),
@@ -54,12 +53,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
5453
public function validateForm(array &$form, FormStateInterface $form_state): void {
5554
$email = $form_state->getValue('email');
5655
$token = $this->getRouteMatch()->getParameter('token');
57-
try {
58-
$this->helper->setTokenByEmail($email, $token);
59-
60-
$form_state->setRedirect('hoeringsportal_anonymous_edit.content');
61-
}
62-
catch (\Exception) {
56+
if (!$this->helper->isValidTokenEmail($email, $token)) {
6357
$form_state->setErrorByName('email', $this->t('Invalid email address'));
6458
}
6559
}
@@ -68,7 +62,16 @@ public function validateForm(array &$form, FormStateInterface $form_state): void
6862
* {@inheritdoc}
6963
*/
7064
public function submitForm(array &$form, FormStateInterface $form_state): void {
71-
// Everything has been done in validateForm.
65+
$email = $form_state->getValue('email');
66+
$token = $this->getRouteMatch()->getParameter('token');
67+
try {
68+
$this->helper->setTokenByEmail($email, $token);
69+
$this->messenger()->addMessage('Your edit token has been recovered');
70+
}
71+
catch (\Exception) {
72+
$this->messenger()->addError('Error recovering your edit token');
73+
}
74+
$form_state->setRedirect('hoeringsportal_anonymous_edit.content');
7275
}
7376

7477
}

web/modules/custom/hoeringsportal_anonymous_edit/src/Form/RequestForm.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function getFormId(): string {
3030
* {@inheritdoc}
3131
*/
3232
public function buildForm(array $form, FormStateInterface $form_state): array {
33-
3433
$form['email'] = [
3534
'#type' => 'email',
3635
'#title' => $this->t('Your email address', options: ['context' => 'hoeringsportal_anonymous_edit']),
@@ -57,7 +56,7 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
5756
try {
5857
$this->helper->sendRecoverUrl($email);
5958

60-
$this->messenger()->addMessage($this->t('Email send to @email', ['@email' => $email]));
59+
$this->messenger()->addMessage($this->t('Email sent to @email', ['@email' => $email]));
6160
// @todo Redirect to where?
6261
$form_state->setRedirect('<front>');
6362
}

web/modules/custom/hoeringsportal_anonymous_edit/src/Helper/Helper.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Drupal\Core\Url;
1818
use Drupal\hoeringsportal_anonymous_edit\Event\HoeringsportalAnonymousEditEvent;
1919
use Drupal\hoeringsportal_anonymous_edit\Exception\InvalidTokenException;
20+
use Drupal\hoeringsportal_anonymous_edit\Model\Item;
2021
use Psr\Log\LoggerAwareInterface;
2122
use Psr\Log\LoggerAwareTrait;
2223
use Psr\Log\LoggerInterface;
@@ -141,18 +142,33 @@ public function setToken(string $token): void {
141142
}
142143

143144
/**
144-
* Set token ny email.
145+
* Decide if email address matches a token.
146+
*/
147+
public function isValidTokenEmail(string $email, string $token): bool {
148+
return NULL !== $this->fetchItemByEmail($email, $token);
149+
}
150+
151+
/**
152+
* Set token by email.
145153
*/
146154
public function setTokenByEmail(string $email, string $token): void {
147-
$items = $this->itemHelper->fetchItemsByEmail($email, $token);
148-
$item = reset($items);
155+
$item = $this->fetchItemByEmail($email, $token);
149156
if (!$item) {
150157
throw new InvalidTokenException($email);
151158
}
152159

153160
$this->setToken($item->owner_token);
154161
}
155162

163+
/**
164+
* Fetch item by email.
165+
*/
166+
private function fetchItemByEmail(string $email, string $token): ?Item {
167+
$items = $this->itemHelper->fetchItemsByEmail($email, $token);
168+
169+
return reset($items) ?: NULL;
170+
}
171+
156172
/**
157173
* Get recover URL.
158174
*/

0 commit comments

Comments
 (0)