Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 80 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| TelescopeIpController | |
0.00% |
0 / 80 |
|
0.00% |
0 / 8 |
110 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| store | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
6 | |||
| authorizeCurrentIp | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
6 | |||
| destroy | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| deactivate | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| activate | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| clearCache | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| checkCurrentIp | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v2; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Models\TelescopeAuthorizedIp; |
| 7 | use Illuminate\Http\Request; |
| 8 | |
| 9 | class TelescopeIpController extends Controller |
| 10 | { |
| 11 | /** |
| 12 | * Listar IPs autorizados |
| 13 | */ |
| 14 | public function index() |
| 15 | { |
| 16 | $ips = TelescopeAuthorizedIp::orderBy('created_at', 'desc')->get(); |
| 17 | |
| 18 | return response()->json([ |
| 19 | 'success' => true, |
| 20 | 'data' => $ips |
| 21 | ]); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Adicionar IP autorizado |
| 26 | */ |
| 27 | public function store(Request $request) |
| 28 | { |
| 29 | $request->validate([ |
| 30 | 'ip_address' => 'required|ip', |
| 31 | 'description' => 'nullable|string|max:255', |
| 32 | 'expires_in_hours' => 'nullable|integer|min:1|max:8760', // Max 1 ano |
| 33 | ]); |
| 34 | |
| 35 | $expiresAt = null; |
| 36 | if ($request->expires_in_hours) { |
| 37 | $expiresAt = now()->addHours($request->expires_in_hours); |
| 38 | } |
| 39 | |
| 40 | $authorizedIp = TelescopeAuthorizedIp::authorize( |
| 41 | $request->ip_address, |
| 42 | $request->description, |
| 43 | $request->user()->email ?? 'system', |
| 44 | $expiresAt |
| 45 | ); |
| 46 | |
| 47 | return response()->json([ |
| 48 | 'success' => true, |
| 49 | 'message' => 'IP autorizado com sucesso', |
| 50 | 'data' => $authorizedIp |
| 51 | ]); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Autorizar IP atual |
| 56 | */ |
| 57 | public function authorizeCurrentIp(Request $request) |
| 58 | { |
| 59 | $request->validate([ |
| 60 | 'description' => 'nullable|string|max:255', |
| 61 | 'expires_in_hours' => 'nullable|integer|min:1|max:8760', |
| 62 | ]); |
| 63 | |
| 64 | $currentIp = $request->ip(); |
| 65 | $expiresAt = null; |
| 66 | |
| 67 | if ($request->expires_in_hours) { |
| 68 | $expiresAt = now()->addHours($request->expires_in_hours); |
| 69 | } |
| 70 | |
| 71 | $authorizedIp = TelescopeAuthorizedIp::authorize( |
| 72 | $currentIp, |
| 73 | $request->description ?? "Auto-authorized from {$currentIp}", |
| 74 | $request->user()->email ?? 'system', |
| 75 | $expiresAt |
| 76 | ); |
| 77 | |
| 78 | return response()->json([ |
| 79 | 'success' => true, |
| 80 | 'message' => "Seu IP atual ({$currentIp}) foi autorizado", |
| 81 | 'data' => $authorizedIp |
| 82 | ]); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Remover IP autorizado |
| 87 | */ |
| 88 | public function destroy(Request $request, $id) |
| 89 | { |
| 90 | $authorizedIp = TelescopeAuthorizedIp::findOrFail($id); |
| 91 | $ip = $authorizedIp->ip_address; |
| 92 | |
| 93 | $authorizedIp->delete(); |
| 94 | TelescopeAuthorizedIp::clearIpCache($ip); |
| 95 | |
| 96 | return response()->json([ |
| 97 | 'success' => true, |
| 98 | 'message' => 'IP removido com sucesso' |
| 99 | ]); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Desativar IP |
| 104 | */ |
| 105 | public function deactivate(Request $request, $id) |
| 106 | { |
| 107 | $authorizedIp = TelescopeAuthorizedIp::findOrFail($id); |
| 108 | $authorizedIp->update(['is_active' => false]); |
| 109 | |
| 110 | TelescopeAuthorizedIp::clearIpCache($authorizedIp->ip_address); |
| 111 | |
| 112 | return response()->json([ |
| 113 | 'success' => true, |
| 114 | 'message' => 'IP desativado com sucesso' |
| 115 | ]); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Reativar IP |
| 120 | */ |
| 121 | public function activate(Request $request, $id) |
| 122 | { |
| 123 | $authorizedIp = TelescopeAuthorizedIp::findOrFail($id); |
| 124 | $authorizedIp->update(['is_active' => true]); |
| 125 | |
| 126 | TelescopeAuthorizedIp::clearIpCache($authorizedIp->ip_address); |
| 127 | |
| 128 | return response()->json([ |
| 129 | 'success' => true, |
| 130 | 'message' => 'IP reativado com sucesso' |
| 131 | ]); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Limpar cache de IPs |
| 136 | */ |
| 137 | public function clearCache() |
| 138 | { |
| 139 | TelescopeAuthorizedIp::clearAllIpCache(); |
| 140 | |
| 141 | return response()->json([ |
| 142 | 'success' => true, |
| 143 | 'message' => 'Cache de IPs limpo com sucesso' |
| 144 | ]); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Verificar status do IP atual |
| 149 | */ |
| 150 | public function checkCurrentIp(Request $request) |
| 151 | { |
| 152 | $currentIp = $request->ip(); |
| 153 | $isAuthorized = TelescopeAuthorizedIp::isAuthorized($currentIp); |
| 154 | |
| 155 | return response()->json([ |
| 156 | 'success' => true, |
| 157 | 'data' => [ |
| 158 | 'ip' => $currentIp, |
| 159 | 'is_authorized' => $isAuthorized, |
| 160 | 'telescope_url' => url('/telescope') |
| 161 | ] |
| 162 | ]); |
| 163 | } |
| 164 | } |