julienmonnerie/kirby/config/api/routes/system.php

91 lines
1.9 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
/**
* System Routes
*/
return [
2022-08-31 15:02:43 +02:00
[
'pattern' => 'system',
'method' => 'GET',
'auth' => false,
'action' => function () {
$system = $this->kirby()->system();
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
if ($this->kirby()->user()) {
return $system;
}
2022-12-19 14:56:05 +01:00
$info = match ($system->isOk()) {
true => $this->resolve($system)->view('login')->toArray(),
false => $this->resolve($system)->view('troubleshooting')->toArray()
};
return [
'status' => 'ok',
'data' => $info,
'type' => 'model'
];
2022-08-31 15:02:43 +02:00
}
],
2025-04-21 18:57:21 +02:00
[
'pattern' => 'system/method-test',
'method' => 'PATCH',
'action' => function () {
return [
'status' => match ($this->kirby()->request()->method()) {
'PATCH' => 'ok',
default => 'fail'
}
];
}
],
2022-08-31 15:02:43 +02:00
[
'pattern' => 'system/register',
'method' => 'POST',
'action' => function () {
return $this->kirby()->system()->register($this->requestBody('license'), $this->requestBody('email'));
}
],
[
'pattern' => 'system/install',
'method' => 'POST',
'auth' => false,
'action' => function () {
$system = $this->kirby()->system();
$auth = $this->kirby()->auth();
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
// csrf token check
if ($auth->type() === 'session' && $auth->csrf() === false) {
throw new InvalidArgumentException('Invalid CSRF token');
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
if ($system->isOk() === false) {
throw new Exception('The server is not setup correctly');
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
if ($system->isInstallable() === false) {
throw new Exception('The Panel cannot be installed');
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
if ($system->isInstalled() === true) {
throw new Exception('The Panel is already installed');
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
// create the first user
$user = $this->users()->create($this->requestBody());
$token = $user->login($this->requestBody('password'));
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return [
'status' => 'ok',
'token' => $token,
'user' => $this->resolve($user)->view('auth')->toArray()
];
}
]
2022-06-17 17:51:59 +02:00
];