2FAuth/app/Services/GroupService.php

144 lines
4.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
2022-11-22 15:15:52 +01:00
use App\Facades\Settings;
2021-12-02 13:15:53 +01:00
use App\Models\Group;
use App\Models\TwoFAccount;
use Illuminate\Database\Eloquent\Collection;
2021-10-15 23:46:21 +02:00
use Illuminate\Support\Facades\Log;
class GroupService
{
/**
* Prepends the pseudo group named 'All' to a group collection
2022-11-22 15:15:52 +01:00
*
* @param Collection<int, Group> $groups
2022-11-21 11:16:43 +01:00
* @return Collection<int, Group>
*/
public static function prependTheAllGroup(Collection $groups, int $userId) : Collection
{
$theAllGroup = new Group([
2022-11-22 15:15:52 +01:00
'name' => __('commons.all'),
]);
$theAllGroup->id = 0;
$theAllGroup->twofaccounts_count = TwoFAccount::where('user_id', $userId)->count();
return $groups->prepend($theAllGroup);
}
/**
* Creates a group
2022-11-22 15:15:52 +01:00
*
* @param array $data
2021-12-02 13:15:53 +01:00
* @return \App\Models\Group The created group
*/
public static 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)));
return $group;
}
/**
* Updates a group using a list of parameters
2022-11-22 15:15:52 +01:00
*
* @param \App\Models\Group $group The group
* @param array $data The parameters
2021-12-02 13:15:53 +01:00
* @return \App\Models\Group The updated group
*/
public static 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)));
return $group;
}
/**
* Deletes one or more groups
2022-11-22 15:15:52 +01:00
*
* @param int|array $ids group ids to delete
* @return int The number of deleted
*/
public static function delete($ids) : int
{
$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)
2022-07-30 17:51:02 +02:00
$defaultGroupId = Settings::get('defaultGroup');
if (in_array($defaultGroupId, $ids)) {
2022-07-30 17:51:02 +02:00
Settings::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)
2022-07-30 17:51:02 +02:00
$activeGroupId = Settings::get('activeGroup');
if (in_array($activeGroupId, $ids)) {
2022-07-30 17:51:02 +02:00
Settings::set('activeGroup', 0);
}
$deleted = Group::destroy($ids);
2021-10-15 23:46:21 +02:00
Log::info(sprintf('Groups #%s deleted', implode(',#', $ids)));
return $deleted;
}
/**
* Assign one or more accounts to a group
2022-11-22 15:15:52 +01:00
*
* @param array|int $ids accounts ids to assign
* @param \App\Models\Group $group The target group
* @return void
*/
public static function assign($ids, Group $group = null) : void
{
2022-11-22 15:15:52 +01:00
if (! $group) {
$group = self::defaultGroup();
}
if ($group) {
// saveMany() expect an iterable so we pass an array to
// find() to always obtain a list of TwoFAccount
2022-11-22 15:15:52 +01:00
if (! is_array($ids)) {
$ids = [$ids];
}
$twofaccounts = TwoFAccount::find($ids);
$group->twofaccounts()->saveMany($twofaccounts);
$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)));
2022-11-22 15:15:52 +01:00
} else {
Log::info('Cannot find a group to assign the TwoFAccounts to');
}
}
/**
* Determines the destination group
2022-11-22 15:15:52 +01:00
*
2021-12-02 13:15:53 +01:00
* @return \App\Models\Group|null The group or null if it does not exist
*/
private static function defaultGroup()
{
2022-07-30 17:51:02 +02:00
$id = Settings::get('defaultGroup') === -1 ? (int) Settings::get('activeGroup') : (int) Settings::get('defaultGroup');
return Group::find($id);
}
2022-11-22 15:15:52 +01:00
}