Files
firefly-iii/app/Support/Http/Api/ValidatesUserGroupTrait.php

107 lines
4.2 KiB
PHP
Raw Normal View History

2023-09-21 16:26:07 +02:00
<?php
/*
* ValidatesUserGroupTrait.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Http\Api;
2024-04-07 06:06:40 +02:00
use FireflyIII\Enums\UserRoleEnum;
2023-09-21 16:26:07 +02:00
use FireflyIII\Models\UserGroup;
2024-04-20 16:18:41 +02:00
use FireflyIII\Repositories\UserGroup\UserGroupRepositoryInterface;
2023-09-21 16:26:07 +02:00
use FireflyIII\User;
2024-04-07 06:06:40 +02:00
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
2023-09-21 16:26:07 +02:00
use Illuminate\Http\Request;
2024-04-20 16:18:41 +02:00
use Illuminate\Support\Facades\Log;
2023-09-21 16:26:07 +02:00
/**
* Trait ValidatesUserGroupTrait
*/
2023-09-21 16:26:07 +02:00
trait ValidatesUserGroupTrait
{
/**
2024-04-07 06:06:40 +02:00
* @throws AuthorizationException
* @throws AuthenticationException
2023-09-21 16:26:07 +02:00
*/
2024-04-07 06:06:40 +02:00
protected function validateUserGroup(Request $request): UserGroup
2023-09-21 16:26:07 +02:00
{
2024-04-20 16:18:41 +02:00
Log::debug(sprintf('validateUserGroup: %s', static::class));
2023-09-21 16:26:07 +02:00
if (!auth()->check()) {
2024-04-20 16:18:41 +02:00
Log::debug('validateUserGroup: user is not logged in, return NULL.');
2023-12-20 19:35:52 +01:00
2024-04-07 06:06:40 +02:00
throw new AuthenticationException();
2023-09-21 16:26:07 +02:00
}
2023-12-20 19:35:52 +01:00
2023-09-21 16:26:07 +02:00
/** @var User $user */
$user = auth()->user();
$groupId = 0;
2023-09-21 16:26:07 +02:00
if (!$request->has('user_group_id')) {
$groupId = $user->user_group_id;
2024-04-20 16:18:41 +02:00
Log::debug(sprintf('validateUserGroup: no user group submitted, use default group #%d.', $groupId));
2023-09-21 16:26:07 +02:00
}
2024-04-07 06:06:40 +02:00
if ($request->has('user_group_id')) {
2024-04-20 16:18:41 +02:00
$groupId = (int) $request->get('user_group_id');
Log::debug(sprintf('validateUserGroup: user group submitted, search for memberships in group #%d.', $groupId));
2024-04-07 06:06:40 +02:00
}
2024-04-20 16:18:41 +02:00
/** @var UserGroupRepositoryInterface $repository */
$repository = app(UserGroupRepositoryInterface::class);
2024-04-20 16:18:41 +02:00
$repository->setUser($user);
$memberships = $repository->getMembershipsFromGroupId($groupId);
2024-04-20 16:18:41 +02:00
if (0 === $memberships->count()) {
Log::debug(sprintf('validateUserGroup: user has no access to group #%d.', $groupId));
2024-04-07 06:06:40 +02:00
2024-04-20 16:18:41 +02:00
throw new AuthorizationException((string) trans('validation.no_access_group'));
2024-04-07 06:06:40 +02:00
}
2023-12-20 19:35:52 +01:00
2024-04-07 06:06:40 +02:00
// need to get the group from the membership:
$group = $repository->getById($groupId);
2024-04-07 06:06:40 +02:00
if (null === $group) {
2024-04-20 16:18:41 +02:00
Log::debug(sprintf('validateUserGroup: group #%d does not exist.', $groupId));
2024-04-20 16:18:41 +02:00
throw new AuthorizationException((string) trans('validation.belongs_user_or_user_group'));
2023-09-21 16:26:07 +02:00
}
2024-04-20 16:18:41 +02:00
Log::debug(sprintf('validateUserGroup: validate access of user to group #%d ("%s").', $groupId, $group->title));
2024-04-23 19:40:48 +02:00
$roles = property_exists($this, 'acceptedRoles') ? $this->acceptedRoles : []; // @phpstan-ignore-line
if (0 === count($roles)) {
2024-04-20 16:18:41 +02:00
Log::debug('validateUserGroup: no roles defined, so no access.');
2024-04-07 06:06:40 +02:00
2024-04-20 16:18:41 +02:00
throw new AuthorizationException((string) trans('validation.no_accepted_roles_defined'));
2024-04-07 06:06:40 +02:00
}
2024-04-20 16:18:41 +02:00
Log::debug(sprintf('validateUserGroup: have %d roles to check.', count($roles)), $roles);
2024-04-07 06:06:40 +02:00
/** @var UserRoleEnum $role */
foreach ($roles as $role) {
if ($user->hasRoleInGroupOrOwner($group, $role)) {
2024-04-20 16:18:41 +02:00
Log::debug(sprintf('validateUserGroup: User has role "%s" in group #%d, return the group.', $role->value, $groupId));
2024-04-07 06:06:40 +02:00
return $group;
}
2024-04-20 16:18:41 +02:00
Log::debug(sprintf('validateUserGroup: User does NOT have role "%s" in group #%d, continue searching.', $role->value, $groupId));
2024-04-07 06:06:40 +02:00
}
2024-04-20 16:18:41 +02:00
Log::debug('validateUserGroup: User does NOT have enough rights to access endpoint.');
2023-12-20 19:35:52 +01:00
2024-04-20 16:18:41 +02:00
throw new AuthorizationException((string) trans('validation.belongs_user_or_user_group'));
2023-09-21 16:26:07 +02:00
}
}