Expand API and refactor for user groups.

This commit is contained in:
James Cole
2023-09-21 15:50:49 +02:00
parent 7dbdf0c4ff
commit 0b220f3288
45 changed files with 950 additions and 243 deletions
@@ -1,95 +0,0 @@
<?php
/*
* AdministrationTrait.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\Repositories\Administration;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\UserGroup;
use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable;
/**
* Trait AdministrationTrait
*/
trait AdministrationTrait
{
protected ?int $administrationId = null;
protected User $user;
protected ?UserGroup $userGroup = null;
/**
* @return int
*/
public function getAdministrationId(): int
{
return $this->administrationId;
}
/**
* @param int $administrationId
*
* @throws FireflyException
*/
public function setAdministrationId(int $administrationId): void
{
$this->administrationId = $administrationId;
$this->refreshAdministration();
}
/**
* @return void
* @throws FireflyException
*/
private function refreshAdministration(): void
{
if (null !== $this->administrationId) {
$memberships = GroupMembership::where('user_id', $this->user->id)
->where('user_group_id', $this->administrationId)
->count();
if (0 === $memberships) {
throw new FireflyException(sprintf('User #%d has no access to administration #%d', $this->user->id, $this->administrationId));
}
$this->userGroup = UserGroup::find($this->administrationId);
if (null === $this->userGroup) {
throw new FireflyException(sprintf('Unfound administration for user #%d', $this->user->id));
}
return;
}
throw new FireflyException(sprintf('Cannot validate administration for user #%d', $this->user->id));
}
/**
* @param Authenticatable|User|null $user
*
* @return void
*/
public function setUser(Authenticatable|User|null $user): void
{
if (null !== $user) {
$this->user = $user;
}
}
}
@@ -0,0 +1,85 @@
<?php
/*
* UserGroupTrait.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\Repositories\UserGroup;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\UserGroup;
use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable;
/**
* Trait UserGroupTrait
*/
trait UserGroupTrait
{
protected User $user;
protected UserGroup $userGroup;
/**
* @param Authenticatable|User|null $user
*
* @return void
*/
public function setUser(Authenticatable | User | null $user): void
{
if (null !== $user) {
$this->user = $user;
$this->userGroup = $user->userGroup;
}
}
/**
* TODO This method does not check if the user has access to this particular user group.
*
* @param UserGroup $userGroup
*
* @return void
*/
public function setUserGroup(UserGroup $userGroup): void
{
$this->userGroup = $userGroup;
}
/**
* @param int $userGroupId
*
* @throws FireflyException
*/
public function setUserGroupById(int $userGroupId): void
{
$memberships = GroupMembership::where('user_id', $this->user->id)
->where('user_group_id', $userGroupId)
->count();
if (0 === $memberships) {
throw new FireflyException(sprintf('User #%d has no access to administration #%d', $this->user->id, $userGroupId));
}
$this->userGroup = UserGroup::find($userGroupId);
if (null === $this->userGroup) {
throw new FireflyException(sprintf('Unfound administration for user #%d', $this->user->id));
}
}
}
+39 -9
View File
@@ -27,7 +27,6 @@ use FireflyIII\Enums\UserRoleEnum;
use FireflyIII\Models\UserGroup;
use FireflyIII\User;
use Illuminate\Support\Facades\Log;
use ValueError;
/**
* Trait ChecksLogin
@@ -51,20 +50,51 @@ trait ChecksLogin
app('log')->debug('Request class has no acceptedRoles array');
return true; // check for false already took place.
}
/** @var UserGroup $userGroup */
$userGroup = $this->route()->parameter('userGroup');
if (null === $userGroup) {
app('log')->debug('Request class has no userGroup parameter.');
return true;
}
/** @var User $user */
$user = auth()->user();
$user = auth()->user();
$userGroup = $this->getUserGroup();
if (null === $userGroup) {
app('log')->error('User has no valid user group submitted or otherwise.');
return false;
}
/** @var UserRoleEnum $role */
foreach ($this->acceptedRoles as $role) {
if ($user->hasRoleInGroup($userGroup, $role, true, true)) {
// system owner cannot overrule this, MUST be member of the group.
if ($user->hasRoleInGroup($userGroup, $role, true, false)) {
return true;
}
}
return false;
}
/**
* Return the user group or NULL if none is set.
* Will throw exception if invalid.
*
* @return UserGroup|null
*/
public function getUserGroup(): ?UserGroup
{
/** @var User $user */
$user = auth()->user();
app('log')->debug('Now in getUserGroup()');
/** @var UserGroup $userGroup */
$userGroup = $this->route()->parameter('userGroup');
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;
}
$userGroup = UserGroup::find($userGroupId);
if (null === $userGroup) {
app('log')->error(sprintf('Request class has user_group_id (#%d), but group does not exist.', $userGroupId));
return null;
}
app('log')->debug('Request class has valid user_group_id.');
}
return $userGroup;
}
}
+2 -1
View File
@@ -26,7 +26,7 @@ namespace FireflyIII\Support\Request;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException;
use FireflyIII\Repositories\Administration\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
@@ -171,6 +171,7 @@ trait ConvertsDataTypes
// set administration ID
// group ID
$administrationId = auth()->user()->getAdministrationId();
die('uses old administration ID check, needs to be updated.G');
$repository->setAdministrationId($administrationId);
$set = $this->get('accounts');