mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +00:00
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>
50 lines
987 B
PHP
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);
|
|
}
|
|
}
|