Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 62 additions & 19 deletions Backend/Controllers/RedisSettingsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,61 @@
namespace Okay\Modules\Sviat\Redis\Backend\Controllers;

use Okay\Admin\Controllers\IndexAdmin;
use Okay\Modules\Sviat\Redis\Security\RequestOrigin;
use Okay\Modules\Sviat\Redis\Services\RedisCacheService;
use Okay\Modules\Sviat\Redis\Services\RedisConnectionForm;

class RedisSettingsAdmin extends IndexAdmin
{
/** Інші POST сторінки (вихід з адмінки) приходять сюди порожніми й не мають зачепити налаштування. */
private const array ACTIONS = ['save', 'test', 'flush_helpers'];

public function fetch(RedisCacheService $redisCache)
{
$testResult = null;
$error = null;
$flushError = null;
$formErrors = [];
$passwordDropped = false;

$locked = $redisCache->lockedConnectionFields();

if ($this->request->method('post') && in_array($this->request->post('action'), self::ACTIONS, true)) {
if (!$this->isFromThisSite()) {
$this->response->setStatusCode(403);
$this->response->setContent(json_encode(['message' => 'Forbidden']), RESPONSE_JSON);

return;
}

if (!isset($locked['enabled'])) {
$this->settings->set('sviat__redis__enabled', (int) $this->request->post('enabled'));
}

$connection = RedisConnectionForm::apply(
[
'host' => $this->request->post('host'),
'port' => $this->request->post('port'),
'db' => $this->request->post('db'),
'username' => $this->request->post('username'),
'password' => $this->request->post('password'),
'clear_password' => $this->request->post('clear_password'),
'prefix' => $this->request->post('prefix'),
],
[
'host' => (string) ($this->settings->get('sviat__redis__host') ?: '127.0.0.1'),
'port' => (string) ($this->settings->get('sviat__redis__port') ?: 6379),
'username' => (string) ($this->settings->get('sviat__redis__username') ?? ''),
'password' => (string) ($this->settings->get('sviat__redis__password') ?? ''),
],
$locked
);
foreach ($connection['values'] as $field => $value) {
$this->settings->set('sviat__redis__' . $field, $value);
}
$formErrors = $connection['errors'];
$passwordDropped = $connection['passwordDropped'];

if ($this->request->method('post')) {
$this->settings->set('sviat__redis__enabled', (int)$this->request->post('enabled'));
$this->settings->set('sviat__redis__host', $this->request->post('host'));
$this->settings->set('sviat__redis__port', (int)$this->request->post('port'));
$this->settings->set('sviat__redis__db', (int)$this->request->post('db'));
$this->settings->set('sviat__redis__username', trim((string) $this->request->post('username')));
// Save password as-is; empty value means no auth.
$this->settings->set('sviat__redis__password', (string) ($this->request->post('password') ?? ''));
$this->settings->set('sviat__redis__prefix', $this->request->post('prefix'));
$this->settings->set('sviat__redis__default_ttl', (int)$this->request->post('default_ttl'));

$this->settings->set('sviat__redis__ttl__products_get_list', (int)$this->request->post('ttl_products_get_list'));
Expand All @@ -31,7 +67,6 @@ public function fetch(RedisCacheService $redisCache)
$this->settings->set('sviat__redis__ttl__product_attach_variants', (int)$this->request->post('ttl_product_attach_variants'));
$this->settings->set('sviat__redis__ttl__product_attach_images', (int)$this->request->post('ttl_product_attach_images'));
$this->settings->set('sviat__redis__ttl__product_attach_features', (int)$this->request->post('ttl_product_attach_features'));
$this->settings->set('sviat__redis__ttl__money_currencies_list', (int)$this->request->post('ttl_money_currencies_list'));
$this->settings->set('sviat__redis__ttl__brands_get_list', (int)$this->request->post('ttl_brands_get_list'));
$this->settings->set('sviat__redis__ttl__filter_get_brands', (int)$this->request->post('ttl_filter_get_brands'));
$this->settings->set('sviat__redis__ttl__authors_get_list', (int)$this->request->post('ttl_authors_get_list'));
Expand All @@ -58,13 +93,17 @@ public function fetch(RedisCacheService $redisCache)

$stats = $redisCache->getStats();

$this->design->assign('redis_enabled', (int)$this->settings->get('sviat__redis__enabled'));
$this->design->assign('redis_host', $this->settings->get('sviat__redis__host') ?: '127.0.0.1');
$this->design->assign('redis_port', (int)($this->settings->get('sviat__redis__port') ?: 6379));
$this->design->assign('redis_db', (int)($this->settings->get('sviat__redis__db') ?: 0));
$this->design->assign('redis_username', (string) ($this->settings->get('sviat__redis__username') ?? ''));
$this->design->assign('redis_password', $this->settings->get('sviat__redis__password'));
$this->design->assign('redis_prefix', $this->settings->get('sviat__redis__prefix') ?: 'okay:');
$view = $redisCache->connectionView();
$this->design->assign('redis_locked', $locked);
$this->design->assign('redis_form_errors', $formErrors);
$this->design->assign('redis_password_dropped', $passwordDropped);
$this->design->assign('redis_enabled', (int) $view['enabled']);
$this->design->assign('redis_host', $view['host']);
$this->design->assign('redis_port', $view['port']);
$this->design->assign('redis_db', $view['db']);
$this->design->assign('redis_username', $view['username']);
$this->design->assign('redis_password_is_set', (string) ($this->settings->get('sviat__redis__password') ?? '') !== '');
$this->design->assign('redis_prefix', $view['prefix']);
$this->design->assign('redis_default_ttl', (int)($this->settings->get('sviat__redis__default_ttl') ?: 600));

$this->design->assign('ttl_products_get_list', (int)($this->settings->get('sviat__redis__ttl__products_get_list') ?: 300));
Expand All @@ -74,7 +113,6 @@ public function fetch(RedisCacheService $redisCache)
$this->design->assign('ttl_product_attach_variants', (int)($this->settings->get('sviat__redis__ttl__product_attach_variants') ?: 300));
$this->design->assign('ttl_product_attach_images', (int)($this->settings->get('sviat__redis__ttl__product_attach_images') ?: 3600));
$this->design->assign('ttl_product_attach_features', (int)($this->settings->get('sviat__redis__ttl__product_attach_features') ?: 3600));
$this->design->assign('ttl_money_currencies_list', (int)($this->settings->get('sviat__redis__ttl__money_currencies_list') ?: 3600));
$this->design->assign('ttl_brands_get_list', (int)($this->settings->get('sviat__redis__ttl__brands_get_list') ?: 600));
$this->design->assign('ttl_filter_get_brands', (int)($this->settings->get('sviat__redis__ttl__filter_get_brands') ?: 600));
$this->design->assign('ttl_authors_get_list', (int)($this->settings->get('sviat__redis__ttl__authors_get_list') ?: 600));
Expand All @@ -88,5 +126,10 @@ public function fetch(RedisCacheService $redisCache)

$this->response->setContent($this->design->fetch('redis_settings.tpl'));
}
}

/** На стоку CSRF-перевірка ядра стоїть після контролера, тому модуль перевіряє сам. */
protected function isFromThisSite(): bool
{
return RequestOrigin::isFromThisSite();
}
}
52 changes: 38 additions & 14 deletions Backend/design/html/redis_settings.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</div>

{* Алерти: збережено / результат тесту підключення *}
{if $message_success || $test_result === true || $test_result === false || $flush_error !== null}
{if $message_success || $test_result === true || $test_result === false || $flush_error !== null || $redis_form_errors || $redis_password_dropped}
<div class="row">
<div class="col-lg-12">
{if $message_success}
Expand All @@ -19,6 +19,20 @@
</div>
</div>
{/if}
{if $redis_form_errors}
<div class="alert alert--warning">
<div class="alert__content">
<div class="alert__title">{$btr->sviat_redis_form_invalid|escape}</div>
</div>
</div>
{/if}
{if $redis_password_dropped}
<div class="alert alert--warning">
<div class="alert__content">
<div class="alert__title">{$btr->sviat_redis_password_dropped|escape}</div>
</div>
</div>
{/if}
{if $test_result === true}
<div class="alert alert--success">
<div class="alert__content">
Expand Down Expand Up @@ -53,13 +67,21 @@
<div class="boxed">
<div class="heading_box">{$btr->sviat_redis_connection_box|escape}</div>

{if $redis_locked}
<div class="alert alert--info">
<div class="alert__content">
<div class="alert__text">{$btr->sviat_redis_locked_hint|escape}</div>
</div>
</div>
{/if}

<div class="form-group">
<div class="activity_of_switch activity_of_switch--left">
<div class="activity_of_switch_item">
<div class="okay_switch clearfix">
<label class="switch_label">{$btr->sviat_redis_enable_cache|escape}</label>
<label class="switch switch-default">
<input class="switch-input" name="enabled" value="1" type="checkbox" id="sviat_redis_enabled" {if $redis_enabled}checked{/if}>
<input class="switch-input" name="enabled" value="1" type="checkbox"{if $redis_locked.enabled} disabled{/if} id="sviat_redis_enabled" {if $redis_enabled}checked{/if}>
<span class="switch-label"></span>
<span class="switch-handle"></span>
</label>
Expand All @@ -70,32 +92,40 @@

<div class="form-group">
<label class="heading_label">{$btr->sviat_redis_host|escape}</label>
<input type="text" class="form-control" name="host" value="{$redis_host|escape}">
<input type="text" class="form-control" name="host" value="{$redis_host|escape}"{if $redis_locked.host} readonly{/if}>
</div>

<div class="form-group">
<label class="heading_label">{$btr->sviat_redis_port|escape}</label>
<input type="number" class="form-control" name="port" value="{$redis_port|escape}">
<input type="number" class="form-control" name="port" value="{$redis_port|escape}"{if $redis_locked.port} readonly{/if}>
</div>

<div class="form-group">
<label class="heading_label">{$btr->sviat_redis_db|escape}</label>
<input type="number" class="form-control" name="db" value="{$redis_db|escape}">
<input type="number" class="form-control" name="db" value="{$redis_db|escape}"{if $redis_locked.db} readonly{/if}>
</div>

<div class="form-group">
<label class="heading_label">{$btr->sviat_redis_username|escape}</label>
<input type="text" class="form-control" name="username" value="{$redis_username|escape}" placeholder="{$btr->sviat_redis_username_placeholder|escape}" autocomplete="off">
<input type="text" class="form-control" name="username" value="{$redis_username|escape}"{if $redis_locked.username} readonly{/if} placeholder="{$btr->sviat_redis_username_placeholder|escape}" autocomplete="off">
</div>

{if !$redis_locked.password}
<div class="form-group">
<label class="heading_label">{$btr->sviat_redis_password|escape}</label>
<input type="password" class="form-control" name="password" value="{$redis_password|escape}" placeholder="{$btr->sviat_redis_password_placeholder|escape}" autocomplete="new-password">
<input type="password" class="form-control" name="password" value="" placeholder="{if $redis_password_is_set}{$btr->sviat_redis_password_placeholder|escape}{else}{$btr->sviat_redis_password_placeholder_empty|escape}{/if}" autocomplete="new-password">
{if $redis_password_is_set}
<label class="form-check-label" style="display:block; margin-top:8px;">
<input type="checkbox" name="clear_password" value="1">
{$btr->sviat_redis_clear_password|escape}
</label>
{/if}
</div>
{/if}

<div class="form-group">
<label class="heading_label">{$btr->sviat_redis_key_prefix|escape}</label>
<input type="text" class="form-control" name="prefix" value="{$redis_prefix|escape}">
<input type="text" class="form-control" name="prefix" value="{$redis_prefix|escape}"{if $redis_locked.prefix} readonly{/if}>
</div>

<div class="form-group">
Expand Down Expand Up @@ -191,12 +221,6 @@
<input type="number" class="form-control" name="ttl_categories_catalog_features" value="{$ttl_categories_catalog_features|escape}">
</div>
</div>
<div class="col-lg-3 col-md-4 col-sm-6">
<div class="form-group">
<label class="heading_label"><small>MoneyHelper<span class="text_grey">:currencies</span></small></label>
<input type="number" class="form-control" name="ttl_money_currencies_list" value="{$ttl_money_currencies_list|escape}">
</div>
</div>
<div class="col-lg-3 col-md-4 col-sm-6">
<div class="form-group">
<label class="heading_label"><small>BrandsHelper<span class="text_grey">:getList</span></small></label>
Expand Down
9 changes: 7 additions & 2 deletions Backend/lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
$lang['sviat_redis_username'] = 'Username';
$lang['sviat_redis_username_placeholder'] = 'ACL (Redis 6+), empty = password only';
$lang['sviat_redis_password'] = 'Password';
$lang['sviat_redis_password_placeholder'] = 'Empty = no authentication';
$lang['sviat_redis_clear_password'] = 'Remove stored password';
$lang['sviat_redis_locked_hint'] = 'Part of the connection is set outside the admin panel (env or config.local.php), those fields cannot be changed here.';
$lang['sviat_redis_form_invalid'] = 'Some fields are invalid and were not saved.';
$lang['sviat_redis_password_dropped'] = 'The host changed, so the stored password was removed. Enter the password for the new server.';
$lang['sviat_redis_password_placeholder'] = 'Leave blank to keep the current password';
$lang['sviat_redis_password_placeholder_empty'] = 'Empty = no authentication';
$lang['sviat_redis_key_prefix'] = 'Key prefix';
$lang['sviat_redis_default_ttl'] = 'Default TTL (sec)';
$lang['sviat_redis_test_connection'] = 'Test connection';
$lang['sviat_redis_cache_status'] = 'Cache status';
$lang['sviat_redis_db_keys_count'] = 'Number of keys in DB';
$lang['sviat_redis_used_memory'] = 'Used memory';
$lang['sviat_redis_flush_current_db'] = 'Clear all Redis cache (current DB)';
$lang['sviat_redis_flush_current_db'] = 'Clear this module cache';
$lang['sviat_redis_unavailable'] = 'Redis is unavailable';
$lang['sviat_redis_helpers_ttl'] = 'TTL for helpers';
9 changes: 7 additions & 2 deletions Backend/lang/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
$lang['sviat_redis_username'] = 'Username';
$lang['sviat_redis_username_placeholder'] = 'ACL (Redis 6+), пусто = только пароль';
$lang['sviat_redis_password'] = 'Password';
$lang['sviat_redis_password_placeholder'] = 'Пусто = без авторизации';
$lang['sviat_redis_clear_password'] = 'Удалить сохранённый пароль';
$lang['sviat_redis_locked_hint'] = 'Часть подключения задана вне админки (env или config.local.php), эти поля здесь не меняются.';
$lang['sviat_redis_form_invalid'] = 'Некоторые поля заполнены неверно и не сохранены.';
$lang['sviat_redis_password_dropped'] = 'Хост изменился, поэтому сохранённый пароль удалён. Введите пароль нового сервера.';
$lang['sviat_redis_password_placeholder'] = 'Оставьте пустым, чтобы сохранить текущий';
$lang['sviat_redis_password_placeholder_empty'] = 'Пусто = без авторизации';
$lang['sviat_redis_key_prefix'] = 'Префикс ключей';
$lang['sviat_redis_default_ttl'] = 'TTL по умолчанию (сек)';
$lang['sviat_redis_test_connection'] = 'Проверить подключение';
$lang['sviat_redis_cache_status'] = 'Состояние кеша';
$lang['sviat_redis_db_keys_count'] = 'Количество ключей в БД';
$lang['sviat_redis_used_memory'] = 'Используемая память';
$lang['sviat_redis_flush_current_db'] = 'Очистить весь кеш Redis (текущая DB)';
$lang['sviat_redis_flush_current_db'] = 'Очистить кеш этого модуля';
$lang['sviat_redis_unavailable'] = 'Redis недоступен';
$lang['sviat_redis_helpers_ttl'] = 'TTL для хелперов';
Loading
Loading