2021-09-21 22:43:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Group;
|
|
|
|
use App\TwoFAccount;
|
2021-09-26 22:06:49 +02:00
|
|
|
use App\Services\SettingServiceInterface;
|
2021-09-21 22:43:39 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2021-10-15 23:46:21 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-09-21 22:43:39 +02:00
|
|
|
|
|
|
|
class GroupService
|
|
|
|
{
|
|
|
|
|
2021-09-26 22:06:49 +02:00
|
|
|
/**
|
|
|
|
* The Settings Service instance.
|
|
|
|
*/
|
|
|
|
protected SettingServiceInterface $settingService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct(SettingServiceInterface $SettingServiceInterface)
|
|
|
|
{
|
|
|
|
$this->settingService = $SettingServiceInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-21 22:43:39 +02:00
|
|
|
/**
|
|
|
|
* Returns all existing groups
|
2021-09-22 22:59:56 +02:00
|
|
|
*
|
|
|
|
* @return Collection
|
2021-09-21 22:43:39 +02:00
|
|
|
*/
|
|
|
|
public function getAll() : Collection
|
|
|
|
{
|
|
|
|
// We return the complete collection of groups
|
|
|
|
// stored in db plus a pseudo group corresponding to 'All'
|
|
|
|
//
|
|
|
|
// This pseudo group contains all twofaccounts regardless
|
|
|
|
// of the user created group they belong to.
|
|
|
|
|
|
|
|
// Get the user created groups
|
|
|
|
$groups = Group::withCount('twofaccounts')->get();
|
|
|
|
|
|
|
|
// Create the pseudo group
|
|
|
|
$allGroup = new Group([
|
|
|
|
'name' => __('commons.all')
|
|
|
|
]);
|
|
|
|
|
|
|
|
$allGroup->id = 0;
|
|
|
|
$allGroup->twofaccounts_count = TwoFAccount::count();
|
|
|
|
|
|
|
|
return $groups->prepend($allGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a group
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Group The created group
|
|
|
|
*/
|
|
|
|
public function create(array $data) : Group
|
|
|
|
{
|
|
|
|
$group = Group::create([
|
|
|
|
'name' => $data['name'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$group->save();
|
|
|
|
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::info(sprintf('Group %s created', var_export($group->name, true)));
|
|
|
|
|
2021-09-21 22:43:39 +02:00
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a group using a list of parameters
|
|
|
|
*
|
|
|
|
* @param Group $group The group
|
|
|
|
* @param array $data The parameters
|
|
|
|
* @return Group The updated group
|
|
|
|
*/
|
|
|
|
public function update(Group $group, array $data) : Group
|
|
|
|
{
|
|
|
|
$group->update([
|
|
|
|
'name' => $data['name'],
|
|
|
|
]);
|
|
|
|
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::info(sprintf('Group %s updated', var_export($group->name, true)));
|
|
|
|
|
2021-09-21 22:43:39 +02:00
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes one or more groups
|
|
|
|
*
|
|
|
|
* @param int|array $ids group ids to delete
|
|
|
|
* @return int The number of deleted
|
|
|
|
*/
|
|
|
|
public function delete($ids) : int
|
|
|
|
{
|
2021-10-09 19:17:05 +02:00
|
|
|
$ids = is_array($ids) ? $ids : func_get_args();
|
|
|
|
|
|
|
|
// A group is possibly set as the default group in Settings.
|
|
|
|
// In this case we reset the setting to "No group" (groupId = 0)
|
|
|
|
$defaultGroupId = $this->settingService->get('defaultGroup');
|
|
|
|
|
|
|
|
if (in_array($defaultGroupId, $ids)) {
|
|
|
|
$this->settingService->set('defaultGroup', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A group is also possibly set as the active group if the user
|
|
|
|
// configured 2FAuth to memorize the active group.
|
|
|
|
// In this case we reset the setting to the pseudo "All" group (groupId = 0)
|
|
|
|
$activeGroupId = $this->settingService->get('activeGroup');
|
|
|
|
|
|
|
|
if (in_array($activeGroupId, $ids)) {
|
|
|
|
$this->settingService->set('activeGroup', 0);
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:43:39 +02:00
|
|
|
$deleted = Group::destroy($ids);
|
|
|
|
|
2021-10-15 23:46:21 +02:00
|
|
|
Log::info(sprintf('Groups #%s deleted', implode(',#', $ids)));
|
|
|
|
|
2021-09-21 22:43:39 +02:00
|
|
|
return $deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign one or more accounts to a group
|
|
|
|
*
|
|
|
|
* @param array|int $ids accounts ids to assign
|
|
|
|
* @param Group $group The target group
|
2021-10-08 23:18:39 +02:00
|
|
|
* @return void
|
2021-09-21 22:43:39 +02:00
|
|
|
*/
|
2021-10-08 23:18:39 +02:00
|
|
|
public function assign($ids, Group $group = null) : void
|
2021-09-21 22:43:39 +02:00
|
|
|
{
|
|
|
|
if (!$group) {
|
2021-10-08 23:18:39 +02:00
|
|
|
$group = $this->defaultGroup();
|
2021-10-08 00:52:15 +02:00
|
|
|
}
|
2021-09-21 22:43:39 +02:00
|
|
|
|
2021-10-08 23:18:39 +02:00
|
|
|
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);
|
2021-09-21 22:43:39 +02:00
|
|
|
|
2021-10-08 23:18:39 +02:00
|
|
|
$group->twofaccounts()->saveMany($twofaccounts);
|
2021-11-13 12:13:22 +01:00
|
|
|
$group->loadCount('twofaccounts');
|
2021-10-15 23:46:21 +02:00
|
|
|
|
|
|
|
Log::info(sprintf('Twofaccounts #%s assigned to groups %s', implode(',#', $ids), var_export($group->name, true)));
|
2021-10-08 23:18:39 +02:00
|
|
|
}
|
2021-10-15 23:46:21 +02:00
|
|
|
else Log::info('Cannot find a group to assign the TwoFAccounts to');
|
2021-09-21 22:43:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-08 23:18:39 +02:00
|
|
|
|
2021-09-22 22:50:45 +02:00
|
|
|
/**
|
|
|
|
* Finds twofaccounts assigned to the group
|
|
|
|
*
|
|
|
|
* @param Group $group The group
|
|
|
|
* @return Collection The assigned accounts
|
|
|
|
*/
|
|
|
|
public function getAccounts(Group $group) : Collection
|
|
|
|
{
|
|
|
|
$twofaccounts = $group->twofaccounts()->where('group_id', $group->id)->get();
|
|
|
|
|
|
|
|
return $twofaccounts;
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:43:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the destination group
|
|
|
|
*
|
2021-10-08 23:18:39 +02:00
|
|
|
* @return Group|null The group or null if it does not exist
|
2021-09-21 22:43:39 +02:00
|
|
|
*/
|
2021-10-08 23:18:39 +02:00
|
|
|
private function defaultGroup()
|
2021-09-21 22:43:39 +02:00
|
|
|
{
|
2021-11-22 01:14:29 +01:00
|
|
|
$id = $this->settingService->get('defaultGroup') === -1 ? (int) $this->settingService->get('activeGroup') : (int) $this->settingService->get('defaultGroup');
|
2021-09-21 22:43:39 +02:00
|
|
|
|
|
|
|
return Group::find($id);
|
|
|
|
}
|
|
|
|
}
|