Skip to content

Commit f83cc83

Browse files
committed
Added external-auth-id option to create-admin command
- Added tests to cover. - Refactored some existing testing. - Requires password or external_auth_id to be provided. Defaults to password. - Randomly sets password to 32 digit random chars if external_auth_id provided instead. For #3222
1 parent 1721543 commit f83cc83

File tree

3 files changed

+89
-34
lines changed

3 files changed

+89
-34
lines changed

app/Console/Commands/CreateAdmin.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace BookStack\Console\Commands;
44

55
use BookStack\Auth\UserRepo;
6+
use BookStack\Exceptions\NotFoundException;
67
use Illuminate\Console\Command;
78
use Illuminate\Support\Facades\Validator;
9+
use Illuminate\Support\Str;
810
use Illuminate\Validation\Rules\Password;
911
use Illuminate\Validation\Rules\Unique;
1012
use Symfony\Component\Console\Command\Command as SymfonyCommand;
@@ -19,7 +21,8 @@ class CreateAdmin extends Command
1921
protected $signature = 'bookstack:create-admin
2022
{--email= : The email address for the new admin user}
2123
{--name= : The name of the new admin user}
22-
{--password= : The password to assign to the new admin user}';
24+
{--password= : The password to assign to the new admin user}
25+
{--external-auth-id= : The external authentication system id for the new admin user (SAML2/LDAP/OIDC)}';
2326

2427
/**
2528
* The console command description.
@@ -42,28 +45,35 @@ public function __construct(UserRepo $userRepo)
4245
/**
4346
* Execute the console command.
4447
*
45-
* @throws \BookStack\Exceptions\NotFoundException
48+
* @throws NotFoundException
4649
*
4750
* @return mixed
4851
*/
4952
public function handle()
5053
{
51-
$details = $this->options();
54+
$details = $this->snakeCaseOptions();
5255

5356
if (empty($details['email'])) {
5457
$details['email'] = $this->ask('Please specify an email address for the new admin user');
5558
}
59+
5660
if (empty($details['name'])) {
5761
$details['name'] = $this->ask('Please specify a name for the new admin user');
5862
}
63+
5964
if (empty($details['password'])) {
60-
$details['password'] = $this->ask('Please specify a password for the new admin user (8 characters min)');
65+
if (empty($details['external_auth_id'])) {
66+
$details['password'] = $this->ask('Please specify a password for the new admin user (8 characters min)');
67+
} else {
68+
$details['password'] = Str::random(32);
69+
}
6170
}
6271

6372
$validator = Validator::make($details, [
64-
'email' => ['required', 'email', 'min:5', new Unique('users', 'email')],
65-
'name' => ['required', 'min:2'],
66-
'password' => ['required', Password::default()],
73+
'email' => ['required', 'email', 'min:5', new Unique('users', 'email')],
74+
'name' => ['required', 'min:2'],
75+
'password' => ['required_without:external_auth_id', Password::default()],
76+
'external_auth_id' => ['required_without:password'],
6777
]);
6878

6979
if ($validator->fails()) {
@@ -84,4 +94,13 @@ public function handle()
8494

8595
return SymfonyCommand::SUCCESS;
8696
}
97+
98+
protected function snakeCaseOptions(): array
99+
{
100+
$returnOpts = [];
101+
foreach ($this->options() as $key => $value) {
102+
$returnOpts[str_replace('-', '_', $key)] = $value;
103+
}
104+
return $returnOpts;
105+
}
87106
}

tests/Commands/AddAdminCommandTest.php

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Tests\Commands;
4+
5+
use BookStack\Auth\User;
6+
use Illuminate\Support\Facades\Auth;
7+
use Tests\TestCase;
8+
9+
class CreateAdminCommandTest extends TestCase
10+
{
11+
public function test_standard_command_usage()
12+
{
13+
$this->artisan('bookstack:create-admin', [
14+
'--email' => 'admintest@example.com',
15+
'--name' => 'Admin Test',
16+
'--password' => 'testing-4',
17+
])->assertExitCode(0);
18+
19+
$this->assertDatabaseHas('users', [
20+
'email' => 'admintest@example.com',
21+
'name' => 'Admin Test',
22+
]);
23+
24+
/** @var User $user */
25+
$user = User::query()->where('email', '=', 'admintest@example.com')->first();
26+
$this->assertTrue($user->hasSystemRole('admin'));
27+
$this->assertTrue(Auth::attempt(['email' => 'admintest@example.com', 'password' => 'testing-4']));
28+
}
29+
30+
public function test_providing_external_auth_id()
31+
{
32+
$this->artisan('bookstack:create-admin', [
33+
'--email' => 'admintest@example.com',
34+
'--name' => 'Admin Test',
35+
'--external-auth-id' => 'xX_admin_Xx',
36+
])->assertExitCode(0);
37+
38+
$this->assertDatabaseHas('users', [
39+
'email' => 'admintest@example.com',
40+
'name' => 'Admin Test',
41+
'external_auth_id' => 'xX_admin_Xx',
42+
]);
43+
44+
/** @var User $user */
45+
$user = User::query()->where('email', '=', 'admintest@example.com')->first();
46+
$this->assertNotEmpty($user->password);
47+
}
48+
49+
public function test_password_required_if_external_auth_id_not_given()
50+
{
51+
$this->artisan('bookstack:create-admin', [
52+
'--email' => 'admintest@example.com',
53+
'--name' => 'Admin Test',
54+
])->expectsQuestion('Please specify a password for the new admin user (8 characters min)', 'hunter2000')
55+
->assertExitCode(0);
56+
57+
$this->assertDatabaseHas('users', [
58+
'email' => 'admintest@example.com',
59+
'name' => 'Admin Test',
60+
]);
61+
$this->assertTrue(Auth::attempt(['email' => 'admintest@example.com', 'password' => 'hunter2000']));
62+
}
63+
}

0 commit comments

Comments
 (0)