Skip to content

Commit

Permalink
Refactor create superuser command
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelWei committed Dec 16, 2024
1 parent bbb2799 commit 927abf6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
45 changes: 16 additions & 29 deletions app/Console/Commands/CreateSuperuserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace App\Console\Commands;

use App\Http\Requests\UserRequest;
use App\Models\Role;
use App\Models\User;
use App\Rules\Password;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

use function Laravel\Prompts\password;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;

/**
* Command class that makes it possible to create an new superuser.
Expand Down Expand Up @@ -65,36 +69,19 @@ public function handle()
$this->info('Creating an new superuser, please notify your inputs.');

$data = [];
$data['firstname'] = $this->ask('Firstname');
$data['lastname'] = $this->ask('Lastname');
$data['email'] = $this->ask('E-Mail');
$data['user_locale'] = $this->ask('Locale (possible values: '.implode(',', array_keys(config('app.enabled_locales'))).')');
$data['password'] = $this->secret('Password');
$data['password_confirmation'] = $this->secret('Password Confirmation');
$data['generate_password'] = false;
$data['bbb_skip_check_audio'] = false;
$data['roles'] = [$superuserRole->id];
$data['timezone'] = 'UTC';

$validator = Validator::make($data, (new UserRequest)->rules());

if ($validator->fails()) {
$this->info('Something went wrong, please see the error messages below for more information.');

foreach ($validator->errors()->all() as $error) {
$this->error($error);
}

return 1;
}
$firstname = text('Firstname', validate: ['firstname' => 'required|max:255']);
$lastname = text('Lastname', validate: ['lastname' => 'required|max:255']);
$email = text('E-Mail', validate: ['email' => ['required', 'max:255', 'email', Rule::unique('users', 'email')->where('authenticator', 'local')]]);
$locale = select('Locale', array_keys(config('app.enabled_locales')));
$password = password('Password', required: true, validate: ['min:8', new Password]);

$user = new User;

$user->firstname = $data['firstname'];
$user->lastname = $data['lastname'];
$user->email = $data['email'];
$user->locale = $data['user_locale'];
$user->password = Hash::make($data['password']);
$user->firstname = $firstname;
$user->lastname = $lastname;
$user->email = $email;
$user->locale = $locale;
$user->password = Hash::make($password);
$user->email_verified_at = $user->freshTimestamp();

$user->save();
Expand Down
4 changes: 2 additions & 2 deletions tests/Backend/Feature/api/v1/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function test_create()
'lastname' => $request['lastname'],
'user_locale' => $request['user_locale'],
'email' => $request['email'],
'roles' => [['id' => $role->id, 'name' => $role->name, 'automatic' => false]],
'roles' => [['id' => $role->id, 'superuser' => false, 'name' => $role->name, 'automatic' => false]],
'authenticator' => 'local',
]);
}
Expand Down Expand Up @@ -1198,7 +1198,7 @@ public function test_show()
'firstname' => $externalUser->firstname,
'lastname' => $externalUser->lastname,
'authenticator' => 'ldap',
'roles' => [['id' => $role->id, 'name' => $role->name, 'automatic' => false]],
'roles' => [['id' => $role->id, 'name' => $role->name, 'superuser' => false, 'automatic' => false]],
]);

// Check user image path
Expand Down
19 changes: 1 addition & 18 deletions tests/Backend/Unit/Console/CreateSuperuserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@ protected function setUp(): void
]);
}

public function test_invalid_inputs()
{
Role::factory()->create(['name' => 'superuser', 'superuser' => true]);

$this->artisan('users:create:superuser')
->expectsOutput('Creating an new superuser, please notify your inputs.')
->expectsQuestion('Firstname', str_repeat('a', 256))
->expectsQuestion('Lastname', str_repeat('a', 256))
->expectsQuestion('E-Mail', str_repeat('a', 256))
->expectsQuestion('Locale (possible values: '.implode(',', array_keys(config('app.enabled_locales'))).')', str_repeat('a', 256))
->expectsQuestion('Password', 'Test')
->expectsQuestion('Password Confirmation', 'Test1234')
->expectsOutput('Something went wrong, please see the error messages below for more information.')
->assertExitCode(1);
}

public function test_missing_role()
{
Role::factory()->create(['name' => 'superuser']);
Expand All @@ -60,9 +44,8 @@ public function test_valid_inputs()
->expectsQuestion('Firstname', $this->faker->firstName)
->expectsQuestion('Lastname', $this->faker->lastName)
->expectsQuestion('E-Mail', $this->faker->email)
->expectsQuestion('Locale (possible values: '.implode(',', array_keys(config('app.enabled_locales'))).')', array_keys(config('app.enabled_locales'))[0])
->expectsQuestion('Locale', array_keys(config('app.enabled_locales'))[0])
->expectsQuestion('Password', 'Test_1234')
->expectsQuestion('Password Confirmation', 'Test_1234')
->expectsOutput('New superuser created successfully.')
->assertExitCode(0);

Expand Down

0 comments on commit 927abf6

Please sign in to comment.