Add ability to tie locations to companies

Locations are the last big part of the application that can't be tied to companies.
This can be a problem with FullMultipleCompanySupport, because you can't restrict the visibility of locations to the company of the users.

In order to change this, add a company_id to the locations table and wire everything up in the views and controllers.
Aditionally add a new formatter to filter the locations to a specific company, like it is done for assets.

Locations are properly scoped to the users company if FullMultipleCompanySupport is enabled.
If a parent location of a location has a different company than the user, the location does not show up.
This commit is contained in:
Tobias Regnery
2022-02-10 11:36:36 +01:00
parent 95dba6c426
commit 1ccbf8942c
12 changed files with 147 additions and 12 deletions
+30 -1
View File
@@ -22,6 +22,7 @@ class Location extends SnipeModel
protected $presenter = \App\Presenters\LocationPresenter::class;
use Presentable;
use SoftDeletes;
use CompanyableTrait;
protected $table = 'locations';
protected $rules = [
@@ -34,11 +35,13 @@ class Location extends SnipeModel
'zip' => 'max:10|nullable',
'manager_id' => 'exists:users,id|nullable',
'parent_id' => 'nullable|exists:locations,id|non_circular:locations,id',
'company_id' => 'integer|nullable|exists:companies,id',
];
protected $casts = [
'parent_id' => 'integer',
'manager_id' => 'integer',
'company_id' => 'integer',
];
/**
@@ -72,6 +75,7 @@ class Location extends SnipeModel
'currency',
'manager_id',
'image',
'company_id',
];
protected $hidden = ['user_id'];
@@ -90,7 +94,8 @@ class Location extends SnipeModel
* @var array
*/
protected $searchableRelations = [
'parent' => ['name'],
'parent' => ['name'],
'company' => ['name']
];
@@ -214,6 +219,17 @@ class Location extends SnipeModel
->with('parent');
}
/**
* Establishes the locations -> company relationship
*
* @author [T. Regnery] [<tobias.regnery@gmail.com>]
* @since [v7.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function company()
{
return $this->belongsTo(\App\Models\Company::class, 'company_id');
}
/**
* Find the manager of a location
@@ -313,4 +329,17 @@ class Location extends SnipeModel
{
return $query->leftJoin('users as location_user', 'locations.manager_id', '=', 'location_user.id')->orderBy('location_user.first_name', $order)->orderBy('location_user.last_name', $order);
}
/**
* Query builder scope to order on company
*
* @param \Illuminate\Database\Query\Builder $query Query builder instance
* @param text $order Order
*
* @return \Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderCompany($query, $order)
{
return $query->leftJoin('companies as company_sort', 'locations.company_id', '=', 'company_sort.id')->orderBy('company_sort.name', $order);
}
}