mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-19 01:46:03 +02:00
Quick fix
This commit is contained in:
12
app/Exceptions/ConflictException.php
Normal file
12
app/Exceptions/ConflictException.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class ConflictException.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ConflictException extends Exception {}
|
@@ -106,5 +106,14 @@ class Handler extends ExceptionHandler
|
||||
], 401);
|
||||
}
|
||||
});
|
||||
|
||||
$this->renderable(function (ConflictException $exception, $request) {
|
||||
return response()->json([
|
||||
'message' => 'conflict',
|
||||
'reason' => [$exception->getMessage()],
|
||||
], 409);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -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');
|
||||
}
|
||||
|
@@ -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')
|
||||
}
|
||||
|
Reference in New Issue
Block a user