Don't resolve services when binding listeners

Refs #1578.
This commit is contained in:
Franz Liedke 2018-12-13 21:47:01 +01:00
parent cb3baf9955
commit ababb8ebef
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
6 changed files with 132 additions and 55 deletions

View File

@ -30,6 +30,7 @@ use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator;
use Flarum\Locale\LocaleManager;
use Flarum\Settings\Event\Saved;
use Flarum\Settings\Event\Saving;
use Flarum\Settings\SettingsRepositoryInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Zend\Stratigility\MiddlewarePipe;
@ -141,15 +142,26 @@ class ForumServiceProvider extends AbstractServiceProvider
$this->app->make(LocaleManager::class)
);
$recompile->whenSettingsSaved($event);
$validator = new ValidateCustomLess(
$this->app->make('flarum.assets.forum'),
$this->app->make('flarum.locales'),
$this->app
);
$validator->whenSettingsSaved($event);
}
);
$events->subscribe(
new ValidateCustomLess(
$this->app->make('flarum.assets.forum'),
$this->app->make('flarum.locales'),
$this->app
)
$events->listen(
Saving::class,
function (Saving $event) {
$validator = new ValidateCustomLess(
$this->app->make('flarum.assets.forum'),
$this->app->make('flarum.locales'),
$this->app
);
$validator->whenSettingsSaving($event);
}
);
}

View File

@ -19,7 +19,6 @@ use Flarum\Settings\Event\Saving;
use Flarum\Settings\OverrideSettingsRepository;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\Adapter\NullAdapter;
use League\Flysystem\Filesystem;
@ -54,12 +53,6 @@ class ValidateCustomLess
$this->container = $container;
}
public function subscribe(Dispatcher $events)
{
$events->listen(Saving::class, [$this, 'whenSettingsSaving']);
$events->listen(Saved::class, [$this, 'whenSettingsSaved']);
}
public function whenSettingsSaving(Saving $event)
{
if (! isset($event->settings['custom_less'])) {

View File

@ -0,0 +1,105 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\User;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Event\Registered;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Mail\Message;
class AccountActivationMailer
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var Mailer
*/
protected $mailer;
/**
* @var UrlGenerator
*/
protected $url;
/**
* @var Translator
*/
protected $translator;
/**
* @param \Flarum\Settings\SettingsRepositoryInterface $settings
* @param Mailer $mailer
* @param UrlGenerator $url
* @param Translator $translator
*/
public function __construct(SettingsRepositoryInterface $settings, Mailer $mailer, UrlGenerator $url, Translator $translator)
{
$this->settings = $settings;
$this->mailer = $mailer;
$this->url = $url;
$this->translator = $translator;
}
public function handle(Registered $event)
{
$user = $event->user;
if ($user->is_email_confirmed) {
return;
}
$data = $this->getEmailData($user, $user->email);
$body = $this->translator->trans('core.email.activate_account.body', $data);
$this->mailer->raw($body, function (Message $message) use ($user, $data) {
$message->to($user->email);
$message->subject('['.$data['{forum}'].'] '.$this->translator->trans('core.email.activate_account.subject'));
});
}
/**
* @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);
return [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
];
}
}

View File

@ -14,8 +14,6 @@ namespace Flarum\User;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Event\EmailChangeRequested;
use Flarum\User\Event\Registered;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Mail\Message;
@ -50,34 +48,7 @@ class EmailConfirmationMailer
$this->translator = $translator;
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Registered::class, [$this, 'whenUserWasRegistered']);
$events->listen(EmailChangeRequested::class, [$this, 'whenUserEmailChangeWasRequested']);
}
public function whenUserWasRegistered(Registered $event)
{
$user = $event->user;
if ($user->is_email_confirmed) {
return;
}
$data = $this->getEmailData($user, $user->email);
$body = $this->translator->trans('core.email.activate_account.body', $data);
$this->mailer->raw($body, function (Message $message) use ($user, $data) {
$message->to($user->email);
$message->subject('['.$data['{forum}'].'] '.$this->translator->trans('core.email.activate_account.subject'));
});
}
public function whenUserEmailChangeWasRequested(EmailChangeRequested $event)
public function handle(EmailChangeRequested $event)
{
$email = $event->email;
$data = $this->getEmailData($event->user, $email);

View File

@ -14,24 +14,15 @@ namespace Flarum\User;
use Flarum\Group\Group;
use Flarum\User\Event\Saving;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Events\Dispatcher;
class SelfDemotionGuard
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Saving::class, [$this, 'whenUserWillBeSaved']);
}
/**
* Prevent an admin from removing their admin permission via the API.
* @param Saving $event
* @throws PermissionDeniedException
*/
public function whenUserWillBeSaved(Saving $event)
public function handle(Saving $event)
{
// Non-admin users pose no problem
if (! $event->actor->isAdmin()) {

View File

@ -14,6 +14,9 @@ namespace Flarum\User;
use Flarum\Event\ConfigureUserPreferences;
use Flarum\Event\GetPermission;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\User\Event\EmailChangeRequested;
use Flarum\User\Event\Registered;
use Flarum\User\Event\Saving;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Factory;
@ -86,8 +89,10 @@ class UserServiceProvider extends AbstractServiceProvider
$events = $this->app->make('events');
$events->subscribe(SelfDemotionGuard::class);
$events->subscribe(EmailConfirmationMailer::class);
$events->listen(Saving::class, SelfDemotionGuard::class);
$events->listen(Registered::class, AccountActivationMailer::class);
$events->listen(EmailChangeRequested::class, EmailConfirmationMailer::class);
$events->subscribe(UserMetadataUpdater::class);
$events->subscribe(UserPolicy::class);