Skip to content

Commit 3f9fd4a

Browse files
committed
feat: implement license checks for user and role creation in RoleResource and UserResource
1 parent 66bc109 commit 3f9fd4a

File tree

3 files changed

+101
-28
lines changed

3 files changed

+101
-28
lines changed

src/Filament/Resources/RoleResource.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use SolutionForest\InspireCms\Helpers\AuthHelper;
2222
use SolutionForest\InspireCms\Helpers\PermissionHelper;
2323
use SolutionForest\InspireCms\InspireCmsConfig;
24+
use SolutionForest\InspireCms\Licensing\LicenseManager;
2425
use Spatie\Permission\Contracts\Role as RoleContract;
2526
use Spatie\Permission\Models\Role;
2627

@@ -361,4 +362,13 @@ private static function getCommonCheckboxListForSection($name, $attribute, $opti
361362
]);
362363
}
363364
// endregion Form field(s)/component(s)
365+
366+
public static function canCreate(): bool
367+
{
368+
if (! app(LicenseManager::class)->canCreateRole()) {
369+
return false;
370+
}
371+
372+
return parent::canCreate();
373+
}
364374
}

src/Filament/Resources/UserResource.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use SolutionForest\InspireCms\Filament\Resources\UserResource\Pages;
1717
use SolutionForest\InspireCms\Helpers\UIHelper;
1818
use SolutionForest\InspireCms\InspireCmsConfig;
19+
use SolutionForest\InspireCms\Licensing\LicenseManager;
1920
use SolutionForest\InspireCms\Models\Contracts\User;
2021

2122
class UserResource extends Resource implements ClusterSectionResource
@@ -109,4 +110,13 @@ public static function getGlobalSearchResultTitle(Model $record): string | Htmla
109110
return UIHelper::generateTextWithDescription($record->name, $record->email);
110111
}
111112
// endregion Global search
113+
114+
public static function canCreate(): bool
115+
{
116+
if (! app(LicenseManager::class)->canCreateUser()) {
117+
return false;
118+
}
119+
120+
return parent::canCreate();
121+
}
112122
}

src/Licensing/LicenseManager.php

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SolutionForest\InspireCms\Licensing;
44

5+
use Illuminate\Database\Eloquent\SoftDeletingScope;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Carbon;
78
use Illuminate\Support\Facades\Cache;
@@ -30,33 +31,6 @@ public function getLicenseKey()
3031
return InspireCmsConfig::get('system.license.key');
3132
}
3233

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-
6034
/**
6135
* @return LicenseVerificationResult
6236
*/
@@ -123,11 +97,90 @@ public function verify()
12397

12498
public function refresh(): void
12599
{
126-
$this->cache()->forget(self::CACHE_KEY_PREFIX . "verification_{$this->getLicenseKey()}_{$this->getCurrentDomain()}");
100+
$this->cache()->forget($this->buildCacheKey());
127101

128102
event(new LicensesRefreshed);
129103
}
130104

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+
131184
public function usingLicenseKeyFile(): bool
132185
{
133186
return File::exists($this->licenseKeyPath());

0 commit comments

Comments
 (0)