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

View File

@@ -21,7 +21,7 @@
if (destinationGroupId.value === 0) { if (destinationGroupId.value === 0) {
await twofaccountService.withdraw(props.selectedAccountsIds) 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') emit('accounts-moved')
} }