From 651d1c735b83cf01fed2ba8cb0607680284a5ba4 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 04a620d8e3..c4b9b89fbf 100644 --- a/app/Models/CompanyableTrait.php +++ b/app/Models/CompanyableTrait.php @@ -13,6 +13,13 @@ trait CompanyableTrait */ public static function bootCompanyableTrait() { - static::addGlobalScope(new CompanyableScope); + // In Version 7.0 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 dee2fbd271..6d92e55b44 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -18,17 +18,8 @@ use Watson\Validating\ValidatingTrait; 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;