Files
nala 48347e2d3a
tests / ci (push) Canceled after 0s
commit5
2026-10-30 10:55:18 +02:00

26 lines
797 B
PHP

<?php
namespace App\Services;
use App\Enums\PurchasingRole;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class CurrentActor
{
/** Call after the company lock, inside the business transaction.
* @param list<PurchasingRole>|null $roles
*/
public function lock(User $actor, ?array $roles = null): User
{
abort_unless(auth()->id() === $actor->id && $actor->tenant_id !== null, 403);
if (DB::connection()->transactionLevel() === 0) {
throw new \LogicException('Revalidar o operador dentro da transacção.');
}
$user = User::query()->where('tenant_id', $actor->tenant_id)->lockForUpdate()->findOrFail($actor->id);
abort_unless($roles === null || $user->hasFunctions($roles), 403);
return $user;
}
}