mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-05 21:53:41 +00:00
Autoformat lol
This commit is contained in:
@@ -24,7 +24,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\UserGroup;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use FireflyIII\Enums\UserRoleEnum;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\UserGroupFactory;
|
||||
@@ -36,6 +35,7 @@ use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface;
|
||||
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Override;
|
||||
use ValueError;
|
||||
|
||||
@@ -54,7 +54,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
/** @var GroupMembership $membership */
|
||||
foreach ($memberships as $membership) {
|
||||
/** @var null|User $user */
|
||||
$user = $membership->user()->first();
|
||||
$user = $membership->user()->first();
|
||||
if (null === $user) {
|
||||
continue;
|
||||
}
|
||||
@@ -63,7 +63,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
$count = $user->groupMemberships()->where('user_group_id', '!=', $userGroup->id)->count();
|
||||
if (0 === $count) {
|
||||
Log::debug('User has no other memberships and needs a new user group.');
|
||||
$newUserGroup = $this->createNewUserGroup($user);
|
||||
$newUserGroup = $this->createNewUserGroup($user);
|
||||
$user->user_group_id = $newUserGroup->id;
|
||||
$user->save();
|
||||
Log::debug(sprintf('Make new group #%d ("%s")', $newUserGroup->id, $newUserGroup->title));
|
||||
@@ -73,7 +73,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
Log::debug('User has other memberships and will be assigned a new administration.');
|
||||
|
||||
/** @var GroupMembership $first */
|
||||
$first = $user->groupMemberships()->where('user_group_id', '!=', $userGroup->id)->inRandomOrder()->first();
|
||||
$first = $user->groupMemberships()->where('user_group_id', '!=', $userGroup->id)->inRandomOrder()->first();
|
||||
$user->user_group_id = $first->id;
|
||||
$user->save();
|
||||
}
|
||||
@@ -83,8 +83,22 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
// all users are now moved away from user group.
|
||||
// time to DESTROY all objects.
|
||||
// we have to do this one by one to trigger the necessary observers :(
|
||||
$objects = ['availableBudgets', 'bills', 'budgets', 'categories', 'currencyExchangeRates', 'objectGroups',
|
||||
'recurrences', 'rules', 'ruleGroups', 'tags', 'transactionGroups', 'transactionJournals', 'piggyBanks', 'accounts', 'webhooks',
|
||||
$objects = [
|
||||
'availableBudgets',
|
||||
'bills',
|
||||
'budgets',
|
||||
'categories',
|
||||
'currencyExchangeRates',
|
||||
'objectGroups',
|
||||
'recurrences',
|
||||
'rules',
|
||||
'ruleGroups',
|
||||
'tags',
|
||||
'transactionGroups',
|
||||
'transactionJournals',
|
||||
'piggyBanks',
|
||||
'accounts',
|
||||
'webhooks'
|
||||
];
|
||||
foreach ($objects as $object) {
|
||||
foreach ($userGroup->{$object}()->get() as $item) { // @phpstan-ignore-line
|
||||
@@ -109,7 +123,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
/** @var null|UserGroup $group */
|
||||
$group = $membership->userGroup()->first();
|
||||
if (null !== $group) {
|
||||
$groupId = $group->id;
|
||||
$groupId = $group->id;
|
||||
if (in_array($groupId, array_keys($set), true)) {
|
||||
continue;
|
||||
}
|
||||
@@ -134,18 +148,18 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
while ($exists && $loop < 10) {
|
||||
$existingGroup = $this->findByName($groupName);
|
||||
if (!$existingGroup instanceof UserGroup) {
|
||||
$exists = false;
|
||||
$exists = false;
|
||||
|
||||
$existingGroup = $this->store(['user' => $user, 'title' => $groupName]);
|
||||
$existingGroup = $this->store(['user' => $user, 'title' => $groupName]);
|
||||
}
|
||||
$groupName = sprintf('%s-%s', $user->email, substr(sha1(random_int(1000, 9999).microtime()), 0, 4));
|
||||
$groupName = sprintf('%s-%s', $user->email, substr(sha1(random_int(1000, 9999) . microtime()), 0, 4));
|
||||
++$loop;
|
||||
}
|
||||
|
||||
return $existingGroup;
|
||||
}
|
||||
|
||||
public function findByName(string $title): ?UserGroup
|
||||
public function findByName(string $title): null|UserGroup
|
||||
{
|
||||
return UserGroup::whereTitle($title)->first();
|
||||
}
|
||||
@@ -158,7 +172,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
$data['user'] = $this->user;
|
||||
|
||||
/** @var UserGroupFactory $factory */
|
||||
$factory = app(UserGroupFactory::class);
|
||||
$factory = app(UserGroupFactory::class);
|
||||
|
||||
return $factory->create($data);
|
||||
}
|
||||
@@ -172,7 +186,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getById(int $id): ?UserGroup
|
||||
public function getById(int $id): null|UserGroup
|
||||
{
|
||||
return UserGroup::find($id);
|
||||
}
|
||||
@@ -180,17 +194,20 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
#[Override]
|
||||
public function getMembershipsFromGroupId(int $groupId): Collection
|
||||
{
|
||||
return $this->user->groupMemberships()->where('user_group_id', $groupId)->get();
|
||||
return $this->user
|
||||
->groupMemberships()
|
||||
->where('user_group_id', $groupId)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function update(UserGroup $userGroup, array $data): UserGroup
|
||||
{
|
||||
$userGroup->title = $data['title'];
|
||||
$userGroup->save();
|
||||
$currency = null;
|
||||
$currency = null;
|
||||
|
||||
/** @var CurrencyRepositoryInterface $repository */
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
if (array_key_exists('primary_currency_code', $data)) {
|
||||
$repository->setUser($this->user);
|
||||
@@ -199,13 +216,12 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
|
||||
if (array_key_exists('primary_currency_id', $data) && null === $currency) {
|
||||
$repository->setUser($this->user);
|
||||
$currency = $repository->find((int)$data['primary_currency_id']);
|
||||
$currency = $repository->find((int) $data['primary_currency_id']);
|
||||
}
|
||||
if (null !== $currency) {
|
||||
$repository->makePrimary($currency);
|
||||
}
|
||||
|
||||
|
||||
return $userGroup;
|
||||
}
|
||||
|
||||
@@ -216,17 +232,17 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
*/
|
||||
public function updateMembership(UserGroup $userGroup, array $data): UserGroup
|
||||
{
|
||||
$owner = UserRole::whereTitle(UserRoleEnum::OWNER)->first();
|
||||
$owner = UserRole::whereTitle(UserRoleEnum::OWNER)->first();
|
||||
Log::debug('in update membership');
|
||||
|
||||
/** @var null|User $user */
|
||||
$user = null;
|
||||
$user = null;
|
||||
if (array_key_exists('id', $data)) {
|
||||
/** @var null|User $user */
|
||||
$user = User::find($data['id']);
|
||||
Log::debug('Found user by ID');
|
||||
}
|
||||
if (array_key_exists('email', $data) && '' !== (string)$data['email']) {
|
||||
if (array_key_exists('email', $data) && '' !== (string) $data['email']) {
|
||||
/** @var null|User $user */
|
||||
$user = User::whereEmail($data['email'])->first();
|
||||
Log::debug('Found user by email');
|
||||
@@ -244,13 +260,13 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
if (1 === $membershipCount) {
|
||||
$lastUserId = $userGroup->groupMemberships()->distinct()->first(['group_memberships.user_id'])->user_id;
|
||||
// if this is also the user we're editing right now, and we remove all of their roles:
|
||||
if ($lastUserId === (int)$user->id && 0 === count($data['roles'])) {
|
||||
if ($lastUserId === (int) $user->id && 0 === count($data['roles'])) {
|
||||
Log::debug('User is last in this group, refuse to act');
|
||||
|
||||
throw new FireflyException('You cannot remove the last member from this user group. Delete the user group instead.');
|
||||
}
|
||||
// if this is also the user we're editing right now, and do not grant them the owner role:
|
||||
if ($lastUserId === (int)$user->id && count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)) {
|
||||
if ($lastUserId === (int) $user->id && count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)) {
|
||||
Log::debug('User needs to have owner role in this group, refuse to act');
|
||||
|
||||
throw new FireflyException('The last member in this user group must get or keep the "owner" role.');
|
||||
@@ -258,16 +274,16 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
}
|
||||
if ($membershipCount > 1) {
|
||||
// group has multiple members. How many are owner, except the user we're editing now?
|
||||
$ownerCount = $userGroup->groupMemberships()
|
||||
->where('user_role_id', $owner->id)
|
||||
->where('user_id', '!=', $user->id)->count()
|
||||
;
|
||||
$ownerCount = $userGroup->groupMemberships()->where('user_role_id', $owner->id)->where('user_id', '!=', $user->id)->count();
|
||||
// if there are no other owners and the current users does not get or keep the owner role, refuse.
|
||||
if (
|
||||
0 === $ownerCount
|
||||
&& (0 === count($data['roles'])
|
||||
|| (count($data['roles']) > 0 // @phpstan-ignore-line
|
||||
&& !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)))) {
|
||||
&& (
|
||||
0 === count($data['roles'])
|
||||
|| count($data['roles']) > 0 // @phpstan-ignore-line
|
||||
&& !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)
|
||||
)
|
||||
) {
|
||||
Log::debug('User needs to keep owner role in this group, refuse to act');
|
||||
|
||||
throw new FireflyException('The last owner in this user group must keep the "owner" role.');
|
||||
@@ -286,7 +302,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
|
||||
continue;
|
||||
}
|
||||
$userRole = UserRole::whereTitle($enum->value)->first();
|
||||
$user->groupMemberships()->create(['user_group_id' => $userGroup->id, 'user_role_id' => $userRole->id]);
|
||||
$user->groupMemberships()->create(['user_group_id' => $userGroup->id, 'user_role_id' => $userRole->id]);
|
||||
}
|
||||
|
||||
return $userGroup;
|
||||
|
||||
Reference in New Issue
Block a user