Code that add extra check for #1039

This commit is contained in:
James Cole 2017-12-05 20:50:04 +01:00
parent 1b56765da3
commit e4a0a718d1
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 53 additions and 1 deletions

View File

@ -29,7 +29,10 @@ use FireflyIII\Mail\ConfirmEmailChangeMail;
use FireflyIII\Mail\RegisteredUser as RegisteredUserMail;
use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail;
use FireflyIII\Mail\UndoEmailChangeMail;
use FireflyIII\Models\Role;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Auth\Events\Login;
use Log;
use Mail;
use Preferences;
@ -64,6 +67,46 @@ class UserEventHandler
return true;
}
/**
* @param Login $event
*
* @return bool
*/
public function checkSingleUserIsAdmin(Login $event): bool
{
Log::debug('In checkSingleUserIsAdmin');
$user = $event->user;
$count = User::count();
if ($count > 1) {
// if more than one user, do nothing.
Log::debug(sprintf('System has %d users, will not change users roles.', $count));
return true;
}
// user is only user but has admin role
if ($count === 1 && $user->hasRole('owner')) {
Log::debug(sprintf('User #%d is only user but has role owner so all is well.', $user->id));
return true;
}
// user is the only user but does not have role "owner".
$role = Role::where('name', 'owner')->first();
if (is_null($role)) {
// create role, does not exist. Very strange situation so let's raise a big fuss about it.
$role = Role::create(['name' => 'owner', 'display_name' => 'Site Owner', 'description' => 'User runs this instance of FF3']);
Log::error('Could not find role "owner". This is weird.');
}
Log::info(sprintf('Gave user #%d role #%d ("%s")', $user->id, $role->id, $role->name));
// give user the role
$user->attachRole($role);
$user->save();
return true;
}
/**
* @param UserChangedEmail $event
*

View File

@ -41,6 +41,8 @@ class Role extends Model
'updated_at' => 'datetime',
];
protected $fillable = ['name','display_name','description'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/

View File

@ -22,12 +22,14 @@ declare(strict_types=1);
namespace FireflyIII\Providers;
use FireflyIII\Events\RegisteredUser;
use FireflyIII\Models\Account;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalMeta;
use Illuminate\Auth\Events\Login;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Log;
@ -44,10 +46,15 @@ class EventServiceProvider extends ServiceProvider
protected $listen
= [
// is a User related event.
'FireflyIII\Events\RegisteredUser' => [
RegisteredUser::class => [
'FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationMail',
'FireflyIII\Handlers\Events\UserEventHandler@attachUserRole',
],
// is a User related event.
Login::class => [
'FireflyIII\Handlers\Events\UserEventHandler@checkSingleUserIsAdmin',
],
// is a User related event.
'FireflyIII\Events\RequestedNewPassword' => [
'FireflyIII\Handlers\Events\UserEventHandler@sendNewPassword',