bp/app/Filters/ModuleSubscriptionFilter.php

76 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Filters;
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use App\Services\ModuleSubscriptionService;
/**
* Фильтр проверки подписки на модуль
*
* Проверяет, есть ли у организации активная подписка на указанный модуль.
* Если подписки нет - перенаправляет на dashboard с сообщением об ошибке.
*/
class ModuleSubscriptionFilter implements FilterInterface
{
/**
* Проверка подписки перед выполнением запроса
*/
public function before(RequestInterface $request, $arguments = null)
{
if (!$arguments) {
return;
}
$moduleCode = $arguments[0] ?? null;
if (!$moduleCode) {
return;
}
$session = session();
$orgId = $session->get('active_org_id');
// Если организация не выбрана - пропускаем (другие фильтры обработают)
if (!$orgId) {
return;
}
// Базовый модуль всегда доступен
if ($moduleCode === 'base') {
return;
}
$subscriptionService = new ModuleSubscriptionService();
// Проверяем доступность модуля
if (!$subscriptionService->isModuleAvailable($moduleCode, $orgId)) {
$session->setFlashdata('error', 'Модуль "' . $this->getModuleName($moduleCode) . '" не активен для вашей организации');
return redirect()->to('/');
}
}
/**
* Получение читаемого имени модуля
*/
protected function getModuleName(string $moduleCode): string
{
$names = [
'crm' => 'CRM',
'booking' => 'Бронирования',
'tasks' => 'Задачи',
'proof' => 'Proof',
];
return $names[$moduleCode] ?? $moduleCode;
}
/**
* Обработка ответа после выполнения запроса
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Ничего не делаем после запроса
}
}