CaiWuXiTong/app/Http/Middleware/IdentifyCompany.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

68 lines
1.8 KiB
PHP

<?php
namespace App\Http\Middleware;
use App\Traits\Companies;
use App\Traits\Modules;
use App\Traits\Users;
use Closure;
use Illuminate\Auth\AuthenticationException;
class IdentifyCompany
{
use Companies, Modules, Users;
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string[] ...$guards
* @return mixed
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function handle($request, Closure $next, ...$guards)
{
$this->request = $request;
$company_id = $this->getCompanyId();
if (empty($company_id)) {
if (request_is_mcp($this->request) || $this->request->bearerToken()) {
throw new AuthenticationException('Unauthenticated.', $guards);
}
abort(400, 'Missing company');
}
// Check if user can access company
if ($this->request->isNotSigned($company_id) && $this->isNotUserCompany($company_id)) {
throw new AuthenticationException('Unauthenticated.', $guards);
}
// Set company as current
$company = company($company_id);
if (empty($company)) {
abort(404, 'Company not found');
}
$company->makeCurrent();
// Load company-specific modules (event listeners, providers)
$this->registerModules();
// Fix file/folder paths
config(['filesystems.disks.' . config('filesystems.default') . '.url' => url('/' . $company_id) . '/uploads']);
// Fix routes
if ($this->request->isNotApi()) {
app('url')->defaults(['company_id' => $company_id]);
$this->request->route()->forgetParameter('company_id');
}
return $next($this->request);
}
}