|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SolutionForest\InspireCms\Licensing; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\SoftDeletingScope; |
| 6 | +use SolutionForest\InspireCms\InspireCmsConfig; |
| 7 | + |
| 8 | +/** |
| 9 | + * Enum representing the actions that can be restricted based on the license tier. |
| 10 | + */ |
| 11 | +enum LicenseTierAction |
| 12 | +{ |
| 13 | + case GlobalSearch; |
| 14 | + case CreateUser; |
| 15 | + case CreateRole; |
| 16 | + case RollbackContentVersion; |
| 17 | + |
| 18 | + private const RESTRICTED_ACTIONS_PRO = [ |
| 19 | + self::GlobalSearch, |
| 20 | + self::CreateUser, |
| 21 | + self::CreateRole, |
| 22 | + self::RollbackContentVersion, |
| 23 | + ]; |
| 24 | + |
| 25 | + private const RESTRICTED_ACTIONS_FREE = [ |
| 26 | + self::CreateUser, |
| 27 | + self::CreateRole, |
| 28 | + ]; |
| 29 | + |
| 30 | + public function isAllowed($tier = null): bool |
| 31 | + { |
| 32 | + $licenseManager = app(LicenseManager::class); |
| 33 | + |
| 34 | + $tier ??= $licenseManager->getLicenseTier(); |
| 35 | + |
| 36 | + $preCheck = match ($tier) { |
| 37 | + 'pro' => in_array($this, self::RESTRICTED_ACTIONS_PRO), |
| 38 | + 'free' => in_array($this, self::RESTRICTED_ACTIONS_FREE), |
| 39 | + default => null, |
| 40 | + }; |
| 41 | + |
| 42 | + if ($preCheck === false) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + // Validate each action based on the tier |
| 47 | + switch ($this) { |
| 48 | + |
| 49 | + case self::CreateUser: |
| 50 | + $limitedUserCount = $licenseManager->getLimitedUserCount(); |
| 51 | + if (is_null($limitedUserCount)) { |
| 52 | + return true; // Unlimited users |
| 53 | + } |
| 54 | + $existingUserCount = InspireCmsConfig::getUserModelClass()::query() |
| 55 | + ->withoutGlobalScope(SoftDeletingScope::class) |
| 56 | + ->count(); |
| 57 | + return $existingUserCount < $limitedUserCount; |
| 58 | + |
| 59 | + case self::CreateRole: |
| 60 | + $limitedRoleCount = $licenseManager->getLimitedRoleCount(); |
| 61 | + if (is_null($limitedRoleCount)) { |
| 62 | + return true; // Unlimited roles |
| 63 | + } |
| 64 | + $existingRoleCount = InspireCmsConfig::getRoleModelClass()::query() |
| 65 | + ->withoutGlobalScope(SoftDeletingScope::class) |
| 66 | + ->count(); |
| 67 | + return $existingRoleCount < $limitedRoleCount; |
| 68 | + |
| 69 | + default: |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + return true; |
| 74 | + } |
| 75 | +} |
0 commit comments