|
@@ -7,6 +7,7 @@ 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\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
@@ -20,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 \Illuminate\Database\Eloquent\ModelNotFoundException
|
|
*/
|
|
*/
|
|
public static function assign($ids, User $user, mixed $targetGroup = null) : void
|
|
public static function assign($ids, User $user, mixed $targetGroup = null) : void
|
|
{
|
|
{
|
|
@@ -57,21 +59,23 @@ class GroupService
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
|
|
DB::transaction(function () use ($group, $ids, $user) {
|
|
DB::transaction(function () use ($group, $ids, $user) {
|
|
- // Check if group still exists within transaction
|
|
|
|
$group = Group::lockForUpdate()->find($group->id);
|
|
$group = Group::lockForUpdate()->find($group->id);
|
|
$twofaccounts = TwoFAccount::sharedLock()->find($ids);
|
|
$twofaccounts = TwoFAccount::sharedLock()->find($ids);
|
|
|
|
+
|
|
|
|
+ if (! $group) {
|
|
|
|
+ throw new ModelNotFoundException('group no longer exists');
|
|
|
|
+ }
|
|
|
|
|
|
if ($user->cannot('updateEach', [(new TwoFAccount), $twofaccounts])) {
|
|
if ($user->cannot('updateEach', [(new TwoFAccount), $twofaccounts])) {
|
|
throw new AuthorizationException;
|
|
throw new AuthorizationException;
|
|
}
|
|
}
|
|
|
|
|
|
- // Proceed with assignment
|
|
|
|
$group->twofaccounts()->saveMany($twofaccounts);
|
|
$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));
|
|
Log::info(sprintf('Twofaccounts #%s assigned to group %s (ID #%s)', implode(',', $ids), var_export($group->name, true), $group->id));
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ $group->loadCount('twofaccounts');
|
|
} else {
|
|
} else {
|
|
Log::info('Cannot find a group to assign the TwoFAccounts to');
|
|
Log::info('Cannot find a group to assign the TwoFAccounts to');
|
|
}
|
|
}
|