Expand models, user groups need more properties.

This commit is contained in:
James Cole
2024-04-01 14:04:22 +02:00
parent b537a3145d
commit 80f410835b
9 changed files with 57 additions and 24 deletions

View File

@@ -43,9 +43,8 @@ class CreateGroupMemberships extends Command
use ShowsFriendlyMessages;
public const string CONFIG_NAME = '560_create_group_memberships';
protected $description = 'Update group memberships';
protected $signature = 'firefly-iii:create-group-memberships';
protected $description = 'Update group memberships';
protected $signature = 'firefly-iii:create-group-memberships';
/**
* Execute the console command.
@@ -82,19 +81,20 @@ class CreateGroupMemberships extends Command
public static function createGroupMembership(User $user): void
{
// check if membership exists
$userGroup = UserGroup::where('title', $user->email)->first();
$userGroup = UserGroup::where('title', $user->email)->first();
if (null === $userGroup) {
$userGroup = UserGroup::create(['title' => $user->email, 'default_administration' => true]);
}
$userRole = UserRole::where('title', UserRoleEnum::OWNER->value)->first();
$userRole = UserRole::where('title', UserRoleEnum::OWNER->value)->first();
if (null === $userRole) {
throw new FireflyException('Firefly III could not find a user role. Please make sure all migrations have run.');
}
$membership = GroupMembership::where('user_id', $user->id)
->where('user_group_id', $userGroup->id)
->where('user_role_id', $userRole->id)->first();
->where('user_group_id', $userGroup->id)
->where('user_role_id', $userRole->id)->first()
;
if (null === $membership) {
GroupMembership::create(
[
@@ -126,15 +126,17 @@ class CreateGroupMemberships extends Command
private function setDefaultGroup(User $user): void
{
Log::debug(sprintf('setDefaultGroup() for #%d "%s"', $user->id, $user->email));
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$groups = $repository->getUserGroups($user);
if(1 === $groups->count()) {
if (1 === $groups->count()) {
/** @var UserGroup $first */
$first = $groups->first();
$first = $groups->first();
$first->default_administration = true;
$first->save();
Log::debug(sprintf('User has only one group (#%d, "%s"), make it the default (owner or not).', $first->id, $first->title));
return;
}
Log::debug(sprintf('User has %d groups.', $groups->count()));
@@ -145,28 +147,30 @@ class CreateGroupMemberships extends Command
*/
/** @var UserGroup $group */
foreach($groups as $group) {
foreach ($groups as $group) {
$group->default_administration = false;
$group->save();
if($group->title === $user->email) {
$roles = $repository->getRolesInGroup($user, $group->id);
if ($group->title === $user->email) {
$roles = $repository->getRolesInGroup($user, $group->id);
Log::debug(sprintf('Group #%d ("%s")', $group->id, $group->title), $roles);
$isOwner = false;
foreach($roles as $role) {
if($role === UserRoleEnum::OWNER->value) {
foreach ($roles as $role) {
if ($role === UserRoleEnum::OWNER->value) {
$isOwner = true;
}
}
if(true === $isOwner) {
if (true === $isOwner) {
// make this group the default, set the rest NOT to be the default:
$group->default_administration = true;
$group->save();
Log::debug(sprintf('Make group #%d ("%s") the default (is owner + name matches).', $group->id, $group->title));
return;
}
if(false === $isOwner) {
if (false === $isOwner) {
$this->friendlyWarning(sprintf('User "%s" has a group with matching name (#%d), but is not the owner. User will be given the owner role.', $user->email, $group->id));
self::createGroupMembership($user);
return;
}
}
@@ -175,5 +179,4 @@ class CreateGroupMemberships extends Command
$this->friendlyWarning(sprintf('User "%s" has no group with matching name. Will be created.', $user->email));
self::createGroupMembership($user);
}
}

View File

@@ -110,6 +110,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @method static EloquentBuilder|Account whereUserGroupId($value)
*
* @property null|UserGroup $userGroup
* @property mixed $account_id
*
* @mixin Eloquent
*/

View File

@@ -61,6 +61,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @method static Builder|InvitedUser whereUpdatedAt($value)
* @method static Builder|InvitedUser whereUserId($value)
*
* @property mixed $user_group_id
*
* @mixin Eloquent
*/
class InvitedUser extends Model

View File

@@ -54,6 +54,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @method static Builder|Preference whereUpdatedAt($value)
* @method static Builder|Preference whereUserId($value)
*
* @property mixed $user_group_id
*
* @mixin Eloquent
*/
class Preference extends Model

View File

@@ -81,6 +81,7 @@ use Illuminate\Database\Query\Builder;
* @method static \Illuminate\Database\Eloquent\Builder|RecurrenceTransaction whereTransactionTypeId($value)
*
* @property null|TransactionType $transactionType
* @property mixed $user_id
*
* @mixin Eloquent
*/

View File

@@ -98,7 +98,7 @@ class UserGroup extends Model
{
use ReturnsIntegerIdTrait;
protected $fillable = ['title'];
protected $fillable = ['title', 'default_administration'];
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).

View File

@@ -392,4 +392,28 @@ class UserRepository implements UserRepositoryInterface
return null !== $invitee;
}
#[\Override]
public function getUserGroups(User $user): Collection
{
$memberships = $user->groupMemberships()->get();
$set = [];
$collection = new Collection();
/** @var GroupMembership $membership */
foreach ($memberships as $membership) {
/** @var null|UserGroup $group */
$group = $membership->userGroup()->first();
if (null !== $group) {
$groupId = (int)$group->id;
if (in_array($groupId, $set, true)) {
continue;
}
$set[$groupId] = $group;
}
}
$collection->push(...$set);
return $collection;
}
}

View File

@@ -59,6 +59,8 @@ interface UserRepositoryInterface
public function changeStatus(User $user, bool $isBlocked, string $code): bool;
public function getUserGroups(User $user): Collection;
/**
* Returns a count of all users.
*/

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
return new class () extends Migration {
/**
* Run the migrations.
*/
@@ -30,8 +31,5 @@ return new class extends Migration
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
public function down(): void {}
};