diff --git a/app/Console/Commands/CreateSuperuserCommand.php b/app/Console/Commands/CreateSuperuserCommand.php index a753ebc99..a48058511 100644 --- a/app/Console/Commands/CreateSuperuserCommand.php +++ b/app/Console/Commands/CreateSuperuserCommand.php @@ -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. @@ -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(); diff --git a/tests/Backend/Feature/api/v1/UserTest.php b/tests/Backend/Feature/api/v1/UserTest.php index 62a6ba1e5..3c0b2dec8 100644 --- a/tests/Backend/Feature/api/v1/UserTest.php +++ b/tests/Backend/Feature/api/v1/UserTest.php @@ -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', ]); } @@ -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 diff --git a/tests/Backend/Unit/Console/CreateSuperuserTest.php b/tests/Backend/Unit/Console/CreateSuperuserTest.php index 55d0116d5..5efd074a3 100644 --- a/tests/Backend/Unit/Console/CreateSuperuserTest.php +++ b/tests/Backend/Unit/Console/CreateSuperuserTest.php @@ -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']); @@ -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);