2021-09-21 22:43:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\Group;
|
|
|
|
use App\Models\TwoFAccount;
|
2023-02-27 00:32:49 +01:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
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
|
|
|
|
{
|
|
|
|
/**
|
2023-03-07 15:17:07 +01:00
|
|
|
* Assign one or more accounts to a group
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2023-03-07 15:17:07 +01:00
|
|
|
* @param array|int $ids accounts ids to assign
|
|
|
|
* @param \App\Models\Group|null $group The group the accounts will be assigned to
|
2023-03-10 22:59:46 +01:00
|
|
|
*
|
2023-02-27 00:32:49 +01:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2021-09-21 22:43:39 +02:00
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public static function assign($ids, User $user, Group $group = null) : void
|
2021-09-21 22:43:39 +02:00
|
|
|
{
|
2023-03-10 22:59:46 +01:00
|
|
|
if (! $group) {
|
2023-03-07 15:17:07 +01:00
|
|
|
$group = self::defaultGroup($user);
|
2023-03-02 14:32:53 +01:00
|
|
|
}
|
2021-09-21 22:43:39 +02:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
if ($group) {
|
2023-03-10 22:59:46 +01:00
|
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
2023-03-07 15:17:07 +01:00
|
|
|
$twofaccounts = TwoFAccount::find($ids);
|
2021-10-09 19:17:05 +02:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
if ($user->cannot('updateEach', [(new TwoFAccount), $twofaccounts])) {
|
|
|
|
throw new AuthorizationException();
|
2023-02-27 00:32:49 +01:00
|
|
|
}
|
2021-10-09 19:17:05 +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
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
Log::info(sprintf('Twofaccounts #%s assigned to group %s (ID #%s)', implode(',', $ids), var_export($group->name, true), $group->id));
|
2023-03-10 22:59:46 +01:00
|
|
|
} else {
|
|
|
|
Log::info('Cannot find a group to assign the TwoFAccounts to');
|
2021-10-08 23:18:39 +02:00
|
|
|
}
|
2021-09-21 22:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-27 00:32:49 +01:00
|
|
|
* Prepends the pseudo group named 'All' to a group collection
|
|
|
|
*
|
|
|
|
* @param Collection<int, Group> $groups
|
|
|
|
* @return Collection<int, Group>
|
|
|
|
*/
|
2023-03-07 15:17:07 +01:00
|
|
|
public static function prependTheAllGroup(Collection $groups, User $user) : Collection
|
2023-02-27 00:32:49 +01:00
|
|
|
{
|
|
|
|
$theAllGroup = new Group([
|
|
|
|
'name' => __('commons.all'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$theAllGroup->id = 0;
|
2023-03-07 15:17:07 +01:00
|
|
|
$theAllGroup->twofaccounts_count = $user->twofaccounts->count();
|
2023-02-27 00:32:49 +01:00
|
|
|
|
|
|
|
return $groups->prepend($theAllGroup);
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:38:48 +02:00
|
|
|
/**
|
|
|
|
* Set owner of given groups
|
|
|
|
*
|
|
|
|
* @param Collection<int, Group> $groups
|
|
|
|
* @param \App\Models\User $user
|
|
|
|
*/
|
|
|
|
public static function setUser(Collection $groups, User $user) : void
|
|
|
|
{
|
|
|
|
$groups->each(function ($group, $key) use ($user) {
|
|
|
|
$group->user_id = $user->id;
|
|
|
|
$group->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
/**
|
|
|
|
* Determines the default group of the given user
|
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
|
2021-09-21 22:43:39 +02:00
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
private static function defaultGroup(User $user)
|
2021-09-21 22:43:39 +02:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$id = $user->preferences['defaultGroup'] === -1 ? (int) $user->preferences['activeGroup'] : (int) $user->preferences['defaultGroup'];
|
2021-09-21 22:43:39 +02:00
|
|
|
|
|
|
|
return Group::find($id);
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|