Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
TelescopeAuthorizedIp
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 7
56
0.00% covered (danger)
0.00%
0 / 1
 isAuthorized
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 clearIpCache
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 clearAllIpCache
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 authorize
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 unauthorize
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 deactivate
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getActiveIps
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Http\Models;
4
5use Illuminate\Support\Facades\Cache;
6use Carbon\Carbon;
7
8class TelescopeAuthorizedIp extends Moloquent
9{
10    protected $fillable = [
11        'ip_address',
12        'description',
13        'added_by',
14        'is_active',
15        'expires_at',
16    ];
17
18    protected $casts = [
19        'is_active' => 'boolean',
20        'expires_at' => 'datetime',
21    ];
22
23    public static function isAuthorized(string $ip): bool
24    {
25        $cacheKey = "telescope_ip_auth_{$ip}";
26
27        return Cache::remember($cacheKey, 300, function () use ($ip) {
28            return self::where('ip_address', $ip)
29                ->where('is_active', true)
30                ->where(function ($query) {
31                    $query->whereNull('expires_at')
32                        ->orWhere('expires_at', '>', now());
33                })
34                ->exists();
35        });
36    }
37
38    public static function clearIpCache(string $ip): void
39    {
40        Cache::forget("telescope_ip_auth_{$ip}");
41    }
42
43    public static function clearAllIpCache(): void
44    {
45        self::pluck('ip_address')->each(function ($ip) {
46            self::clearIpCache($ip);
47        });
48
49        Cache::forget('telescope_all_authorized_ips');
50    }
51
52    public static function authorize(
53        string $ip,
54        string $description = '',
55        string $addedBy = '',
56        Carbon $expiresAt
57    ): self {
58        $authorizedIp = self::updateOrCreate(
59            ['ip_address' => $ip],
60            [
61                'description' => $description,
62                'added_by' => $addedBy,
63                'is_active' => true,
64                'expires_at' => $expiresAt ?? now()->addDays(7),
65            ]
66        );
67
68        self::clearIpCache($ip);
69
70        return $authorizedIp;
71    }
72
73    public static function unauthorize(string $ip): bool
74    {
75        $result = self::where('ip_address', $ip)->delete();
76        self::clearIpCache($ip);
77
78        return $result > 0;
79    }
80
81    public static function deactivate(string $ip): bool
82    {
83        $result = self::where('ip_address', $ip)
84            ->update(['is_active' => false]);
85
86        self::clearIpCache($ip);
87
88        return $result > 0;
89    }
90
91    public static function getActiveIps(): array
92    {
93        return Cache::remember('telescope_all_authorized_ips', 300, function () {
94            return self::where('is_active', true)
95                ->where(function ($query) {
96                    $query->whereNull('expires_at')
97                        ->orWhere('expires_at', '>', now());
98                })
99                ->pluck('ip_address')
100                ->toArray();
101        });
102    }
103}