Turns out it's pointless to add "default_administration", you can force that already through the owner role (which is already in place).

This commit is contained in:
James Cole 2024-04-01 14:15:35 +02:00
parent cc32578c5f
commit 5b83c33039
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
4 changed files with 27 additions and 135 deletions

View File

@ -9,16 +9,16 @@
"packages-dev": [
{
"name": "composer/pcre",
"version": "3.1.2",
"version": "3.1.3",
"source": {
"type": "git",
"url": "https://github.com/composer/pcre.git",
"reference": "4775f35b2d70865807c89d32c8e7385b86eb0ace"
"reference": "5b16e25a5355f1f3afdfc2f954a0a80aec4826a8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/composer/pcre/zipball/4775f35b2d70865807c89d32c8e7385b86eb0ace",
"reference": "4775f35b2d70865807c89d32c8e7385b86eb0ace",
"url": "https://api.github.com/repos/composer/pcre/zipball/5b16e25a5355f1f3afdfc2f954a0a80aec4826a8",
"reference": "5b16e25a5355f1f3afdfc2f954a0a80aec4826a8",
"shasum": ""
},
"require": {
@ -60,7 +60,7 @@
],
"support": {
"issues": "https://github.com/composer/pcre/issues",
"source": "https://github.com/composer/pcre/tree/3.1.2"
"source": "https://github.com/composer/pcre/tree/3.1.3"
},
"funding": [
{
@ -76,20 +76,20 @@
"type": "tidelift"
}
],
"time": "2024-03-07T15:38:35+00:00"
"time": "2024-03-19T10:26:25+00:00"
},
{
"name": "composer/xdebug-handler",
"version": "3.0.3",
"version": "3.0.4",
"source": {
"type": "git",
"url": "https://github.com/composer/xdebug-handler.git",
"reference": "ced299686f41dce890debac69273b47ffe98a40c"
"reference": "4f988f8fdf580d53bdb2d1278fe93d1ed5462255"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/composer/xdebug-handler/zipball/ced299686f41dce890debac69273b47ffe98a40c",
"reference": "ced299686f41dce890debac69273b47ffe98a40c",
"url": "https://api.github.com/repos/composer/xdebug-handler/zipball/4f988f8fdf580d53bdb2d1278fe93d1ed5462255",
"reference": "4f988f8fdf580d53bdb2d1278fe93d1ed5462255",
"shasum": ""
},
"require": {
@ -100,7 +100,7 @@
"require-dev": {
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-strict-rules": "^1.1",
"symfony/phpunit-bridge": "^6.0"
"phpunit/phpunit": "^8.5 || ^9.6 || ^10.5"
},
"type": "library",
"autoload": {
@ -124,9 +124,9 @@
"performance"
],
"support": {
"irc": "irc://irc.freenode.org/composer",
"irc": "ircs://irc.libera.chat:6697/composer",
"issues": "https://github.com/composer/xdebug-handler/issues",
"source": "https://github.com/composer/xdebug-handler/tree/3.0.3"
"source": "https://github.com/composer/xdebug-handler/tree/3.0.4"
},
"funding": [
{
@ -142,7 +142,7 @@
"type": "tidelift"
}
],
"time": "2022-02-25T21:32:43+00:00"
"time": "2024-03-26T18:29:49+00:00"
},
{
"name": "pdepend/pdepend",

View File

@ -30,10 +30,8 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\UserGroup;
use FireflyIII\Models\UserRole;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
/**
* Class CreateGroupMemberships
@ -54,7 +52,6 @@ class CreateGroupMemberships extends Command
public function handle(): int
{
$this->createGroupMemberships();
$this->setDefaultGroups();
$this->friendlyPositive('Validated group memberships');
return 0;
@ -93,8 +90,7 @@ class CreateGroupMemberships extends Command
}
$membership = GroupMembership::where('user_id', $user->id)
->where('user_group_id', $userGroup->id)
->where('user_role_id', $userRole->id)->first()
;
->where('user_role_id', $userRole->id)->first();
if (null === $membership) {
GroupMembership::create(
[
@ -110,73 +106,4 @@ class CreateGroupMemberships extends Command
}
}
private function setDefaultGroups(): void
{
$users = User::get();
/** @var User $user */
foreach ($users as $user) {
$this->setDefaultGroup($user);
}
}
/**
* @throws FireflyException
*/
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()) {
/** @var UserGroup $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()));
/*
* Loop all the groups, expect to find at least ONE
* where you're owner, and it has your name. In that case, it's yours.
* Then we can safely return and stop.
*/
/** @var UserGroup $group */
foreach ($groups as $group) {
$group->default_administration = false;
$group->save();
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) {
$isOwner = true;
}
}
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) {
$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;
}
}
}
// if there is no group at all, create it.
$this->friendlyWarning(sprintf('User "%s" has no group with matching name. Will be created.', $user->email));
self::createGroupMembership($user);
}
}

View File

@ -1,35 +0,0 @@
<?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 {
/**
* Run the migrations.
*/
public function up(): void
{
try {
Schema::table(
'user_groups',
static function (Blueprint $table): void {
if (!Schema::hasColumn('user_groups', 'default_administration')) {
$table->boolean('default_administration')->default(false)->after('title');
}
}
);
} catch (QueryException $e) {
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
}
/**
* Reverse the migrations.
*/
public function down(): void {}
};