From 194eb54335c545e0eafe294b7a9e63a098bab3c7 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Fri, 8 Oct 2021 23:18:39 +0200 Subject: [PATCH] Fix TwoFAccounts assignment when destination group does not exist --- app/Services/GroupService.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/app/Services/GroupService.php b/app/Services/GroupService.php index 300fdb29..969dae4f 100644 --- a/app/Services/GroupService.php +++ b/app/Services/GroupService.php @@ -108,25 +108,27 @@ public function delete($ids) : int * * @param array|int $ids accounts ids to assign * @param Group $group The target group - * @return Group The updated group + * @return void */ - public function assign($ids, Group $group = null) : Group + public function assign($ids, Group $group = null) : void { if (!$group) { - $group = $this->destinationGroup(); + $group = $this->defaultGroup(); } - if (!is_array($ids)) { - $ids = array($ids); + if ($group) { + // saveMany() expect an iterable so we pass an array to + // find() to always obtain a list of TwoFAccount + if (!is_array($ids)) { + $ids = array($ids); + } + $twofaccounts = TwoFAccount::find($ids); + + $group->twofaccounts()->saveMany($twofaccounts); } - $twofaccounts = TwoFAccount::find($ids); - - $group->twofaccounts()->saveMany($twofaccounts); - $group->loadCount('twofaccounts'); - - return $group; } + /** * Finds twofaccounts assigned to the group * @@ -144,9 +146,9 @@ public function getAccounts(Group $group) : Collection /** * Determines the destination group * - * @return Group The group + * @return Group|null The group or null if it does not exist */ - private function destinationGroup() : Group + private function defaultGroup() { $id = $this->settingService->get('defaultGroup') === '-1' ? (int) $this->settingService->get('activeGroup') : (int) $this->settingService->get('defaultGroup');