Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor companyable #15814

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function index() : View | RedirectResponse
$counts['license'] = \App\Models\License::assetcount();
$counts['consumable'] = \App\Models\Consumable::count();
$counts['component'] = \App\Models\Component::count();
$counts['user'] = \App\Models\Company::scopeCompanyables(auth()->user())->count();
$counts['user'] = \App\Models\User::count(); //\App\Models\Company::scopeCompanyables(auth()->user())->count();
$counts['grand_total'] = $counts['asset'] + $counts['accessory'] + $counts['license'] + $counts['consumable'];

if ((! file_exists(storage_path().'/oauth-private.key')) || (! file_exists(storage_path().'/oauth-public.key'))) {
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Licenses/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ public function getExportLicensesCsv()
'supplier',
'adminuser',
'assignedusers')
->orderBy('created_at', 'DESC');
Company::scopeCompanyables($licenses)
->orderBy('created_at', 'DESC') //;
//Company::scopeCompanyables($licenses)
->chunk(500, function ($licenses) use ($handle) {
$headers = [
// strtolower to prevent Excel from trying to open it as a SYLK file
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Accessory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Helpers\Helper;
use App\Models\Traits\Acceptable;
use App\Models\Traits\Companyable;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -21,7 +22,7 @@ class Accessory extends SnipeModel
use HasFactory;

protected $presenter = \App\Presenters\AccessoryPresenter::class;
use CompanyableTrait;
use Companyable;
use Loggable, Presentable;
use SoftDeletes;

Expand Down
3 changes: 2 additions & 1 deletion app/Models/Actionlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\Companyable;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Carbon\Carbon;
Expand All @@ -16,7 +17,7 @@
*/
class Actionlog extends SnipeModel
{
use CompanyableTrait;
use Companyable;
use HasFactory;

// This is to manually set the source (via setActionSource()) for determineActionSource()
Expand Down
12 changes: 5 additions & 7 deletions app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
use App\Helpers\Helper;
use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\Acceptable;
use App\Models\Traits\Companyable;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use App\Presenters\AssetPresenter;
use Illuminate\Support\Facades\Auth;
use App\Presenters\Presentable;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

/**
* Model for Assets.
Expand All @@ -32,7 +30,7 @@ class Asset extends Depreciable
protected $presenter = AssetPresenter::class;
protected $with = ['model', 'adminuser'];

use CompanyableTrait;
use Companyable;
use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait;

public const LOCATION = 'location';
Expand Down Expand Up @@ -1437,7 +1435,7 @@ public function scopeRequestableAssets($query)
{
$table = $query->getModel()->getTable();

return Company::scopeCompanyables($query->where($table.'.requestable', '=', 1))
return $query->where($table.'.requestable', '=', 1) // TODO Companyable removed here
->whereHas('assetstatus', function ($query) {
$query->where(function ($query) {
$query->where('deployable', '=', 1)
Expand Down
3 changes: 2 additions & 1 deletion app/Models/AssetMaintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Helpers\Helper;
use App\Models\Traits\CompanyableChild;
use App\Models\Traits\Searchable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -18,7 +19,7 @@ class AssetMaintenance extends Model implements ICompanyableChild
{
use HasFactory;
use SoftDeletes;
use CompanyableChildTrait;
use CompanyableChild;
use ValidatingTrait;


Expand Down
93 changes: 0 additions & 93 deletions app/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,104 +242,11 @@ public function components()
return $this->hasMany(Component::class, 'company_id');
}

/**
* START COMPANY SCOPING FOR FMCS
*/

/**
* Scoping table queries, determining if a logged in user is part of a company, and only allows the user to access items associated with that company if FMCS is enabled.
*
* This method is the one that the CompanyableTrait uses to contrain queries automatically, however that trait CANNOT be
* applied to the user's model, since it causes an infinite loop against the authenticated user.
*
* @todo - refactor that trait to handle the user's model as well.
*
* @author [A. Gianotto] <[email protected]>
* @param $query
* @param $column
* @param $table_name
* @return mixed
*/
public static function scopeCompanyables($query, $column = 'company_id', $table_name = null)
{
// If not logged in and hitting this, assume we are on the command line and don't scope?'
if (! static::isFullMultipleCompanySupportEnabled() || (Auth::hasUser() && auth()->user()->isSuperUser()) || (! Auth::hasUser())) {
return $query;
} else {
return static::scopeCompanyablesDirectly($query, $column, $table_name);
}
}

/**
* Scoping table queries, determining if a logged-in user is part of a company, and only allows
* that user to see items associated with that company
*
* @see https://github.com/laravel/framework/pull/24518 for info on Auth::hasUser()
*/
private static function scopeCompanyablesDirectly($query, $column = 'company_id', $table_name = null)
{

// Get the company ID of the logged-in user, or set it to null if there is no company associated with the user
if (Auth::hasUser()) {
$company_id = auth()->user()->company_id;
} else {
$company_id = null;
}


// If the column exists in the table, use it to scope the query
if ((($query) && ($query->getModel()) && (Schema::hasColumn($query->getModel()->getTable(), $column)))) {

// Dynamically get the table name if it's not passed in, based on the model we're querying against
$table = ($table_name) ? $table_name."." : $query->getModel()->getTable().".";

return $query->where($table.$column, '=', $company_id);
}

}

public function adminuser()
{
return $this->belongsTo(\App\Models\User::class, 'created_by');
}


/**
* I legit do not know what this method does, but we can't remove it (yet).
*
* This gets invoked by CompanyableChildScope, but I'm not sure what it does.
*
* @author [A. Gianotto] <[email protected]>
* @param array $companyable_names
* @param $query
* @return mixed
*/
public static function scopeCompanyableChildren(array $companyable_names, $query)
{

if (count($companyable_names) == 0) {
throw new Exception('No Companyable Children to scope');
} elseif (! static::isFullMultipleCompanySupportEnabled() || (Auth::hasUser() && auth()->user()->isSuperUser())) {
return $query;
} else {
$f = function ($q) {
Log::debug('scopeCompanyablesDirectly firing ');
static::scopeCompanyablesDirectly($q);
};

$q = $query->where(function ($q) use ($companyable_names, $f) {
$q2 = $q->whereHas($companyable_names[0], $f);

for ($i = 1; $i < count($companyable_names); $i++) {
$q2 = $q2->orWhereHas($companyable_names[$i], $f);
}
});

return $q;
}
}


/**
* Query builder scope to order on the user that created it
*/
Expand Down
40 changes: 0 additions & 40 deletions app/Models/CompanyableChildScope.php

This file was deleted.

16 changes: 0 additions & 16 deletions app/Models/CompanyableChildTrait.php

This file was deleted.

38 changes: 0 additions & 38 deletions app/Models/CompanyableScope.php

This file was deleted.

18 changes: 0 additions & 18 deletions app/Models/CompanyableTrait.php

This file was deleted.

5 changes: 3 additions & 2 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\Companyable;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -18,7 +19,7 @@ class Component extends SnipeModel
use HasFactory;

protected $presenter = \App\Presenters\ComponentPresenter::class;
use CompanyableTrait;
use Companyable;
use Loggable, Presentable;
use SoftDeletes;
protected $casts = [
Expand Down Expand Up @@ -234,7 +235,7 @@ public function numCheckedOut()
// In case there are elements checked out to assets that belong to a different company
// than this asset and full multiple company support is on we'll remove the global scope,
// so they are included in the count.
foreach ($this->assets()->withoutGlobalScope(new CompanyableScope)->get() as $checkout) {
foreach ($this->assets()->withoutGlobalScope('companyable')->get() as $checkout) {
$checkedout += $checkout->pivot->assigned_qty;
}

Expand Down
14 changes: 4 additions & 10 deletions app/Models/Consumable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@

use App\Helpers\Helper;
use App\Models\Traits\Acceptable;
use App\Models\Traits\Companyable;
use App\Models\Traits\Searchable;
use App\Presenters\ConsumablePresenter;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Relations\Relation;
use App\Presenters\ConsumablePresenter;
use App\Models\Actionlog;
use App\Models\ConsumableAssignment;
use App\Models\User;
use App\Models\Location;
use App\Models\Manufacturer;
use App\Models\Supplier;
use App\Models\Category;

class Consumable extends SnipeModel
{
use HasFactory;

protected $presenter = ConsumablePresenter::class;
use CompanyableTrait;
use Companyable;
use Loggable, Presentable;
use SoftDeletes;
use Acceptable;
Expand Down
Loading
Loading