New user groups and memberships

This commit is contained in:
James Cole
2021-08-28 15:47:33 +02:00
parent a14c9438ad
commit 10787aada8
12 changed files with 483 additions and 8 deletions

View File

@@ -0,0 +1,130 @@
<?php
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\UserGroup;
use FireflyIII\Models\UserRole;
use FireflyIII\User;
use Illuminate\Console\Command;
use Log;
/**
* Class CreateGroupMemberships
*/
class CreateGroupMemberships extends Command
{
public const CONFIG_NAME = '560_create_group_memberships';
/**
* The console command description.
*
* @var string
*/
protected $description = 'SOME DESCRIPTION';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:create-group-memberships {--F|force : Force the execution of this command.}';
/**
* Execute the console command.
*
* @return int
* @throws FireflyException
*/
public function handle(): int
{
$start = microtime(true);
if ($this->isExecuted() && true !== $this->option('force')) {
$this->warn('This command has already been executed.');
return 0;
}
$this->createGroupMemberships();
$this->markAsExecuted();
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('in %s seconds.', $end));
return 0;
}
/**
* @return bool
*/
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
return (bool)$configVar->data;
}
return false;
}
/**
*
* @throws FireflyException
*/
private function createGroupMemberships(): void
{
$users = User::get();
/** @var User $user */
foreach ($users as $user) {
Log::debug(sprintf('Manage group memberships for user #%d', $user->id));
if (!$this->hasGroupMembership($user)) {
Log::debug(sprintf('User #%d has no main group.', $user->id));
$this->createGroupMembership($user);
}
Log::debug(sprintf('Done with user #%d', $user->id));
}
}
/**
* @param User $user
*
* @return bool
*/
private function hasGroupMembership(User $user): bool
{
return $user->groupMemberships()->count() > 0;
}
/**
* @param User $user
*
* @throws FireflyException
*/
private function createGroupMembership(User $user): void
{
$userGroup = UserGroup::create(['title' => $user->email]);
$userRole = UserRole::where('title', UserRole::FULL)->first();
if (null === $userRole) {
throw new FireflyException('Firefly III could not find a user role. Please make sure all validations have run.');
}
$membership = GroupMembership::create(
[
'user_id' => $user->id,
'user_role_id' => $userRole->id,
'user_group_id' => $userGroup->id,
]
);
if (null === $membership) {
throw new FireflyException('Firefly III could not create user group management object. Please make sure all validations have run.');
}
Log::debug(sprintf('User #%d now has main group.', $user->id));
}
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
}

View File

@@ -35,6 +35,9 @@ use FireflyIII\Mail\NewIPAddressWarningMail;
use FireflyIII\Mail\RegisteredUser as RegisteredUserMail;
use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail;
use FireflyIII\Mail\UndoEmailChangeMail;
use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\UserGroup;
use FireflyIII\Models\UserRole;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Auth\Events\Login;
@@ -248,6 +251,32 @@ class UserEventHandler
return true;
}
/**
* @param RegisteredUser $event
*
* @return bool
* @throws FireflyException
*/
public function createGroupMembership(RegisteredUser $event): bool
{
$user = $event->user;
// create a new group.
$group = UserGroup::create(['title' => $user->email]);
$role = UserRole::where('title', UserRole::FULL)->first();
if (null === $role) {
throw new FireflyException('The user role is unexpectedly empty. Did you run all migrations?');
}
GroupMembership::create(
[
'user_id' => $user->id,
'user_group_id' => $group->id,
'user_role_id' => $role->id,
]
);
return true;
}
/**
* This method will send the user a registration mail, welcoming him or her to Firefly III.
* This message is only sent when the configuration of Firefly III says so.

View File

@@ -28,6 +28,7 @@ use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Http\Middleware\Installer;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\GroupMembership;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\User;

View File

@@ -0,0 +1,58 @@
<?php
/*
* GroupMembership.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Models;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Class GroupMembership
*/
class GroupMembership extends Model
{
protected $fillable = ['user_id', 'user_group_id', 'user_role_id'];
/**
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* @return BelongsTo
*/
public function userGroup(): BelongsTo
{
return $this->belongsTo(UserGroup::class);
}
/**
* @return BelongsTo
*/
public function userRole(): BelongsTo
{
return $this->belongsTo(UserRole::class);
}
}

43
app/Models/UserGroup.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
/*
* UserGroup.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class UserGroup
*/
class UserGroup extends Model
{
protected $fillable = ['title'];
/**
* @codeCoverageIgnore
*
* @return HasMany
*/
public function groupMemberships(): HasMany
{
return $this->hasMany(GroupMembership::class);
}
}

50
app/Models/UserRole.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
/*
* UserRole.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class UserRole
*/
class UserRole extends Model
{
public const READ_ONLY = 'ro';
public const CHANGE_TRANSACTIONS = 'change_tx';
public const CHANGE_RULES = 'change_rules';
public const CHANGE_PIGGY_BANKS = 'change_piggies';
public const CHANGE_REPETITIONS = 'change_reps';
public const VIEW_REPORTS = 'view_reports';
public const FULL = 'full';
protected $fillable = ['title'];
/**
* @codeCoverageIgnore
*
* @return HasMany
*/
public function groupMemberships(): HasMany
{
return $this->hasMany(GroupMembership::class);
}
}

View File

@@ -68,6 +68,7 @@ class EventServiceProvider extends ServiceProvider
RegisteredUser::class => [
'FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationMail',
'FireflyIII\Handlers\Events\UserEventHandler@attachUserRole',
'FireflyIII\Handlers\Events\UserEventHandler@createGroupMembership',
],
// is a User related event.
Login::class => [

View File

@@ -34,6 +34,7 @@ use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\GroupMembership;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Preference;
@@ -136,14 +137,14 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @method static Builder|User whereObjectguid($value)
* @property string|null $provider
* @method static Builder|User whereProvider($value)
* @property-read \Illuminate\Database\Eloquent\Collection|ObjectGroup[] $objectGroups
* @property-read int|null $object_groups_count
* @property-read \Illuminate\Database\Eloquent\Collection|Webhook[] $webhooks
* @property-read int|null $webhooks_count
* @property string|null $two_factor_secret
* @property string|null $two_factor_recovery_codes
* @property string|null $guid
* @property string|null $domain
* @property-read \Illuminate\Database\Eloquent\Collection|ObjectGroup[] $objectGroups
* @property-read int|null $object_groups_count
* @property-read \Illuminate\Database\Eloquent\Collection|Webhook[] $webhooks
* @property-read int|null $webhooks_count
* @property string|null $two_factor_secret
* @property string|null $two_factor_recovery_codes
* @property string|null $guid
* @property string|null $domain
* @method static Builder|User whereDomain($value)
* @method static Builder|User whereGuid($value)
* @method static Builder|User whereTwoFactorRecoveryCodes($value)
@@ -212,6 +213,16 @@ class User extends Authenticatable
return $this->hasMany(Account::class);
}
/**
* @codeCoverageIgnore
*
* @return HasMany
*/
public function groupMemberships(): HasMany
{
return $this->hasMany(GroupMembership::class)->with(['userGroup','userRole']);
}
/**
* @codeCoverageIgnore
* Link to attachments
@@ -449,6 +460,7 @@ class User extends Authenticatable
}
// start LDAP related code
/**
* Get the database column name of the domain.
*