2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Scopes;
|
|
|
|
|
2020-12-26 16:13:34 +03:00
|
|
|
use App\Traits\Scopes;
|
2017-09-14 22:21:00 +03:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2020-02-09 12:44:36 +03:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Scope;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Company implements Scope
|
|
|
|
{
|
2020-12-26 16:13:34 +03:00
|
|
|
use Scopes;
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Apply the scope to a given Eloquent query builder.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder
|
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function apply(Builder $builder, Model $model)
|
|
|
|
{
|
2020-05-02 14:33:41 +03:00
|
|
|
if (method_exists($model, 'isNotTenantable') && $model->isNotTenantable()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
$table = $model->getTable();
|
|
|
|
|
|
|
|
// Skip for specific tables
|
2020-02-09 12:44:36 +03:00
|
|
|
$skip_tables = [
|
2020-05-02 14:33:41 +03:00
|
|
|
'jobs', 'firewall_ips', 'firewall_logs', 'media', 'mediables', 'migrations', 'notifications', 'role_companies',
|
|
|
|
'role_permissions', 'sessions', 'user_companies', 'user_dashboards', 'user_permissions', 'user_roles',
|
2020-02-09 12:44:36 +03:00
|
|
|
];
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
if (in_array($table, $skip_tables)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if already exists
|
2020-12-26 16:13:34 +03:00
|
|
|
if ($this->scopeExists($builder, 'company_id')) {
|
2017-09-14 22:21:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply company scope
|
2018-11-10 15:52:56 +03:00
|
|
|
$builder->where($table . '.company_id', '=', session('company_id'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
2020-02-09 12:44:36 +03:00
|
|
|
}
|