Files
firefly-iii/app/Console/Commands/System/CreatesFirstUser.php

87 lines
3.0 KiB
PHP
Raw Normal View History

2020-10-04 07:56:03 +02:00
<?php
2021-01-29 18:50:35 +01:00
/*
* CreateFirstUser.php
2023-04-16 07:33:12 +02:00
* Copyright (c) 2023 james@firefly-iii.org
2021-01-29 18:50:35 +01: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/>.
*/
2020-10-04 07:56:03 +02:00
declare(strict_types=1);
2023-04-16 07:33:12 +02:00
namespace FireflyIII\Console\Commands\System;
2020-10-04 07:56:03 +02:00
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2020-10-04 07:56:03 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
2025-02-23 12:47:04 +01:00
use Illuminate\Support\Str;
2020-10-04 07:56:03 +02:00
2024-12-27 09:30:41 +01:00
class CreatesFirstUser extends Command
2020-10-04 07:56:03 +02:00
{
use ShowsFriendlyMessages;
2021-03-21 09:15:40 +01:00
protected $description = 'Creates a new user and gives admin rights. Outputs the password on the command line. Strictly for testing.';
2023-11-05 09:54:53 +01:00
2024-12-27 09:30:41 +01:00
protected $signature = 'system:create-first-user {email}';
2020-10-04 07:56:03 +02:00
private UserRepositoryInterface $repository;
/**
* Execute the console command.
*/
public function handle(): int
{
2025-01-03 15:04:15 +01:00
if ('testing' !== config('app.env')) {
$this->friendlyError('This command only works in the testing environment.');
2021-03-21 09:15:40 +01:00
2020-10-04 07:56:03 +02:00
return 1;
}
$this->stupidLaravel();
$count = $this->repository->count();
2020-10-04 07:56:03 +02:00
if ($count > 0) {
$this->friendlyError('Already have more than zero users in DB.');
2021-03-21 09:15:40 +01:00
2020-10-04 07:56:03 +02:00
return 1;
}
$data = [
'blocked' => false,
'blocked_code' => null,
'email' => $this->argument('email'),
'role' => 'owner',
];
2025-02-23 12:47:04 +01:00
$password = Str::random(24);
2020-10-04 07:56:03 +02:00
$user = $this->repository->store($data);
$user->password = Hash::make($password);
$user->save();
2025-02-23 12:47:04 +01:00
$user->setRememberToken(Str::random(60));
2020-10-04 07:56:03 +02:00
$this->friendlyInfo(sprintf('Created new admin user (ID #%d) with email address "%s" and password "%s".', $user->id, $user->email, $password));
$this->friendlyWarning('Change this password.');
2021-03-21 09:15:40 +01:00
2020-10-04 07:56:03 +02:00
return 0;
}
/**
* Laravel will execute ALL __construct() methods for ALL commands whenever a SINGLE command is
* executed. This leads to noticeable slow-downs and class calls. To prevent this, this method should
* be called from the handle method instead of using the constructor to initialize the command.
*/
private function stupidLaravel(): void
{
$this->repository = app(UserRepositoryInterface::class);
}
}