2020-10-19 06:23:22 +02:00
|
|
|
<?php
|
2021-01-29 18:50:35 +01:00
|
|
|
/*
|
|
|
|
* ChecksLogin.php
|
|
|
|
* Copyright (c) 2021 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/>.
|
|
|
|
*/
|
|
|
|
|
2020-10-19 06:23:22 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\Request;
|
|
|
|
|
2023-09-21 11:29:09 +02:00
|
|
|
use FireflyIII\Enums\UserRoleEnum;
|
|
|
|
use FireflyIII\Models\UserGroup;
|
|
|
|
use FireflyIII\User;
|
2023-01-11 17:30:20 +01:00
|
|
|
|
2020-10-19 06:23:22 +02:00
|
|
|
/**
|
|
|
|
* Trait ChecksLogin
|
|
|
|
*/
|
|
|
|
trait ChecksLogin
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Verify the request.
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now in %s', __METHOD__));
|
2023-09-21 11:29:09 +02:00
|
|
|
// Only allow logged-in users
|
2024-01-01 14:43:56 +01:00
|
|
|
$check = auth()->check();
|
2023-09-21 11:29:09 +02:00
|
|
|
if (!$check) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-05 16:55:16 +01:00
|
|
|
if (!property_exists($this, 'acceptedRoles')) { // @phpstan-ignore-line
|
2023-09-21 11:29:09 +02:00
|
|
|
app('log')->debug('Request class has no acceptedRoles array');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-09-21 11:29:09 +02:00
|
|
|
return true; // check for false already took place.
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-09-21 15:50:49 +02:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$userGroup = $this->getUserGroup();
|
2023-09-21 11:29:09 +02:00
|
|
|
if (null === $userGroup) {
|
2023-09-21 15:50:49 +02:00
|
|
|
app('log')->error('User has no valid user group submitted or otherwise.');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-09-21 15:50:49 +02:00
|
|
|
return false;
|
2023-09-21 11:29:09 +02:00
|
|
|
}
|
2023-09-21 15:50:49 +02:00
|
|
|
|
2023-09-21 11:29:09 +02:00
|
|
|
/** @var UserRoleEnum $role */
|
|
|
|
foreach ($this->acceptedRoles as $role) {
|
2023-09-21 15:50:49 +02:00
|
|
|
// system owner cannot overrule this, MUST be member of the group.
|
2023-11-30 17:28:44 +01:00
|
|
|
$access = $user->hasRoleInGroupOrOwner($userGroup, $role);
|
|
|
|
if ($access) {
|
2023-09-21 11:29:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-09-21 11:29:09 +02:00
|
|
|
return false;
|
2020-10-19 06:23:22 +02:00
|
|
|
}
|
2023-09-21 15:50:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the user group or NULL if none is set.
|
|
|
|
* Will throw exception if invalid.
|
2024-07-27 15:42:43 +02:00
|
|
|
* TODO duplicated in JSONAPI code.
|
2023-09-21 15:50:49 +02:00
|
|
|
*/
|
|
|
|
public function getUserGroup(): ?UserGroup
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
2024-01-01 14:43:56 +01:00
|
|
|
$user = auth()->user();
|
2023-09-21 15:50:49 +02:00
|
|
|
app('log')->debug('Now in getUserGroup()');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
|
|
|
/** @var null|UserGroup $userGroup */
|
2023-11-29 06:30:35 +01:00
|
|
|
$userGroup = $this->route()?->parameter('userGroup');
|
2023-09-21 15:50:49 +02:00
|
|
|
if (null === $userGroup) {
|
|
|
|
app('log')->debug('Request class has no userGroup parameter, but perhaps there is a parameter.');
|
|
|
|
$userGroupId = (int)$this->get('user_group_id');
|
|
|
|
if (0 === $userGroupId) {
|
|
|
|
app('log')->debug(sprintf('Request class has no user_group_id parameter, grab default from user (group #%d).', $user->user_group_id));
|
|
|
|
$userGroupId = (int)$user->user_group_id;
|
|
|
|
}
|
2024-01-01 14:43:56 +01:00
|
|
|
$userGroup = UserGroup::find($userGroupId);
|
2023-09-21 15:50:49 +02:00
|
|
|
if (null === $userGroup) {
|
|
|
|
app('log')->error(sprintf('Request class has user_group_id (#%d), but group does not exist.', $userGroupId));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-09-21 15:50:49 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
app('log')->debug('Request class has valid user_group_id.');
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-09-21 15:50:49 +02:00
|
|
|
return $userGroup;
|
|
|
|
}
|
2020-12-22 05:35:06 +01:00
|
|
|
}
|