php-tenant/Features/UnlimitedWorkspaces.php
Snider d0ad2737cb refactor: rename namespace from Core\Mod\Tenant to Core\Tenant
Simplifies the namespace hierarchy by removing the intermediate Mod
segment. Updates all 118 files including models, services, controllers,
middleware, tests, and composer.json autoload configuration.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 16:30:46 +00:00

75 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Tenant\Features;
use Core\Tenant\Enums\UserTier;
use Core\Tenant\Models\User;
use Core\Tenant\Models\Workspace;
use Core\Tenant\Services\EntitlementService;
class UnlimitedWorkspaces
{
public function __construct(
protected EntitlementService $entitlements
) {}
/**
* Resolve the feature's initial value.
* Unlimited workspaces if:
* - User's workspace has 'tier.hades' feature, OR
* - User has Hades tier on their profile (legacy fallback)
*/
public function resolve(mixed $scope): bool
{
// Check workspace entitlements first
if ($scope instanceof Workspace) {
return $this->checkWorkspaceEntitlement($scope);
}
if ($scope instanceof User) {
// Check user's owner workspace
$workspace = $scope->ownedWorkspaces()->first();
if ($workspace && $this->checkWorkspaceEntitlement($workspace)) {
return true;
}
// Legacy fallback: check user tier
return $this->checkUserTier($scope);
}
return false;
}
/**
* Check if workspace has Hades tier entitlement (unlimited workspaces).
*/
protected function checkWorkspaceEntitlement(Workspace $workspace): bool
{
$result = $this->entitlements->can($workspace, 'tier.hades');
return $result->isAllowed();
}
/**
* Legacy fallback: check user's tier attribute.
*/
protected function checkUserTier(mixed $scope): bool
{
if (method_exists($scope, 'getTier')) {
return $scope->getTier() === UserTier::HADES;
}
if (isset($scope->tier)) {
$tier = $scope->tier;
if (is_string($tier)) {
return $tier === UserTier::HADES->value;
}
return $tier === UserTier::HADES;
}
return false;
}
}