(develop) Fix asset auto-incrementation (#6806)

* Fix auto-increment not updating

This is due to the addition of caching for settings.  If we're not
explicitly saving the Settings model, then the cache isn't getting
updated, causing the asset tag auto-increment to get an old cached
version with the wrong number

* Move Setting cache clear to an observer
This commit is contained in:
Martin Meredith 2019-03-13 17:58:35 +00:00 committed by snipe
parent a462e91983
commit f403db274a
6 changed files with 35 additions and 64 deletions

View file

@ -1,20 +0,0 @@
<?php
namespace App\Events;
use App\Models\Setting;
class SettingSaved
{
public $settings;
/**
* Create a new event instance.
*
* @param \App\Models\Setting $settings
*/
public function __construct(Setting $settings)
{
$this->settings = $settings;
}
}

View file

@ -3,13 +3,11 @@
namespace App\Models; namespace App\Models;
use Parsedown; use Parsedown;
use App\Events\SettingSaved;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
use Schema;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
/** /**
@ -42,15 +40,6 @@ class Setting extends Model
*/ */
protected $injectUniqueIdentifier = true; protected $injectUniqueIdentifier = true;
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'saved' => SettingSaved::class,
];
/** /**
* Model rules. * Model rules.
* *

View file

@ -59,6 +59,7 @@ class AssetObserver
{ {
if ($settings = Setting::getSettings()) { if ($settings = Setting::getSettings()) {
$settings->increment('next_auto_tag_base'); $settings->increment('next_auto_tag_base');
$settings->save();
} }
$logAction = new Actionlog(); $logAction = new Actionlog();

View file

@ -0,0 +1,23 @@
<?php
namespace App\Observers;
use App\Models\Setting;
use Illuminate\Support\Facades\Cache;
class SettingObserver
{
/**
* Listen to the Setting saved event.
*
* @param Setting $setting
*
* @return void
*/
public function saved(Setting $setting)
{
Cache::forget(Setting::APP_SETTINGS_KEY);
Cache::forget(Setting::SETUP_CHECK_KEY);
}
}

View file

@ -2,6 +2,8 @@
namespace App\Providers; namespace App\Providers;
use App\Models\Setting;
use App\Observers\SettingObserver;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Log; use Log;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
@ -41,8 +43,7 @@ class AppServiceProvider extends ServiceProvider
Component::observe(ComponentObserver::class); Component::observe(ComponentObserver::class);
Consumable::observe(ConsumableObserver::class); Consumable::observe(ConsumableObserver::class);
License::observe(LicenseObserver::class); License::observe(LicenseObserver::class);
Setting::observe(SettingObserver::class);
} }
/** /**

View file

@ -2,12 +2,8 @@
namespace App\Providers; namespace App\Providers;
use App\Models\Setting;
use App\Events\SettingSaved;
use App\Listeners\LogListener;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use App\Listeners\CheckoutableListener; use App\Listeners\CheckoutableListener;
use App\Listeners\LogListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
@ -18,15 +14,14 @@ class EventServiceProvider extends ServiceProvider
* @var array * @var array
*/ */
protected $listen = [ protected $listen = [
'Illuminate\Auth\Events\Login' => [
'App\Listeners\LogSuccessfulLogin',
],
'Illuminate\Auth\Events\Login' => [ 'Illuminate\Auth\Events\Failed' => [
'App\Listeners\LogSuccessfulLogin', 'App\Listeners\LogFailedLogin',
], ],
];
'Illuminate\Auth\Events\Failed' => [
'App\Listeners\LogFailedLogin',
],
];
/** /**
* The subscriber classes to register. * The subscriber classes to register.
@ -37,22 +32,4 @@ class EventServiceProvider extends ServiceProvider
LogListener::class, LogListener::class,
CheckoutableListener::class CheckoutableListener::class
]; ];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
/**
* Clear the LDAP settings cache when the settings model is saved
*/
Event::listen(SettingSaved::class, function () {
Cache::forget(Setting::APP_SETTINGS_KEY);
Cache::forget(Setting::SETUP_CHECK_KEY);
});
}
} }