Deploy Ladill POS / deploy (push) Successful in 1m58s
Pro and Business users can manage branches, invite cashiers with branch assignment, and switch registers; sales and register flows respect acting location. Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
2.1 KiB
PHP
72 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\PosLocation;
|
|
use App\Models\User;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\View;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class SetActingAccount
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
if ($user = $request->user()) {
|
|
if ($request->is('api/*')) {
|
|
$accountId = (int) ($request->header('X-Ladill-Account') ?: $user->id);
|
|
} else {
|
|
$accountId = (int) $request->session()->get('ladill_account', $user->id);
|
|
}
|
|
|
|
if (! $user->canAccessAccount($accountId)) {
|
|
$accountId = $user->id;
|
|
if (! $request->is('api/*')) {
|
|
$request->session()->put('ladill_account', $accountId);
|
|
}
|
|
}
|
|
|
|
$accountId = $this->preferEmployerAccount($request, $user, $accountId);
|
|
|
|
$account = $accountId === $user->id ? $user : (User::find($accountId) ?? $user);
|
|
|
|
$request->attributes->set('actingAccount', $account);
|
|
|
|
if (! $request->is('api/*')) {
|
|
View::share('actingAccount', $account);
|
|
View::share('accessibleAccounts', $user->accessibleAccounts());
|
|
}
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
private function preferEmployerAccount(Request $request, User $user, int $accountId): int
|
|
{
|
|
if ($accountId !== $user->id) {
|
|
return $accountId;
|
|
}
|
|
|
|
if (PosLocation::owned($user->public_id)->exists()) {
|
|
return $accountId;
|
|
}
|
|
|
|
$membership = $user->posMemberships()->first();
|
|
if (! $membership) {
|
|
return $accountId;
|
|
}
|
|
|
|
$employer = User::where('public_id', $membership->owner_ref)->first();
|
|
if ($employer && $user->canAccessAccount($employer->id)) {
|
|
if (! $request->is('api/*')) {
|
|
$request->session()->put('ladill_account', $employer->id);
|
|
}
|
|
|
|
return $employer->id;
|
|
}
|
|
|
|
return $accountId;
|
|
}
|
|
}
|