mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Expand a few repositories to support user groups.
This commit is contained in:
@@ -38,6 +38,7 @@ use Illuminate\Support\Facades\Log;
|
||||
*/
|
||||
trait ValidatesUserGroupTrait
|
||||
{
|
||||
protected ?UserGroup $userGroup = null;
|
||||
/**
|
||||
* An "undocumented" filter
|
||||
*
|
||||
@@ -98,7 +99,7 @@ trait ValidatesUserGroupTrait
|
||||
foreach ($roles as $role) {
|
||||
if ($user->hasRoleInGroupOrOwner($group, $role)) {
|
||||
Log::debug(sprintf('validateUserGroup: User has role "%s" in group #%d, return the group.', $role->value, $groupId));
|
||||
|
||||
$this->userGroup = $group;
|
||||
return $group;
|
||||
}
|
||||
Log::debug(sprintf('validateUserGroup: User does NOT have role "%s" in group #%d, continue searching.', $role->value, $groupId));
|
||||
|
@@ -24,45 +24,63 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Repositories\UserGroup;
|
||||
|
||||
use FireflyIII\Enums\UserRoleEnum;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\GroupMembership;
|
||||
use FireflyIII\Models\UserGroup;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Trait UserGroupTrait
|
||||
*/
|
||||
trait UserGroupTrait
|
||||
{
|
||||
protected User $user;
|
||||
protected UserGroup $userGroup;
|
||||
protected ?User $user = null;
|
||||
protected ?UserGroup $userGroup = null;
|
||||
|
||||
public function getUserGroup(): UserGroup
|
||||
{
|
||||
return $this->userGroup;
|
||||
}
|
||||
|
||||
public function checkUserGroupAccess(UserRoleEnum $role): bool
|
||||
{
|
||||
$result = $this->user->hasRoleInGroupOrOwner($this->userGroup, $role);
|
||||
if($result) {
|
||||
Log::debug(sprintf('User #%d has role %s in group #%d or is owner/full.', $this->user->id, $role->value, $this->userGroup->id));
|
||||
return true;
|
||||
}
|
||||
Log::warning(sprintf('User #%d DOES NOT have role %s in group #%d.', $this->user->id, $role->value, $this->userGroup->id));
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO This method does not check if the user has access to this particular user group.
|
||||
*/
|
||||
public function setUserGroup(UserGroup $userGroup): void
|
||||
{
|
||||
if (null === $this->user) {
|
||||
Log::warning(sprintf('User is not set in repository %s', get_class($this)));
|
||||
}
|
||||
$this->userGroup = $userGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function setUser(null|Authenticatable|User $user): void
|
||||
public function setUser(null | Authenticatable | User $user): void
|
||||
{
|
||||
if ($user instanceof User) {
|
||||
$this->user = $user;
|
||||
$this->user = $user;
|
||||
if (null === $user->userGroup) {
|
||||
throw new FireflyException(sprintf('User #%d has no user group.', $user->id));
|
||||
}
|
||||
$this->userGroup = $user->userGroup;
|
||||
return;
|
||||
}
|
||||
throw new FireflyException(sprintf('Object is of class %s, not User.', get_class($user)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,19 +88,18 @@ trait UserGroupTrait
|
||||
*/
|
||||
public function setUserGroupById(int $userGroupId): void
|
||||
{
|
||||
$memberships = GroupMembership::where('user_id', $this->user->id)
|
||||
->where('user_group_id', $userGroupId)
|
||||
->count()
|
||||
;
|
||||
$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));
|
||||
}
|
||||
|
||||
/** @var null|UserGroup $userGroup */
|
||||
$userGroup = UserGroup::find($userGroupId);
|
||||
$userGroup = UserGroup::find($userGroupId);
|
||||
if (null === $userGroup) {
|
||||
throw new FireflyException(sprintf('Cannot find administration for user #%d', $this->user->id));
|
||||
}
|
||||
$this->userGroup = $userGroup;
|
||||
$this->setUserGroup($userGroup);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user