Merge pull request #1032 from dav-is/patch-1

Prevent deletion of default/all locale(s)
This commit is contained in:
Franz Liedke 2017-02-03 20:21:19 +01:00 committed by GitHub
commit 59226000da
5 changed files with 111 additions and 2 deletions

View File

@ -106,6 +106,7 @@ class CoreServiceProvider extends AbstractServiceProvider
$events->subscribe('Flarum\Core\Listener\DiscussionMetadataUpdater');
$events->subscribe('Flarum\Core\Listener\UserMetadataUpdater');
$events->subscribe('Flarum\Core\Listener\ExtensionValidator');
$events->subscribe('Flarum\Core\Listener\EmailConfirmationMailer');
$events->subscribe('Flarum\Core\Listener\DiscussionRenamedNotifier');

View File

@ -0,0 +1,42 @@
<?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\Core\Listener;
use Flarum\Event\ExtensionWillBeDisabled;
use Flarum\Http\Exception\MethodNotAllowedException;
use Illuminate\Contracts\Events\Dispatcher;
class ExtensionValidator
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ExtensionWillBeDisabled::class, [$this, 'whenExtensionWillBeDisabled']);
}
/**
* @param ExtensionWillBeDisabled $event
* @throws MethodNotAllowedException
*/
public function whenExtensionWillBeDisabled(ExtensionWillBeDisabled $event)
{
if (in_array('flarum-locale', $event->extension->extra)) {
$default_locale = $this->app->make('flarum.settings')->get('default_locale');
$locale = array_get($event->extension->extra, 'flarum-locale.code');
if ($locale === $default_locale) {
throw new MethodNotAllowedException('You cannot disable the default language pack!');
}
}
}
}

View File

@ -0,0 +1,30 @@
<?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\Event;
use Flarum\Extension\Extension;
class ExtensionWillBeDisabled
{
/**
* @var string
*/
protected $extension;
/**
* @param Extension $extension
*/
public function __construct(Extension $extension)
{
$this->extension = $extension;
}
}

View File

@ -0,0 +1,30 @@
<?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\Event;
use Flarum\Extension\Extension;
class ExtensionWillBeEnabled
{
/**
* @var string
*/
protected $extension;
/**
* @param Extension $extension
*/
public function __construct(Extension $extension)
{
$this->extension = $extension;
}
}

View File

@ -15,6 +15,8 @@ use Flarum\Database\Migrator;
use Flarum\Event\ExtensionWasDisabled;
use Flarum\Event\ExtensionWasEnabled;
use Flarum\Event\ExtensionWasUninstalled;
use Flarum\Event\ExtensionWillBeDisabled;
use Flarum\Event\ExtensionWillBeEnabled;
use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
@ -112,6 +114,8 @@ class ExtensionManager
{
if (! $this->isEnabled($name)) {
$extension = $this->getExtension($name);
$this->dispatcher->fire(new ExtensionWillBeEnabled($extension));
$enabled = $this->getEnabled();
@ -139,9 +143,11 @@ class ExtensionManager
$enabled = $this->getEnabled();
if (($k = array_search($name, $enabled)) !== false) {
unset($enabled[$k]);
$extension = $this->getExtension($name);
$this->dispatcher->fire(new ExtensionWillBeDisabled($extension));
unset($enabled[$k]);
$this->setEnabled($enabled);