akaunting/app/Scopes/Company.php

48 lines
1.2 KiB
PHP
Raw Normal View History

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 = [
2021-05-17 22:32:45 +03:00
'jobs', 'firewall_ips', 'firewall_logs', 'migrations', 'notifications', 'role_companies',
2020-05-02 14:33:41 +03:00
'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
2021-04-16 00:59:43 +03:00
$builder->where($table . '.company_id', '=', company_id());
2017-09-14 22:21:00 +03:00
}
2020-02-09 12:44:36 +03:00
}