Skip to content

A free open source IT asset/license management system

Moved company scoping methods to group them together

snipe989dab6259d9
diff --git a/app/Models/Company.php b/app/Models/Company.php
index 60a8022ed7..76cbda10ed 100644
--- a/app/Models/Company.php
+++ b/app/Models/Company.php
@@ -81,26 +81,6 @@ final class Company extends SnipeModel
}
}
- /**
- * 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
- */
- private static function scopeCompanyablesDirectly($query, $column = 'company_id', $table_name = null)
- {
- if (Auth::user()) {
- $company_id = Auth::user()->company_id;
- } else {
- $company_id = null;
- }
-
- $table = ($table_name) ? $table_name."." : $query->getModel()->getTable().".";
-
- if (\Schema::hasColumn($query->getModel()->getTable(), $column)) {
- return $query->where($table.$column, '=', $company_id);
- } else {
- return $query->join('users as users_comp', 'users_comp.id', 'user_id')->where('users_comp.company_id', '=', $company_id);
- }
- }
public static function getIdFromInput($unescaped_input)
{
@@ -190,6 +170,10 @@ final class Company extends SnipeModel
&& ($this->users()->count() === 0);
}
+ /**
+ * @param $unescaped_input
+ * @return int|mixed|string|null
+ */
public static function getIdForUser($unescaped_input)
{
if (! static::isFullMultipleCompanySupportEnabled() || Auth::user()->isSuperUser()) {
@@ -199,24 +183,117 @@ final class Company extends SnipeModel
}
}
+
+ public function users()
+ {
+ return $this->hasMany(User::class, 'company_id');
+ }
+
+ public function assets()
+ {
+ return $this->hasMany(Asset::class, 'company_id');
+ }
+
+ public function licenses()
+ {
+ return $this->hasMany(License::class, 'company_id');
+ }
+
+ public function accessories()
+ {
+ return $this->hasMany(Accessory::class, 'company_id');
+ }
+
+ public function consumables()
+ {
+ return $this->hasMany(Consumable::class, 'company_id');
+ }
+
+ 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::check() && Auth::user()->isSuperUser()) || (! Auth::check())) {
+ \Log::debug('Skip scoping in scopeCompanyableChildren. User is not logged in or is a superadmin');
return $query;
} else {
+ \Log::debug('Fire scopeCompanyablesDirectly.');
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
+ */
+ 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 assicoated with the user
+ if (Auth::user()) {
+ \Log::debug('Admin company is: '.Auth::user()->company_id);
+ $company_id = Auth::user()->company_id;
+ } else {
+ $company_id = null;
+ }
+
+ // 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().".";
+ \Log::debug('Model is: '.$query->getModel());
+
+ \Log::debug('Table is: '.$table);
+
+ // If the column exists in the table, use it to scope the query
+ if (\Schema::hasColumn($query->getModel()->getTable(), $column)) {
+ return $query->where($table.$column, '=', $company_id);
+ } else {
+ return $query->join('users as users_comp', 'users_comp.id', 'user_id')->where('users_comp.company_id', '=', $company_id);
+ }
+ }
+
+ /**
+ * 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)
{
+ \Log::debug('Company Names in scopeCompanyableChildren: '.print_r($companyable_names, true));
+
if (count($companyable_names) == 0) {
throw new Exception('No Companyable Children to scope');
} elseif (! static::isFullMultipleCompanySupportEnabled() || (Auth::check() && Auth::user()->isSuperUser())) {
+ \Log::debug('Skip scoping in scopeCompanyableChildren. User is not logged in or is a superadmin');
return $query;
} else {
$f = function ($q) {
+ \Log::debug('scopeCompanyablesDirectly firing ');
static::scopeCompanyablesDirectly($q);
};
@@ -232,33 +309,4 @@ final class Company extends SnipeModel
}
}
- public function users()
- {
- return $this->hasMany(User::class, 'company_id');
- }
-
- public function assets()
- {
- return $this->hasMany(Asset::class, 'company_id');
- }
-
- public function licenses()
- {
- return $this->hasMany(License::class, 'company_id');
- }
-
- public function accessories()
- {
- return $this->hasMany(Accessory::class, 'company_id');
- }
-
- public function consumables()
- {
- return $this->hasMany(Consumable::class, 'company_id');
- }
-
- public function components()
- {
- return $this->hasMany(Component::class, 'company_id');
- }
}