Edit shared headers and footers as page types
This commit is contained in:
@@ -27,6 +27,8 @@ final class DesignLibrary
|
||||
'accent-light' => ['强调色浅底', '#e8efff'], 'line' => ['边框', '#dbe3f0'],
|
||||
'button-ink' => ['填充按钮文字', '#ffffff'],
|
||||
];
|
||||
public const PROVIDER_FIELDS = ['mode', 'client_id', 'scope', 'issuer', 'authorization', 'token', 'userinfo',
|
||||
'subject_field', 'name_field', 'avatar_field', 'token_auth'];
|
||||
|
||||
public static function all(?string $kind = null): array
|
||||
{
|
||||
@@ -151,7 +153,7 @@ final class DesignLibrary
|
||||
throw new DesignFieldError('provider_mode', '请选择 OIDC 或 OAuth 2.0。');
|
||||
}
|
||||
$result = ['mode' => $mode];
|
||||
foreach (['client_id', 'scope', 'issuer', 'authorization', 'token', 'userinfo', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) {
|
||||
foreach (array_slice(self::PROVIDER_FIELDS, 1) as $field) {
|
||||
$result[$field] = self::string($data[$field] ?? '', 600, 'provider_' . $field);
|
||||
}
|
||||
if (!in_array($result['token_auth'], ['', 'client_secret_basic', 'client_secret_post'], true)) {
|
||||
@@ -186,10 +188,20 @@ final class DesignLibrary
|
||||
return null;
|
||||
}
|
||||
|
||||
$page = (new Repository())->layoutPage((int) $layout['id']);
|
||||
if ($page !== null && !LayoutPage::hasMember($page['sections'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return hash('sha256', Database::json([$layout['payload']['provider'], $layout['credential_revision']]));
|
||||
}
|
||||
|
||||
public static function save(string $kind, ?int $id, string $name, string $preset, array $data, int $version, bool $default, string $secret = ''): int
|
||||
{
|
||||
return Database::write(static fn (PDO $pdo): int => self::saveIn($pdo, $kind, $id, $name, $preset, $data, $version, $default, $secret));
|
||||
}
|
||||
|
||||
public static function saveIn(PDO $pdo, string $kind, ?int $id, string $name, string $preset, array $data, int $version, bool $default, string $secret = ''): int
|
||||
{
|
||||
$name = self::string($name, 100, 'name');
|
||||
if ($name === '') {
|
||||
@@ -200,44 +212,50 @@ final class DesignLibrary
|
||||
$provider = $clean['provider'] ?? [];
|
||||
$active = $provider !== [] && self::hasAccount($preset, $clean['components']);
|
||||
|
||||
return Database::write(function (PDO $pdo) use ($kind, $id, $name, $preset, $payload, $version, $default, $provider, $active, $secret): int {
|
||||
if ($active && $secret === '') {
|
||||
$saved = $id === null ? null : self::find($id, $kind);
|
||||
$fields = array_flip(['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id']);
|
||||
if (empty($saved['credential_revision'])
|
||||
|| array_intersect_key($provider, $fields) !== array_intersect_key($saved['payload']['provider'], $fields)) {
|
||||
throw new DesignFieldError('provider_secret', '启用登录或变更身份提供商时,请填写客户端密钥。');
|
||||
}
|
||||
if ($active && $secret === '') {
|
||||
$saved = $id === null ? null : self::find($id, $kind);
|
||||
$fields = array_flip(['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id']);
|
||||
if (empty($saved['credential_revision'])
|
||||
|| array_intersect_key($provider, $fields) !== array_intersect_key($saved['payload']['provider'], $fields)) {
|
||||
throw new DesignFieldError('provider_secret', '启用登录或变更身份提供商时,请填写客户端密钥。');
|
||||
}
|
||||
if ($id === null) {
|
||||
$pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)')->execute([$kind, $name, $preset, $payload]);
|
||||
$id = (int) $pdo->lastInsertId();
|
||||
} else {
|
||||
$query = $pdo->prepare('UPDATE design_presets SET name = ?, preset = ?, payload = ?, version = version + 1 WHERE id = ? AND kind = ? AND version = ?');
|
||||
$query->execute([$name, $preset, $payload, $id, $kind, $version]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('配置已更新,请重新打开后再保存。');
|
||||
}
|
||||
}
|
||||
if ($id === null) {
|
||||
$pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)')->execute([$kind, $name, $preset, $payload]);
|
||||
$id = (int) $pdo->lastInsertId();
|
||||
} else {
|
||||
$query = $pdo->prepare('UPDATE design_presets SET name = ?, preset = ?, payload = ?, version = version + 1 WHERE id = ? AND kind = ? AND version = ?');
|
||||
$query->execute([$name, $preset, $payload, $id, $kind, $version]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('配置已更新,请重新打开后再保存。');
|
||||
}
|
||||
if ($kind === 'header') {
|
||||
if ($provider === []) {
|
||||
$pdo->prepare('DELETE FROM layout_oauth_credentials WHERE layout_id = ?')->execute([$id]);
|
||||
} elseif ($active && $secret !== '') {
|
||||
OAuthCredentials::store($pdo, $id, $secret);
|
||||
}
|
||||
}
|
||||
if ($kind !== 'palette') {
|
||||
$site = json_decode($pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
|
||||
if ($default) {
|
||||
$site[$kind . '_id'] = $id;
|
||||
} elseif (($site[$kind . '_id'] ?? null) === $id) {
|
||||
unset($site[$kind . '_id']);
|
||||
}
|
||||
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
||||
}
|
||||
if ($kind === 'header') {
|
||||
if ($provider === []) {
|
||||
$pdo->prepare('DELETE FROM layout_oauth_credentials WHERE layout_id = ?')->execute([$id]);
|
||||
} elseif ($active && $secret !== '') {
|
||||
OAuthCredentials::store($pdo, $id, $secret);
|
||||
}
|
||||
}
|
||||
if ($kind !== 'palette') {
|
||||
self::setDefault($pdo, $kind, $id, $default);
|
||||
}
|
||||
|
||||
return $id;
|
||||
});
|
||||
return $id;
|
||||
}
|
||||
|
||||
public static function setDefault(PDO $pdo, string $kind, int $id, bool $default): void
|
||||
{
|
||||
$site = json_decode($pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
|
||||
$field = $kind . '_id';
|
||||
if ($default) {
|
||||
$site[$field] = $id;
|
||||
} elseif (($site[$field] ?? null) === $id) {
|
||||
unset($site[$field]);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
||||
}
|
||||
|
||||
private static function string(mixed $value, int $limit, string $field): string
|
||||
|
||||
Reference in New Issue
Block a user