mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
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:
parent
cc32578c5f
commit
5b83c33039
28
.ci/phpmd/composer.lock
generated
28
.ci/phpmd/composer.lock
generated
@ -9,16 +9,16 @@
|
|||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
{
|
{
|
||||||
"name": "composer/pcre",
|
"name": "composer/pcre",
|
||||||
"version": "3.1.2",
|
"version": "3.1.3",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/composer/pcre.git",
|
"url": "https://github.com/composer/pcre.git",
|
||||||
"reference": "4775f35b2d70865807c89d32c8e7385b86eb0ace"
|
"reference": "5b16e25a5355f1f3afdfc2f954a0a80aec4826a8"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/composer/pcre/zipball/4775f35b2d70865807c89d32c8e7385b86eb0ace",
|
"url": "https://api.github.com/repos/composer/pcre/zipball/5b16e25a5355f1f3afdfc2f954a0a80aec4826a8",
|
||||||
"reference": "4775f35b2d70865807c89d32c8e7385b86eb0ace",
|
"reference": "5b16e25a5355f1f3afdfc2f954a0a80aec4826a8",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@ -60,7 +60,7 @@
|
|||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/composer/pcre/issues",
|
"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": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@ -76,20 +76,20 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2024-03-07T15:38:35+00:00"
|
"time": "2024-03-19T10:26:25+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "composer/xdebug-handler",
|
"name": "composer/xdebug-handler",
|
||||||
"version": "3.0.3",
|
"version": "3.0.4",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/composer/xdebug-handler.git",
|
"url": "https://github.com/composer/xdebug-handler.git",
|
||||||
"reference": "ced299686f41dce890debac69273b47ffe98a40c"
|
"reference": "4f988f8fdf580d53bdb2d1278fe93d1ed5462255"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/composer/xdebug-handler/zipball/ced299686f41dce890debac69273b47ffe98a40c",
|
"url": "https://api.github.com/repos/composer/xdebug-handler/zipball/4f988f8fdf580d53bdb2d1278fe93d1ed5462255",
|
||||||
"reference": "ced299686f41dce890debac69273b47ffe98a40c",
|
"reference": "4f988f8fdf580d53bdb2d1278fe93d1ed5462255",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@ -100,7 +100,7 @@
|
|||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpstan/phpstan": "^1.0",
|
"phpstan/phpstan": "^1.0",
|
||||||
"phpstan/phpstan-strict-rules": "^1.1",
|
"phpstan/phpstan-strict-rules": "^1.1",
|
||||||
"symfony/phpunit-bridge": "^6.0"
|
"phpunit/phpunit": "^8.5 || ^9.6 || ^10.5"
|
||||||
},
|
},
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
@ -124,9 +124,9 @@
|
|||||||
"performance"
|
"performance"
|
||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"irc": "irc://irc.freenode.org/composer",
|
"irc": "ircs://irc.libera.chat:6697/composer",
|
||||||
"issues": "https://github.com/composer/xdebug-handler/issues",
|
"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": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@ -142,7 +142,7 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2022-02-25T21:32:43+00:00"
|
"time": "2024-03-26T18:29:49+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "pdepend/pdepend",
|
"name": "pdepend/pdepend",
|
||||||
|
@ -30,10 +30,8 @@ use FireflyIII\Exceptions\FireflyException;
|
|||||||
use FireflyIII\Models\GroupMembership;
|
use FireflyIII\Models\GroupMembership;
|
||||||
use FireflyIII\Models\UserGroup;
|
use FireflyIII\Models\UserGroup;
|
||||||
use FireflyIII\Models\UserRole;
|
use FireflyIII\Models\UserRole;
|
||||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CreateGroupMemberships
|
* Class CreateGroupMemberships
|
||||||
@ -43,8 +41,8 @@ class CreateGroupMemberships extends Command
|
|||||||
use ShowsFriendlyMessages;
|
use ShowsFriendlyMessages;
|
||||||
|
|
||||||
public const string CONFIG_NAME = '560_create_group_memberships';
|
public const string CONFIG_NAME = '560_create_group_memberships';
|
||||||
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.
|
||||||
@ -54,7 +52,6 @@ class CreateGroupMemberships extends Command
|
|||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
$this->createGroupMemberships();
|
$this->createGroupMemberships();
|
||||||
$this->setDefaultGroups();
|
|
||||||
$this->friendlyPositive('Validated group memberships');
|
$this->friendlyPositive('Validated group memberships');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -81,20 +78,19 @@ class CreateGroupMemberships extends Command
|
|||||||
public static function createGroupMembership(User $user): void
|
public static function createGroupMembership(User $user): void
|
||||||
{
|
{
|
||||||
// check if membership exists
|
// check if membership exists
|
||||||
$userGroup = UserGroup::where('title', $user->email)->first();
|
$userGroup = UserGroup::where('title', $user->email)->first();
|
||||||
if (null === $userGroup) {
|
if (null === $userGroup) {
|
||||||
$userGroup = UserGroup::create(['title' => $user->email, 'default_administration' => true]);
|
$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) {
|
if (null === $userRole) {
|
||||||
throw new FireflyException('Firefly III could not find a user role. Please make sure all migrations have run.');
|
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)
|
$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(
|
||||||
[
|
[
|
||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -84,13 +84,13 @@ class UserGroupTransformer extends AbstractTransformer
|
|||||||
public function transform(UserGroup $userGroup): array
|
public function transform(UserGroup $userGroup): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => $userGroup->id,
|
'id' => $userGroup->id,
|
||||||
'created_at' => $userGroup->created_at->toAtomString(),
|
'created_at' => $userGroup->created_at->toAtomString(),
|
||||||
'updated_at' => $userGroup->updated_at->toAtomString(),
|
'updated_at' => $userGroup->updated_at->toAtomString(),
|
||||||
'in_use' => $this->inUse[$userGroup->id] ?? false,
|
'in_use' => $this->inUse[$userGroup->id] ?? false,
|
||||||
'title' => $userGroup->title,
|
'title' => $userGroup->title,
|
||||||
'can_see_members' => $this->membershipsVisible[$userGroup->id] ?? false,
|
'can_see_members' => $this->membershipsVisible[$userGroup->id] ?? false,
|
||||||
'members' => $this->memberships[$userGroup->id] ?? [],
|
'members' => $this->memberships[$userGroup->id] ?? [],
|
||||||
];
|
];
|
||||||
// if the user has a specific role in this group, then collect the memberships.
|
// if the user has a specific role in this group, then collect the memberships.
|
||||||
}
|
}
|
||||||
|
@ -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 {}
|
|
||||||
};
|
|
Loading…
Reference in New Issue
Block a user