Add a backward compatibility setting for locations with companies

Now that locations have a company_id they get restricted to the users company with FullMultipleCompanySupport.
This breaks backward compatibility, because before everyone can handle locations without restrictions.
Add a setting right below FullMultipleCompanySupport so that everyone can switch to the desired behaviour.
The default is off and the existing behaviour is preserved.
This commit is contained in:
Tobias Regnery 2023-02-27 13:54:41 +01:00
parent 1ccbf8942c
commit 1318dc6111
9 changed files with 108 additions and 9 deletions

View file

@ -11,6 +11,7 @@ use App\Http\Transformers\SelectlistTransformer;
use App\Models\Asset;
use App\Models\Company;
use App\Models\Location;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
@ -77,7 +78,10 @@ class LocationsController extends Controller
->withCount('children as children_count')
->withCount('users as users_count');
$locations = Company::scopeCompanyables($locations);
// Only scope locations if the setting is enabled
if (Setting::getSettings()->scope_locations_fmcs) {
$locations = Company::scopeCompanyables($locations);
}
if ($request->filled('search')) {
$locations = $locations->TextSearch($request->input('search'));
@ -159,9 +163,13 @@ class LocationsController extends Controller
$this->authorize('create', Location::class);
$location = new Location;
$location->fill($request->all());
$location->company_id = Company::getIdForCurrentUser($request->get('company_id'));
$location = $request->handleImages($location);
// Only scope location if the setting is enabled
if (Setting::getSettings()->scope_locations_fmcs) {
$location->company_id = Company::getIdForCurrentUser($request->get('company_id'));
}
if ($location->save()) {
return response()->json(Helper::formatStandardApiResponse('success', (new LocationsTransformer)->transformLocation($location), trans('admin/locations/message.create.success')));
}
@ -223,7 +231,12 @@ class LocationsController extends Controller
$location = $request->handleImages($location);
if ($request->filled('company_id')) {
$location->company_id = Company::getIdForCurrentUser($request->get('company_id'));
// Only scope location if the setting is enabled
if (Setting::getSettings()->scope_locations_fmcs) {
$location->company_id = Company::getIdForCurrentUser($request->get('company_id'));
} else {
$location->company_id = $request->get('company_id');
}
}
if ($location->isValid()) {
@ -322,7 +335,10 @@ class LocationsController extends Controller
'locations.image',
]);
$locations = Company::scopeCompanyables($locations);
// Only scope locations if the setting is enabled
if (Setting::getSettings()->scope_locations_fmcs) {
$locations = Company::scopeCompanyables($locations);
}
$page = 1;
if ($request->filled('page')) {

View file

@ -7,6 +7,7 @@ use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Company;
use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
@ -81,6 +82,13 @@ class LocationsController extends Controller
$location->fax = request('fax');
$location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
// Only scope the location if the setting is enabled
if (Setting::getSettings()->scope_locations_fmcs) {
$location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
} else {
$location->company_id = $request->input('company_id');
}
$location = $request->handleImages($location);
if ($location->save()) {
@ -140,7 +148,13 @@ class LocationsController extends Controller
$location->fax = request('fax');
$location->ldap_ou = $request->input('ldap_ou');
$location->manager_id = $request->input('manager_id');
$location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
// Only scope the location if the setting is enabled
if (Setting::getSettings()->scope_locations_fmcs) {
$location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
} else {
$location->company_id = $request->input('company_id');
}
$location = $request->handleImages($location);

View file

@ -314,6 +314,13 @@ class SettingsController extends Controller
}
$setting->full_multiple_companies_support = $request->input('full_multiple_companies_support', '0');
$setting->scope_locations_fmcs = $request->input('scope_locations_fmcs', '0');
// Backward compatibility for locations makes no sense without FullMultipleCompanySupport
if (!$setting->full_multiple_companies_support) {
$setting->scope_locations_fmcs = '0';
}
$setting->unique_serial = $request->input('unique_serial', '0');
$setting->shortcuts_enabled = $request->input('shortcuts_enabled', '0');
$setting->show_images_in_email = $request->input('show_images_in_email', '0');

View file

@ -4,6 +4,7 @@ namespace App\Models;
use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Asset;
use App\Models\Setting;
use App\Models\SnipeModel;
use App\Models\Traits\Searchable;
use App\Models\User;
@ -17,12 +18,21 @@ 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;
protected $presenter = \App\Presenters\LocationPresenter::class;
use Presentable;
use SoftDeletes;
use CompanyableTrait;
protected $table = 'locations';
protected $rules = [

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddScopeLocationsSetting extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('settings', function (Blueprint $table) {
$table->boolean('scope_locations_fmcs')->default('0')->after('full_multiple_companies_support');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('scope_locations_fmcs');
});
}
}

