forked from laysense/laysite
Prepare LaySite 1.4.7 Docker administrator bootstrap
This commit is contained in:
@@ -4,8 +4,45 @@ declare(strict_types=1);
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Config\Environment;
|
||||
use App\Content\Database;
|
||||
use App\Content\Passwords;
|
||||
|
||||
umask(0027);
|
||||
Database::initialize();
|
||||
fwrite(STDOUT, "SQLite 已初始化。现有内容会保留。\n");
|
||||
|
||||
$email = strtolower(trim((string) Environment::get('LAYSITE_ADMIN_EMAIL')));
|
||||
$password = (string) Environment::get('LAYSITE_ADMIN_PASSWORD');
|
||||
if ($email === '' && $password === '') {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
try {
|
||||
$created = Database::write(static function (PDO $pdo) use ($email, $password): bool {
|
||||
if ($pdo->query('SELECT 1 FROM users LIMIT 1')->fetchColumn() !== false) {
|
||||
return false;
|
||||
}
|
||||
if ($email === '' || $password === '') {
|
||||
throw new InvalidArgumentException('请同时设置 LAYSITE_ADMIN_EMAIL 和 LAYSITE_ADMIN_PASSWORD。');
|
||||
}
|
||||
if (strlen($email) > 254 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new InvalidArgumentException('LAYSITE_ADMIN_EMAIL 必须是有效的邮箱地址。');
|
||||
}
|
||||
if (!Passwords::isValidNew($password)) {
|
||||
throw new InvalidArgumentException('LAYSITE_ADMIN_PASSWORD 至少 8 个字符,最多 72 字节,并须使用有效的 UTF-8。');
|
||||
}
|
||||
|
||||
$query = $pdo->prepare('INSERT INTO users (email, password_hash) VALUES (?, ?)');
|
||||
$query->execute([$email, password_hash($password, PASSWORD_DEFAULT)]);
|
||||
|
||||
return true;
|
||||
});
|
||||
} catch (InvalidArgumentException $error) {
|
||||
fwrite(STDERR, $error->getMessage() . PHP_EOL);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ($created) {
|
||||
fwrite(STDOUT, "已创建初始管理员账号。\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user