Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
EnsureZeusInboundEnabled
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Http\Middleware;
4
5use App\Models\TblAuthUsers;
6use Closure;
7use Illuminate\Http\Request;
8
9/**
10 * Gate for the Zeus → Titan sync endpoints (FIRE-1733).
11 *
12 * Runs AFTER `auth.token`, so the bearer token is already validated and
13 * $request->user() is populated. Three checks:
14 *
15 *   1. `ZEUS_INBOUND_ENABLED=true` — kill switch. Otherwise 503.
16 *   2. Caller is the configured Zeus consumer email — otherwise 403. Matching
17 *      on email (not the numeric tbl_auth_users id) keeps the config portable
18 *      across environments: local, STG and prod all pin the same string.
19 *      Prevents any other external consumer (Fluency, chatbot, landings) from
20 *      reaching a workflow-bypass endpoint that was designed only for Zeus.
21 *   3. Flags the request `is_from_zeus = true` so ZeusBridge can short-circuit
22 *      the outbound forward and never create a Zeus ↔ Titan write loop.
23 *      Company/region gating is done in ZeusSyncService per record.
24 */
25class EnsureZeusInboundEnabled
26{
27    public function handle(Request $request, Closure $next)
28    {
29        if (! (bool) config('zeus.inbound_enabled', false)) {
30            return response([
31                'status' => 'rejected',
32                'error' => 'Zeus inbound sync is disabled.',
33            ], 503);
34        }
35
36        $expectedEmail = trim((string) config('zeus.inbound_consumer_email', ''));
37        if ($expectedEmail === '') {
38            // Misconfiguration: ZEUS_INBOUND_ENABLED is on but no consumer
39            // email is set. Refuse rather than accept a call from any consumer.
40            return response([
41                'status' => 'rejected',
42                'error' => 'Zeus inbound consumer is not configured.',
43            ], 503);
44        }
45
46        $user = $request->user();
47        if (! $user instanceof TblAuthUsers || strcasecmp((string) $user->email, $expectedEmail) !== 0) {
48            return response([
49                'status' => 'rejected',
50                'error' => 'This endpoint is reserved for the Zeus sync consumer.',
51            ], 403);
52        }
53
54        // Signals ZeusBridge to skip its outbound forward: a write that came
55        // FROM Zeus must never be forwarded back to Zeus. Redundant with the
56        // fact that the endpoint does not call Quotations::update_quotation
57        // (so the bridge is never even reached), but kept as defence in depth.
58        $request->attributes->set('is_from_zeus', true);
59
60        return $next($request);
61    }
62}