Quick fix

This commit is contained in:
Bubka
2025-02-13 17:23:54 +01:00
parent 23a0ae31e5
commit 78e337021e
4 changed files with 44 additions and 9 deletions

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Exceptions;
use Exception;
/**
* Class ConflictException.
*
* @codeCoverageIgnore
*/
class ConflictException extends Exception {}

View File

@@ -106,5 +106,14 @@ class Handler extends ExceptionHandler
], 401);
}
});
$this->renderable(function (ConflictException $exception, $request) {
return response()->json([
'message' => 'conflict',
'reason' => [$exception->getMessage()],
], 409);
});
}
}

View File

@@ -2,11 +2,13 @@
namespace App\Services;
use App\Exceptions\ConflictException;
use App\Models\Group;
use App\Models\TwoFAccount;
use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class GroupService
@@ -19,6 +21,7 @@ class GroupService
* @param mixed $targetGroup The group the accounts should be assigned to
*
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws App\Exceptions\ConflictException
*/
public static function assign($ids, User $user, mixed $targetGroup = null) : void
{
@@ -54,16 +57,27 @@ class GroupService
if ($group) {
$ids = is_array($ids) ? $ids : [$ids];
$twofaccounts = TwoFAccount::find($ids);
DB::transaction(function () use ($group, $ids, $user) {
// Check if group still exists within transaction
$group = Group::lockForUpdate()->find($group->id);
$twofaccounts = TwoFAccount::sharedLock()->find($ids);
if ($user->cannot('updateEach', [(new TwoFAccount), $twofaccounts])) {
throw new AuthorizationException;
}
if (! $group) {
throw new ConflictException(__('errors.resource_not_found'));
}
// Proceed with assignment
$group->twofaccounts()->saveMany($twofaccounts);
$group->loadCount('twofaccounts');
Log::info(sprintf('Twofaccounts #%s assigned to group %s (ID #%s)', implode(',', $ids), var_export($group->name, true), $group->id));
});
} else {
Log::info('Cannot find a group to assign the TwoFAccounts to');
}

View File

@@ -21,7 +21,7 @@
if (destinationGroupId.value === 0) {
await twofaccountService.withdraw(props.selectedAccountsIds)
}
else await groupService.assign(props.selectedAccountsIds, destinationGroupId.value)
else groupService.assign(props.selectedAccountsIds, destinationGroupId.value, { returnError: true })
emit('accounts-moved')
}