2021-08-28 15:47:33 +02:00
< ? php
2021-09-16 14:21:35 +02:00
/*
* CreateGroupMemberships . php
2023-02-01 06:06:07 +01:00
* Copyright ( c ) 2023 james @ firefly - iii . org
2021-09-16 14:21:35 +02:00
*
* 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 />.
*/
declare ( strict_types = 1 );
2023-02-01 06:06:07 +01:00
namespace FireflyIII\Console\Commands\Integrity ;
2021-08-28 15:47:33 +02:00
2023-06-20 07:16:56 +02:00
use FireflyIII\Console\Commands\ShowsFriendlyMessages ;
2023-09-20 06:17:56 +02:00
use FireflyIII\Enums\UserRoleEnum ;
2021-08-28 15:47:33 +02:00
use FireflyIII\Exceptions\FireflyException ;
use FireflyIII\Models\GroupMembership ;
use FireflyIII\Models\UserGroup ;
use FireflyIII\Models\UserRole ;
2024-04-01 14:01:52 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface ;
2021-08-28 15:47:33 +02:00
use FireflyIII\User ;
use Illuminate\Console\Command ;
2024-04-01 14:01:52 +02:00
use Illuminate\Support\Facades\Log ;
2021-08-28 15:47:33 +02:00
/**
* Class CreateGroupMemberships
*/
class CreateGroupMemberships extends Command
{
2023-06-20 07:16:56 +02:00
use ShowsFriendlyMessages ;
2023-12-02 12:56:48 +01:00
public const string CONFIG_NAME = '560_create_group_memberships' ;
2024-04-01 14:04:22 +02:00
protected $description = 'Update group memberships' ;
protected $signature = 'firefly-iii:create-group-memberships' ;
2021-08-28 15:47:33 +02:00
2023-06-21 12:34:58 +02:00
/**
* Execute the console command .
*
* @ throws FireflyException
*/
public function handle () : int
{
$this -> createGroupMemberships ();
2024-04-01 14:01:52 +02:00
$this -> setDefaultGroups ();
2023-06-21 12:34:58 +02:00
$this -> friendlyPositive ( 'Validated group memberships' );
return 0 ;
}
2024-02-22 20:11:09 +01:00
/**
* @ throws FireflyException
*/
private function createGroupMemberships () : void
{
$users = User :: get ();
/** @var User $user */
foreach ( $users as $user ) {
self :: createGroupMembership ( $user );
}
}
2021-08-28 15:47:33 +02:00
/**
2023-03-26 19:19:10 +02:00
* TODO move to helper .
2023-05-29 13:56:55 +02:00
*
2021-08-28 15:47:33 +02:00
* @ throws FireflyException
*/
2023-03-26 19:19:10 +02:00
public static function createGroupMembership ( User $user ) : void
2021-08-28 15:47:33 +02:00
{
2023-02-01 06:06:07 +01:00
// check if membership exists
2024-04-01 14:04:22 +02:00
$userGroup = UserGroup :: where ( 'title' , $user -> email ) -> first ();
2023-02-01 06:06:07 +01:00
if ( null === $userGroup ) {
2024-04-01 14:01:52 +02:00
$userGroup = UserGroup :: create ([ 'title' => $user -> email , 'default_administration' => true ]);
2023-02-01 06:06:07 +01:00
}
2024-04-01 14:04:22 +02:00
$userRole = UserRole :: where ( 'title' , UserRoleEnum :: OWNER -> value ) -> first ();
2021-08-28 15:47:33 +02:00
if ( null === $userRole ) {
2023-02-01 06:06:07 +01:00
throw new FireflyException ( 'Firefly III could not find a user role. Please make sure all migrations have run.' );
2021-08-28 15:47:33 +02:00
}
2023-02-01 06:06:07 +01:00
$membership = GroupMembership :: where ( 'user_id' , $user -> id )
2024-04-01 14:04:22 +02:00
-> where ( 'user_group_id' , $userGroup -> id )
-> where ( 'user_role_id' , $userRole -> id ) -> first ()
;
2021-08-28 15:47:33 +02:00
if ( null === $membership ) {
2023-02-01 06:06:07 +01:00
GroupMembership :: create (
[
'user_id' => $user -> id ,
'user_role_id' => $userRole -> id ,
'user_group_id' => $userGroup -> id ,
]
);
}
if ( null === $user -> user_group_id ) {
$user -> user_group_id = $userGroup -> id ;
$user -> save ();
2021-08-28 15:47:33 +02:00
}
}
2024-04-01 14:01:52 +02:00
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 ));
2024-04-01 14:04:22 +02:00
2024-04-01 14:01:52 +02:00
/** @var UserRepositoryInterface $repository */
$repository = app ( UserRepositoryInterface :: class );
$groups = $repository -> getUserGroups ( $user );
2024-04-01 14:04:22 +02:00
if ( 1 === $groups -> count ()) {
2024-04-01 14:01:52 +02:00
/** @var UserGroup $first */
2024-04-01 14:04:22 +02:00
$first = $groups -> first ();
2024-04-01 14:01:52 +02:00
$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 ));
2024-04-01 14:04:22 +02:00
2024-04-01 14:01:52 +02:00
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 */
2024-04-01 14:04:22 +02:00
foreach ( $groups as $group ) {
2024-04-01 14:01:52 +02:00
$group -> default_administration = false ;
$group -> save ();
2024-04-01 14:04:22 +02:00
if ( $group -> title === $user -> email ) {
$roles = $repository -> getRolesInGroup ( $user , $group -> id );
2024-04-01 14:01:52 +02:00
Log :: debug ( sprintf ( 'Group #%d ("%s")' , $group -> id , $group -> title ), $roles );
$isOwner = false ;
2024-04-01 14:04:22 +02:00
foreach ( $roles as $role ) {
if ( $role === UserRoleEnum :: OWNER -> value ) {
2024-04-01 14:01:52 +02:00
$isOwner = true ;
}
}
2024-04-01 14:04:22 +02:00
if ( true === $isOwner ) {
2024-04-01 14:01:52 +02:00
// 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 ));
2024-04-01 14:04:22 +02:00
2024-04-01 14:01:52 +02:00
return ;
}
2024-04-01 14:04:22 +02:00
if ( false === $isOwner ) {
2024-04-01 14:01:52 +02:00
$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 );
2024-04-01 14:04:22 +02:00
2024-04-01 14:01:52 +02:00
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 );
}
2021-08-28 15:47:33 +02:00
}