CaiWuXiTong/app/Scopes/Company.php
WuYongJie 9595267ee7
Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
chore: 导入 Akaunting 初始源码与团队协作规则
2026-07-20 18:20:25 +08:00

52 lines
1.5 KiB
PHP

<?php
namespace App\Scopes;
use App\Traits\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class Company implements Scope
{
use Scopes;
/**
* 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)
{
if (method_exists($model, 'isNotTenantable') && $model->isNotTenantable()) {
return;
}
$table = $model->getTable();
// Skip for specific tables
// Note: 'notifications' is excluded because Laravel's notification system
// links to users (not companies). Queries against notifications should always
// be scoped to the authenticated user (e.g. auth()->user()->notifications)
// to avoid cross-company data exposure.
$skip_tables = [
'jobs', 'firewall_ips', 'firewall_logs', 'migrations', 'notifications', 'role_companies',
'role_permissions', 'sessions', 'user_companies', 'user_dashboards', 'user_permissions', 'user_roles',
];
if (in_array($table, $skip_tables)) {
return;
}
// Skip if already exists
if ($this->scopeColumnExists($builder, '', 'company_id')) {
return;
}
// Apply company scope
$builder->where($table . '.company_id', '=', company_id());
}
}