Better user group management and object observers

This commit is contained in:
James Cole
2023-09-21 11:29:09 +02:00
parent c3bc1af7b5
commit 724a16944a
31 changed files with 1253 additions and 73 deletions

View File

@@ -25,12 +25,16 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroup;
use FireflyIII\Enums\UserRoleEnum;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\UserGroupFactory;
use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\UserGroup;
use FireflyIII\Models\UserRole;
use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;
use ValueError;
/**
* Class UserGroupRepository
@@ -57,41 +61,36 @@ class UserGroupRepository implements UserGroupRepositoryInterface
// user has memberships of other groups?
$count = $user->groupMemberships()->where('user_group_id', '!=', $userGroup->id)->count();
if (0 === $count) {
app('log')->debug('User has no other memberships and needs a new administration.');
// makeNewAdmin()
// assignToUser().
app('log')->debug('User has no other memberships and needs a new user group.');
$newUserGroup = $this->createNewUserGroup($user);
$user->user_group_id = $newUserGroup->id;
$user->save();
app('log')->debug(sprintf('Make new group #%d ("%s")', $newUserGroup->id, $newUserGroup->title));
}
// user has other memberships, select one at random and assign it to the user.
if ($count > 0) {
// findAndAssign()
app('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();
$user->user_group_id = $first->id;
$user->save();
}
// deleteMembership()
// delete membership so group is empty after this for-loop.
$membership->delete();
}
// all users are now moved away from user group.
// time to DESTROY all objects.
// TODO piggy banks linked to accounts were deleting.
$userGroup->piggyBanks()->delete();
$userGroup->accounts()->delete();
$userGroup->availableBudgets()->delete();
$userGroup->attachments()->delete();
$userGroup->bills()->delete();
$userGroup->budgets()->delete();
$userGroup->categories()->delete();
$userGroup->currencyExchangeRates()->delete();
$userGroup->objectGroups()->delete();
$userGroup->recurrences()->delete();
$userGroup->rules()->delete();
$userGroup->ruleGroups()->delete();
$userGroup->tags()->delete();
$userGroup->transactionJournals()->delete(); // TODO needs delete service probably.
$userGroup->transactionGroups()->delete(); // TODO needs delete service probably.
$userGroup->webhooks()->delete();
// user group deletion should also delete everything else.
// for all users, if this is the primary user group switch to the first alternative.
// if they have no other memberships, create a new user group for them.
// 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',
];
foreach ($objects as $object) {
foreach ($userGroup->$object()->get() as $item) {
$item->delete();
}
}
$userGroup->delete();
app('log')->debug('Done!');
}
/**
@@ -114,6 +113,59 @@ class UserGroupRepository implements UserGroupRepositoryInterface
return $collection;
}
/**
* Because there is the chance that a group with this name already exists,
* Firefly III runs a little loop of combinations to make sure the group name is unique.
*
* @param User $user
*
* @return UserGroup
*/
private function createNewUserGroup(User $user): UserGroup
{
$loop = 0;
$groupName = $user->email;
$exists = true;
$existingGroup = null;
while ($exists && $loop < 10) {
$existingGroup = $this->findByName($groupName);
if (null === $existingGroup) {
$exists = false;
$existingGroup = $this->store(['user' => $user, 'title' => $groupName]);
}
if (null !== $existingGroup) {
// group already exists
$groupName = sprintf('%s-%s', $user->email, substr(sha1((string)(rand(1000, 9999) . microtime())), 0, 4));
}
$loop++;
}
return $existingGroup;
}
/**
* @param string $title
*
* @return UserGroup|null
*/
public function findByName(string $title): ?UserGroup
{
return UserGroup::whereTitle($title)->first();
}
/**
* @param array $data
*
* @return UserGroup
* @throws FireflyException
*/
public function store(array $data): UserGroup
{
$data['user'] = $this->user;
/** @var UserGroupFactory $factory */
$factory = app(UserGroupFactory::class);
return $factory->create($data);
}
/**
* Returns all groups.
*
@@ -136,15 +188,98 @@ class UserGroupRepository implements UserGroupRepositoryInterface
}
/**
* @param array $data
*
* @return UserGroup
* @inheritDoc
*/
public function store(array $data): UserGroup
public function update(UserGroup $userGroup, array $data): UserGroup
{
$data['user'] = $this->user;
/** @var UserGroupFactory $factory */
$factory = app(UserGroupFactory::class);
return $factory->create($data);
$userGroup->title = $data['title'];
$userGroup->save();
return $userGroup;
}
/**
* @inheritDoc
* @throws FireflyException
*/
public function updateMembership(UserGroup $userGroup, array $data): UserGroup
{
$owner = UserRole::whereTitle(UserRoleEnum::OWNER)->first();
app('log')->debug('in update membership');
$user = null;
if (array_key_exists('id', $data)) {
$user = User::find($data['id']);
app('log')->debug('Found user by ID');
}
if (array_key_exists('email', $data) && '' !== (string)$data['email']) {
$user = User::whereEmail($data['email'])->first();
app('log')->debug('Found user by email');
}
if (null === $user) {
// should throw error, but validator already catches this.
app('log')->debug('No user found');
return $userGroup;
}
// count the number of members in the group right now:
$membershipCount = $userGroup->groupMemberships()->distinct()->get(['group_memberships.user_id'])->count();
// if it's 1:
if (1 === $membershipCount) {
$lastUserId = (int)$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'])) {
app('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)) {
app('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.');
}
}
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();
// 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 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)))) {
app('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.');
}
}
// simplify the list of roles:
$rolesSimplified = $this->simplifyListByName($data['roles']);
// delete all existing roles for user:
$user->groupMemberships()->where('user_group_id', $userGroup->id)->delete();
foreach ($rolesSimplified as $role) {
try {
$enum = UserRoleEnum::from($role);
} catch (ValueError $e) {
// TODO error message
continue;
}
$userRole = UserRole::whereTitle($enum->value)->first();
$user->groupMemberships()->create(['user_group_id' => $userGroup->id, 'user_role_id' => $userRole->id]);
}
return $userGroup;
}
/**
* @param array $roles
*
* @return array
*/
private function simplifyListByName(array $roles): array
{
if (in_array(UserRoleEnum::OWNER->value, $roles, true)) {
app('log')->debug(sprintf('List of roles is [%1$s] but this includes "%2$s", so return [%2$s]', join(',', $roles), UserRoleEnum::OWNER->value));
return [UserRoleEnum::OWNER->value];
}
if (in_array(UserRoleEnum::FULL->value, $roles, true)) {
app('log')->debug(sprintf('List of roles is [%1$s] but this includes "%2$s", so return [%2$s]', join(',', $roles), UserRoleEnum::FULL->value));
return [UserRoleEnum::FULL->value];
}
return $roles;
}
}