2FAuth/app/Services/GroupService.php

118 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
2021-12-02 13:15:53 +01:00
use App\Models\Group;
use App\Models\TwoFAccount;
use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Collection;
2021-10-15 23:46:21 +02:00
use Illuminate\Support\Facades\Log;
class GroupService
{
/**
* Assign one or more accounts to a group
2022-11-22 15:15:52 +01:00
*
2024-04-20 19:03:44 +02:00
* @param array|int $ids accounts ids to assign
* @param User $user The user who owns the accounts & the target group
* @param mixed $targetGroup The group the accounts should be assigned to
2023-03-10 22:59:46 +01:00
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public static function assign($ids, User $user, mixed $targetGroup = null) : void
{
// targetGroup == 0 == The pseudo group named 'All' == No group
// It means we do not want the accounts to be associated to a group, either a
// specific group or the default group from user preferences.
// If you need to release the accounts from an existing association, use the
// TwoFAccountService::withdraw() method.
if ($targetGroup === 0 || $targetGroup === '0') {
Log::info('Group assignment skipped, no group explicitly requested');
return;
}
// Two main cases :
// - A group (or group id) is passed as parameter => It has priority for use, if the group is valid
// - No group is passed => We try to identify a destination group through user preferences
$group = null;
if(! is_null($targetGroup)) {
if ($targetGroup instanceof Group && $targetGroup->exists && $targetGroup->user_id == $user->id) {
$group = $targetGroup;
}
else {
$group = Group::where('id', (int) $targetGroup)
->where('user_id', $user->id)
->first();
}
}
2023-03-10 22:59:46 +01:00
if (! $group) {
$group = self::defaultGroup($user);
}
if ($group) {
2023-03-10 22:59:46 +01:00
$ids = is_array($ids) ? $ids : [$ids];
$twofaccounts = TwoFAccount::find($ids);
if ($user->cannot('updateEach', [(new TwoFAccount), $twofaccounts])) {
throw new AuthorizationException();
}
$group->twofaccounts()->saveMany($twofaccounts);
$group->loadCount('twofaccounts');
2021-10-15 23:46:21 +02: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');
}
}
/**
* Prepends the pseudo group named 'All' to a group collection
*
2024-06-27 13:37:15 +02:00
* @param \Illuminate\Database\Eloquent\Collection<int, Group> $groups
* @return \Illuminate\Database\Eloquent\Collection<int, Group>
*/
2024-06-27 13:37:15 +02:00
public static function prependTheAllGroup(Collection $groups, User $user)
{
$theAllGroup = new Group([
'name' => __('commons.all'),
]);
$theAllGroup->id = 0;
$theAllGroup->twofaccounts_count = $user->twofaccounts->count();
return $groups->prepend($theAllGroup);
}
/**
* Set owner of given groups
2023-12-20 16:55:58 +01:00
*
2024-06-27 13:37:15 +02:00
* @param \Illuminate\Database\Eloquent\Collection<int, Group> $groups
*/
public static function setUser(Collection $groups, User $user) : void
{
$groups->each(function ($group, $key) use ($user) {
$group->user_id = $user->id;
$group->save();
});
}
/**
* 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
*/
private static function defaultGroup(User $user)
{
$id = $user->preferences['defaultGroup'] === -1 ? (int) $user->preferences['activeGroup'] : (int) $user->preferences['defaultGroup'];
return Group::where('id', $id)
->where('user_id', $user->id)
->first();
}
2022-11-22 15:15:52 +01:00
}