Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AccountActivationMailer and SendConfirmationEmailController #2493

Merged
merged 7 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions src/Api/Controller/SendConfirmationEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
namespace Flarum\Api\Controller;

use Flarum\Http\UrlGenerator;
use Flarum\Mail\Job\SendRawEmailJob;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\EmailToken;
use Flarum\User\AccountActivationMailerTrait;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Support\Arr;
Expand All @@ -24,6 +23,8 @@

class SendConfirmationEmailController implements RequestHandlerInterface
{
use AccountActivationMailerTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand Down Expand Up @@ -72,19 +73,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
throw new PermissionDeniedException;
}

$token = EmailToken::generate($actor->email, $actor->id);
$token->save();

$data = [
'{username}' => $actor->username,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
];

$body = $this->translator->trans('core.email.activate_account.body', $data);
$subject = $this->translator->trans('core.email.activate_account.subject');
$token = $this->generateToken($actor, $actor->email);
$data = $this->getEmailData($actor, $token);

$this->queue->push(new SendRawEmailJob($actor->email, $subject, $body));
$this->sendConfirmationEmail($actor, $data);

return new EmptyResponse;
}
Expand Down
42 changes: 5 additions & 37 deletions src/User/AccountActivationMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
namespace Flarum\User;

use Flarum\Http\UrlGenerator;
use Flarum\Mail\Job\SendRawEmailJob;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Event\Registered;
use Illuminate\Contracts\Queue\Queue;
use Symfony\Component\Translation\TranslatorInterface;

class AccountActivationMailer
{
use AccountActivationMailerTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand Down Expand Up @@ -60,42 +61,9 @@ public function handle(Registered $event)
return;
}

$data = $this->getEmailData($user, $user->email);

$body = $this->translator->trans('core.email.activate_account.body', $data);
$subject = '['.$data['{forum}'].'] '.$this->translator->trans('core.email.activate_account.subject');

$this->queue->push(new SendRawEmailJob($user->email, $subject, $body));
}

/**
* @param User $user
* @param string $email
* @return EmailToken
*/
protected function generateToken(User $user, $email)
{
$token = EmailToken::generate($email, $user->id);
$token->save();

return $token;
}

/**
* Get the data that should be made available to email templates.
*
* @param User $user
* @param string $email
* @return array
*/
protected function getEmailData(User $user, $email)
{
$token = $this->generateToken($user, $email);
$token = $this->generateToken($user, $user->email);
$data = $this->getEmailData($user, $token);

return [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
];
$this->sendConfirmationEmail($user, $data);
}
}
56 changes: 56 additions & 0 deletions src/User/AccountActivationMailerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\User;

use Flarum\Mail\Job\SendRawEmailJob;

trait AccountActivationMailerTrait
{
/**
* @param User $user
* @param string $email
* @return EmailToken
*/
protected function generateToken(User $user, $email)
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
{
$token = EmailToken::generate($email, $user->id);
$token->save();

return $token;
}

/**
* Get the data that should be made available to email templates.
*
* @param User $user
* @param EmailToken $token
* @return array
*/
protected function getEmailData(User $user, EmailToken $token)
{
return [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
];
}

/**
* @param User $user
* @param array $data
*/
protected function sendConfirmationEmail(User $user, $data)
{
$body = $this->translator->trans('core.email.activate_account.body', $data);
$subject = $this->translator->trans('core.email.activate_account.subject');

$this->queue->push(new SendRawEmailJob($user->email, $subject, $body));
}
}