2021-08-28 08:47:33 -05:00
|
|
|
<?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]);
|
2021-08-29 23:37:55 -05:00
|
|
|
$userRole = UserRole::where('title', UserRole::OWNER)->first();
|
2021-08-28 08:47:33 -05:00
|
|
|
|
|
|
|
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.');
|
|
|
|
}
|
2021-08-29 23:37:55 -05:00
|
|
|
$user->user_group_id = $userGroup->id;
|
|
|
|
$user->save();
|
|
|
|
|
2021-08-28 08:47:33 -05:00
|
|
|
Log::debug(sprintf('User #%d now has main group.', $user->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function markAsExecuted(): void
|
|
|
|
{
|
|
|
|
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
|
|
|
}
|
|
|
|
}
|