mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-19 09:51:35 +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);
|
], 401);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->renderable(function (ConflictException $exception, $request) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'conflict',
|
||||||
|
'reason' => [$exception->getMessage()],
|
||||||
|
], 409);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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');
|
||||||
}
|
}
|
||||||
|
@@ -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')
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user