Files
InvoiceShelf/app/Http/Middleware/ScopeBouncer.php
Darko Gjorgjijoski afbc6c1db3 Handle no-company user in ScopeBouncer middleware and User model
Skip bouncer scoping when user has no companies instead of crashing
on null. Fall back to Y-m-d date format in getFormattedCreatedAtAttribute
when no company settings are available.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 00:12:00 +02:00

50 lines
987 B
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Silber\Bouncer\Bouncer;
use Symfony\Component\HttpFoundation\Response;
class ScopeBouncer
{
/**
* The Bouncer instance.
*
* @var Bouncer
*/
protected $bouncer;
/**
* Constructor.
*/
public function __construct(Bouncer $bouncer)
{
$this->bouncer = $bouncer;
}
/**
* Set the proper Bouncer scope for the incoming request.
*
* @return mixed
*/
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
$company = $request->header('company');
if (! $company) {
$firstCompany = $user->companies()->first();
if (! $firstCompany) {
return $next($request);
}
$company = $firstCompany->id;
}
$this->bouncer->scope()->to($company);
return $next($request);
}
}