Repository navigation
Global Gatekeeper
🇧🇷 Ler em Português | 🇺🇸 Read in English
O pior "código sujo" (Code Smell) que você pode encontrar na camada Controller de um sistema é a repetição manual de validações de acesso, como:
if ($_SESSION['user_role'] !== 'admin') { die('Acesso Negado'); }Além de violar o princípio de Responsabilidade Única (SRP) (o controlador não deveria ser o segurança da porta), isso cria falhas críticas de escalabilidade. Se a sistema criar um novo cargo, o programador precisará alterar dezenas de controladores manualmente.
Para resolver isso, o Domain-System move a responsabilidade de autorização (ACL - Access Control List) para a engrenagem mais alta possível: O Roteador (Router).
Em vez de verificar sessões dentro dos controladores de negócio (Finanças, usuários, Prontuários), a autorização passa a ser puramente Declarativa.
Quando um módulo registra suas rotas no arquivo central Plugin.php, ele declara uma matriz (array) de Papéis Permitidos (Roles) diretamente na rota:
$router->addRoute('GET', '/admin/finance', [FinanceController::class, 'index'], 'finance', ['admin']);
$router->addRoute('GET', '/admin/triage', [TriageController::class, 'index'], 'triage', ['admin', 'receptionist', 'doctor']);Antes do Roteador instanciar o Controlador na memória e processar injeções de dependência, ele age como um "Porteiro":
- Lê a matriz de exigências da Rota.
- Compara com a sessão ativa no Kernel.
- Se o papel não bater, o Roteador exibe um Escudo de Acesso Negado (Status 403 Forbidden).
O controlador sequer chega a ser acionado. O código flui maravilhosamente limpo e focado 100% no negócio, deixando toda a lógica de segurança para o núcleo do sistema tratar de forma invisível.
The worst "Code Smell" you can find in the Controller layer of a system is the manual repetition of access validations, such as:
if ($_SESSION['user_role'] !== 'admin') { die('Access Denied'); }Besides violating the Single Responsibility Principle (SRP) (the controller shouldn't be the door bouncer), this creates critical scalability flaws. If the clinic creates a new job role, the programmer will have to alter dozens of controllers manually.
To solve this, the Domain-System moves the authorization responsibility (ACL - Access Control List) to the highest possible gear: The Router.
Instead of verifying sessions inside business controllers (Finances, Patients, Records), authorization becomes purely Declarative.
When a module registers its routes in the central Plugin.php file, it declares an array of Allowed Roles directly on the route:
$router->addRoute('GET', '/admin/finance', [FinanceController::class, 'index'], 'finance', ['admin']);
$router->addRoute('GET', '/admin/triage', [TriageController::class, 'index'], 'triage', ['admin', 'receptionist', 'doctor']);Before the Router instantiates the Controller in memory and processes dependency injections, it acts as a "Gatekeeper":
- Reads the route's requirement array.
- Compares it with the active session in the Kernel.
- If the role doesn't match, the Router displays a Denied Access Shield (Status 403 Forbidden).
The controller never even gets triggered. The code flows beautifully clean and focused 100% on business logic, leaving all security handling to be processed invisibly by the system's core.