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

@@ -46,7 +46,6 @@ class CreateGroupMemberships extends Command
protected $description = 'Update group memberships'; protected $description = 'Update group memberships';
protected $signature = 'firefly-iii:create-group-memberships'; protected $signature = 'firefly-iii:create-group-memberships';
/** /**
* Execute the console command. * Execute the console command.
* *
@@ -94,7 +93,8 @@ class CreateGroupMemberships extends Command
} }
$membership = GroupMembership::where('user_id', $user->id) $membership = GroupMembership::where('user_id', $user->id)
->where('user_group_id', $userGroup->id) ->where('user_group_id', $userGroup->id)
->where('user_role_id', $userRole->id)->first(); ->where('user_role_id', $userRole->id)->first()
;
if (null === $membership) { if (null === $membership) {
GroupMembership::create( GroupMembership::create(
[ [
@@ -126,15 +126,17 @@ class CreateGroupMemberships extends Command
private function setDefaultGroup(User $user): void private function setDefaultGroup(User $user): void
{ {
Log::debug(sprintf('setDefaultGroup() for #%d "%s"', $user->id, $user->email)); Log::debug(sprintf('setDefaultGroup() for #%d "%s"', $user->id, $user->email));
/** @var UserRepositoryInterface $repository */ /** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class); $repository = app(UserRepositoryInterface::class);
$groups = $repository->getUserGroups($user); $groups = $repository->getUserGroups($user);
if(1 === $groups->count()) { if (1 === $groups->count()) {
/** @var UserGroup $first */ /** @var UserGroup $first */
$first = $groups->first(); $first = $groups->first();
$first->default_administration = true; $first->default_administration = true;
$first->save(); $first->save();
Log::debug(sprintf('User has only one group (#%d, "%s"), make it the default (owner or not).', $first->id, $first->title)); Log::debug(sprintf('User has only one group (#%d, "%s"), make it the default (owner or not).', $first->id, $first->title));
return; return;
} }
Log::debug(sprintf('User has %d groups.', $groups->count())); Log::debug(sprintf('User has %d groups.', $groups->count()));
@@ -145,28 +147,30 @@ class CreateGroupMemberships extends Command
*/ */
/** @var UserGroup $group */ /** @var UserGroup $group */
foreach($groups as $group) { foreach ($groups as $group) {
$group->default_administration = false; $group->default_administration = false;
$group->save(); $group->save();
if($group->title === $user->email) { if ($group->title === $user->email) {
$roles = $repository->getRolesInGroup($user, $group->id); $roles = $repository->getRolesInGroup($user, $group->id);
Log::debug(sprintf('Group #%d ("%s")', $group->id, $group->title), $roles); Log::debug(sprintf('Group #%d ("%s")', $group->id, $group->title), $roles);
$isOwner = false; $isOwner = false;
foreach($roles as $role) { foreach ($roles as $role) {
if($role === UserRoleEnum::OWNER->value) { if ($role === UserRoleEnum::OWNER->value) {
$isOwner = true; $isOwner = true;
} }
} }
if(true === $isOwner) { if (true === $isOwner) {
// make this group the default, set the rest NOT to be the default: // make this group the default, set the rest NOT to be the default:
$group->default_administration = true; $group->default_administration = true;
$group->save(); $group->save();
Log::debug(sprintf('Make group #%d ("%s") the default (is owner + name matches).', $group->id, $group->title)); Log::debug(sprintf('Make group #%d ("%s") the default (is owner + name matches).', $group->id, $group->title));
return; 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)); $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); self::createGroupMembership($user);
return; 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)); $this->friendlyWarning(sprintf('User "%s" has no group with matching name. Will be created.', $user->email));
self::createGroupMembership($user); self::createGroupMembership($user);
} }
} }

View File

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

View File

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

View File

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

View File

@@ -98,7 +98,7 @@ class UserGroup extends Model
{ {
use ReturnsIntegerIdTrait; 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). * 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; 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 changeStatus(User $user, bool $isBlocked, string $code): bool;
public function getUserGroups(User $user): Collection;
/** /**
* Returns a count of all users. * Returns a count of all users.
*/ */

View File

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