-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
InstallController.php
120 lines (103 loc) · 3.79 KB
/
InstallController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Install\Controller;
use Flarum\Http\RememberAccessToken;
use Flarum\Http\Rememberer;
use Flarum\Http\SessionAuthenticator;
use Flarum\Install\AdminUser;
use Flarum\Install\BaseUrl;
use Flarum\Install\DatabaseConfig;
use Flarum\Install\Installation;
use Flarum\Install\StepFailed;
use Flarum\Install\ValidationFailed;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
class InstallController implements RequestHandlerInterface
{
public function __construct(
protected Installation $installation,
protected SessionAuthenticator $authenticator,
protected Rememberer $rememberer
) {
}
public function handle(Request $request): ResponseInterface
{
$input = $request->getParsedBody();
$baseUrl = BaseUrl::fromUri($request->getUri());
// An access token we will use to auto-login the admin at the end of installation
$accessToken = Str::random(40);
try {
$pipeline = $this->installation
->baseUrl($baseUrl)
->databaseConfig($this->makeDatabaseConfig($input))
->adminUser($this->makeAdminUser($input))
->accessToken($accessToken)
->settings([
'forum_title' => Arr::get($input, 'forumTitle'),
'mail_from' => $baseUrl->toEmail('noreply'),
'welcome_title' => 'Welcome to '.Arr::get($input, 'forumTitle'),
])
->build();
} catch (ValidationFailed $e) {
return new Response\HtmlResponse($e->getMessage(), 500);
}
try {
$pipeline->run();
} catch (StepFailed $e) {
return new Response\HtmlResponse($e->getPrevious()->getMessage(), 500);
}
$session = $request->getAttribute('session');
// Because the Eloquent models cannot be used yet, we create a temporary in-memory object
// that won't interact with the database but can be passed to the authenticator and rememberer
$token = new RememberAccessToken();
$token->token = $accessToken;
$this->authenticator->logIn($session, $token);
return $this->rememberer->remember(new Response\EmptyResponse, $token);
}
private function makeDatabaseConfig(array $input): DatabaseConfig
{
$host = Arr::get($input, 'mysqlHost');
$port = 3306;
if (Str::contains($host, ':')) {
list($host, $port) = explode(':', $host, 2);
}
return new DatabaseConfig(
'mysql',
$host,
intval($port),
Arr::get($input, 'mysqlDatabase'),
Arr::get($input, 'mysqlUsername'),
Arr::get($input, 'mysqlPassword'),
Arr::get($input, 'tablePrefix')
);
}
/**
* @throws ValidationFailed
*/
private function makeAdminUser(array $input): AdminUser
{
return new AdminUser(
Arr::get($input, 'adminUsername'),
$this->getConfirmedAdminPassword($input),
Arr::get($input, 'adminEmail')
);
}
private function getConfirmedAdminPassword(array $input): string
{
$password = Arr::get($input, 'adminPassword');
$confirmation = Arr::get($input, 'adminPasswordConfirmation');
if ($password !== $confirmation) {
throw new ValidationFailed('The admin password did not match its confirmation.');
}
return $password;
}
}