Fix TwoFAccounts assignment when destination group does not exist

This commit is contained in:
Bubka 2021-10-08 23:18:39 +02:00
parent faada93720
commit 194eb54335

View File

@ -108,24 +108,26 @@ public function delete($ids) : int
* *
* @param array|int $ids accounts ids to assign * @param array|int $ids accounts ids to assign
* @param Group $group The target group * @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) { if (!$group) {
$group = $this->destinationGroup(); $group = $this->defaultGroup();
} }
if ($group) {
// saveMany() expect an iterable so we pass an array to
// find() to always obtain a list of TwoFAccount
if (!is_array($ids)) { if (!is_array($ids)) {
$ids = array($ids); $ids = array($ids);
} }
$twofaccounts = TwoFAccount::find($ids); $twofaccounts = TwoFAccount::find($ids);
$group->twofaccounts()->saveMany($twofaccounts); $group->twofaccounts()->saveMany($twofaccounts);
$group->loadCount('twofaccounts');
return $group;
} }
}
/** /**
* Finds twofaccounts assigned to the group * Finds twofaccounts assigned to the group
@ -144,9 +146,9 @@ public function getAccounts(Group $group) : Collection
/** /**
* Determines the destination group * 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'); $id = $this->settingService->get('defaultGroup') === '-1' ? (int) $this->settingService->get('activeGroup') : (int) $this->settingService->get('defaultGroup');