From abae86511c90104f39e894a3cfa28a2ddfbfc6e8 Mon Sep 17 00:00:00 2001 From: Tobias Regnery Date: Wed, 13 Sep 2023 10:54:23 +0200 Subject: [PATCH] Another slightly less ugly way for backward compatibility Instead of using a constructor, add a special check in the boot-method for locations. This seems to fit better in the system and does hopefully not break the existing tests. Signed-off-by: Tobias Regnery --- app/Models/CompanyableTrait.php | 9 ++++++++- app/Models/Location.php | 11 +---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/Models/CompanyableTrait.php b/app/Models/CompanyableTrait.php index b03b346d2ed8..884324a02b71 100644 --- a/app/Models/CompanyableTrait.php +++ b/app/Models/CompanyableTrait.php @@ -11,6 +11,13 @@ trait CompanyableTrait */ public static function bootCompanyableTrait() { - static::addGlobalScope(new CompanyableScope); + // In Version 6 and before locations weren't scoped by companies, so add a check for the backward compatibility setting + if (__CLASS__ != 'App\Models\Location') { + static::addGlobalScope(new CompanyableScope); + } else { + if (Setting::getSettings()->scope_locations_fmcs == 1) { + static::addGlobalScope(new CompanyableScope); + } + } } } diff --git a/app/Models/Location.php b/app/Models/Location.php index 7d2dd0365599..859e36d60fc5 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -18,17 +18,8 @@ class Location extends SnipeModel { - function __construct() { - parent::__construct(); - // This is a workaround for backward compatibility with older versions where locations doesn't get scoped. - // Normaly we would only add 'use CompanyableTrait;', but this has to be conditional on the setting. - // So instead of using the trait, add the scope directly if no backward compatibility is used - if (Setting::getSettings()->scope_locations_fmcs) { - static::addGlobalScope(new CompanyableScope); - } - } - use HasFactory; + use CompanyableTrait; protected $presenter = \App\Presenters\LocationPresenter::class; use Presentable;