View file

@ -148,6 +148,8 @@ return [
'logo_print_assets_help' => 'Firmenlogo anzeigen beim Drucken der Asset-Liste ',
'full_multiple_companies_support_help_text' => 'Beschränkung von Benutzern (inklusive Administratoren) die einer Firma zugewiesen sind zu den Assets der Firma.',
'full_multiple_companies_support_text' => 'Volle Mehrmandanten-Unterstützung für Firmen',
'scope_locations_fmcs_support_text' => 'Beschränke Standorte mit voller Mehrmandanten-Unterstützung für Firmen',
'scope_locations_fmcs_support_help_text' => 'Bis zu Version 7.0 waren Standorte nicht auf die Firma des Benutzers beschränkt. Wenn diese Einstellung deaktiviert ist, wird die Kompatibilität zu älteren Versionen gewahrt und die Standorte nicht beschränkt. Wenn diese Einstellung aktiviert ist, werden Standorte ebenfalls auf die Firma des Benutzers beschränkt.',
'show_in_model_list' => 'In Modell-Dropdown-Liste anzeigen',
'optional' => 'optional',
'per_page' => 'Ergebnisse pro Seite',

View file

@ -148,6 +148,8 @@ return [
'logo_print_assets_help' => 'Use branding on printable asset lists ',
'full_multiple_companies_support_help_text' => 'Restricting users (including admins) assigned to companies to their company\'s assets.',
'full_multiple_companies_support_text' => 'Full Multiple Companies Support',
'scope_locations_fmcs_support_text' => 'Scope Locations with Full Multiple Companies Support',
'scope_locations_fmcs_support_help_text' => 'Up until Version 7.0 locations were not restricted to the users company. If this setting is disabled, this preserves backward compatibility with older versions and locations are not restricted. If this setting is enabled, locations are also restricted to the users company',
'show_in_model_list' => 'Show in Model Dropdowns',
'optional' => 'optional',
'per_page' => 'Results Per Page',

View file

@ -11,8 +11,8 @@
</div>
@include('modals.partials.name', ['item' => new \App\Models\Location(), 'required' => 'true'])
<!-- Setup of default company, taken from asset creator -->
@if ($user->company)
<!-- Setup of default company, taken from asset creator if scoped locations are activated in the settings -->
@if (($snipeSettings->scope_locations_fmcs == '1') && ($user->company))
<input type="hidden" name="company_id" id='modal-company' value='{{ $user->company->id }}' class="form-control">
@endif

View file

@ -54,7 +54,24 @@
</p>
</div>
</div>
<!-- /.form-group -->
<!-- Scope Locations with Full Multiple Companies Support -->
<div class="form-group {{ $errors->has('scope_locations_fmcs') ? 'error' : '' }}">
<div class="col-md-3">
{{ Form::label('scope_locations_fmcs', trans('admin/settings/general.scope_locations_fmcs_support_text')) }}
</div>
<div class="col-md-9">
<label class="form-control">
{{ Form::checkbox('scope_locations_fmcs', '1', old('scope_locations_fmcs', $setting->scope_locations_fmcs),array('class' => 'minimal', 'aria-label'=>'scope_locations_fmcs')) }}
{{ trans('admin/settings/general.scope_locations_fmcs_support_text') }}
</label>
{!! $errors->first('scope_locations_fmcs', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
<p class="help-block">
{{ trans('admin/settings/general.scope_locations_fmcs_support_help_text') }}
</p>
</div>
</div>
<!-- /.form-group -->
<!-- Require signature for acceptance -->
@ -479,6 +496,5 @@
});
});
</script>
@stop