|
2 | 2 |
|
3 | 3 | namespace SolutionForest\InspireCms\Licensing; |
4 | 4 |
|
| 5 | +use Illuminate\Database\Eloquent\SoftDeletingScope; |
5 | 6 | use Illuminate\Support\Arr; |
6 | 7 | use Illuminate\Support\Carbon; |
7 | 8 | use Illuminate\Support\Facades\Cache; |
@@ -30,33 +31,6 @@ public function getLicenseKey() |
30 | 31 | return InspireCmsConfig::get('system.license.key'); |
31 | 32 | } |
32 | 33 |
|
33 | | - public function canUpgrade(): bool |
34 | | - { |
35 | | - $licenseKey = $this->getLicenseKey(); |
36 | | - |
37 | | - if (filled($licenseKey)) { |
38 | | - try { |
39 | | - |
40 | | - $this->verify(); |
41 | | - |
42 | | - $cacheKey = $this->buildCacheKey(); |
43 | | - |
44 | | - if (($verificationResult = $this->cache()->get($cacheKey)) && $verificationResult instanceof LicenseVerificationResult) { |
45 | | - $data = $verificationResult->getData(); |
46 | | - |
47 | | - $pvSlug = data_get($data, 'meta.product_variant_slug', ''); |
48 | | - |
49 | | - return is_string($pvSlug) && $pvSlug == 'free'; |
50 | | - } |
51 | | - |
52 | | - } catch (\Throwable $th) { |
53 | | - // |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - return true; |
58 | | - } |
59 | | - |
60 | 34 | /** |
61 | 35 | * @return LicenseVerificationResult |
62 | 36 | */ |
@@ -123,11 +97,90 @@ public function verify() |
123 | 97 |
|
124 | 98 | public function refresh(): void |
125 | 99 | { |
126 | | - $this->cache()->forget(self::CACHE_KEY_PREFIX . "verification_{$this->getLicenseKey()}_{$this->getCurrentDomain()}"); |
| 100 | + $this->cache()->forget($this->buildCacheKey()); |
127 | 101 |
|
128 | 102 | event(new LicensesRefreshed); |
129 | 103 | } |
130 | 104 |
|
| 105 | + public function canUpgrade(): bool |
| 106 | + { |
| 107 | + $tier = $this->getLicenseTier(); |
| 108 | + if (! $tier || ! is_string($tier)) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + return $this->isFree(); |
| 112 | + } |
| 113 | + |
| 114 | + public function getLimitedUserCount(): ?int |
| 115 | + { |
| 116 | + return match ($this->getLicenseTier()) { |
| 117 | + 'pro' => null, // Pro tier has unlimited users |
| 118 | + default => 3, |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + public function getLimitedRoleCount(): ?int |
| 123 | + { |
| 124 | + return match ($this->getLicenseTier()) { |
| 125 | + 'pro' => null, // Pro tier has unlimited roles |
| 126 | + default => 1, |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + public function canCreateUser(): bool |
| 131 | + { |
| 132 | + $limitedUserCount = $this->getLimitedUserCount(); |
| 133 | + if (is_null($limitedUserCount)) { |
| 134 | + return true; // Unlimited users |
| 135 | + } |
| 136 | + $existingUserCount = InspireCmsConfig::getUserModelClass()::query() |
| 137 | + ->withoutGlobalScope(SoftDeletingScope::class) |
| 138 | + ->count(); |
| 139 | + return $existingUserCount < $limitedUserCount; |
| 140 | + } |
| 141 | + |
| 142 | + public function canCreateRole(): bool |
| 143 | + { |
| 144 | + $limitedRoleCount = $this->getLimitedRoleCount(); |
| 145 | + if (is_null($limitedRoleCount)) { |
| 146 | + return true; // Unlimited roles |
| 147 | + } |
| 148 | + $existingRoleCount = InspireCmsConfig::getRoleModelClass()::query() |
| 149 | + ->withoutGlobalScope(SoftDeletingScope::class) |
| 150 | + ->count(); |
| 151 | + return $existingRoleCount < $limitedRoleCount; |
| 152 | + } |
| 153 | + |
| 154 | + public function getLicenseTier(): ?string |
| 155 | + { |
| 156 | + $licenseKey = $this->getLicenseKey(); |
| 157 | + |
| 158 | + if (filled($licenseKey)) { |
| 159 | + try { |
| 160 | + |
| 161 | + $this->verify(); |
| 162 | + |
| 163 | + $cacheKey = $this->buildCacheKey(); |
| 164 | + |
| 165 | + if (($verificationResult = $this->cache()->get($cacheKey)) && $verificationResult instanceof LicenseVerificationResult) { |
| 166 | + $data = $verificationResult->getData(); |
| 167 | + |
| 168 | + return data_get($data, 'meta.product_variant_slug', null); |
| 169 | + } |
| 170 | + |
| 171 | + } catch (\Throwable $th) { |
| 172 | + // |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + private function isFree(): bool |
| 180 | + { |
| 181 | + return $this->getLicenseTier() === 'free'; |
| 182 | + } |
| 183 | + |
131 | 184 | public function usingLicenseKeyFile(): bool |
132 | 185 | { |
133 | 186 | return File::exists($this->licenseKeyPath()); |
|
0 commit comments