import { unref } from 'vue'; import { useMapGetter } from 'dashboard/composables/store'; import { useAccount } from 'dashboard/composables/useAccount'; import { getUserPermissions, hasPermissions, } from 'dashboard/helper/permissionsHelper'; import { PREMIUM_FEATURES } from 'dashboard/featureFlags'; export function usePolicy() { const user = useMapGetter('getCurrentUser'); const isFeatureEnabled = useMapGetter('accounts/isFeatureEnabledonAccount'); const isOnChatwootCloud = useMapGetter('globalConfig/isOnChatwootCloud'); const isACustomBrandedInstance = useMapGetter( 'globalConfig/isACustomBrandedInstance' ); const { accountId } = useAccount(); const getUserPermissionsForAccount = () => { return getUserPermissions(user.value, accountId.value); }; const isFeatureFlagEnabled = featureFlag => { if (!featureFlag) return true; return isFeatureEnabled.value(accountId.value, featureFlag); }; const checkPermissions = requiredPermissions => { if (!requiredPermissions || !requiredPermissions.length) return true; const userPermissions = getUserPermissionsForAccount(); return hasPermissions(requiredPermissions, userPermissions); }; const isPremiumFeature = featureFlag => { if (!featureFlag) return true; return PREMIUM_FEATURES.includes(featureFlag); }; const shouldShow = (featureFlag, permissions) => { const flag = unref(featureFlag); const perms = unref(permissions); // Permissions supersede feature visibility. if (!checkPermissions(perms)) return false; if (isACustomBrandedInstance.value) { // if this is a custom branded instance, we just use the feature flag as a reference return isFeatureFlagEnabled(flag); } // if on cloud, we should if the feature is allowed // or if the feature is a premium one like SLA to show a paywall // the paywall should be managed by the individual component if (isOnChatwootCloud.value) { return isFeatureFlagEnabled(flag) || isPremiumFeature(flag); } // Premium routes remain visible so their page can render its paywall; // other routes still honor the account feature flag on self-hosted installs. return isFeatureFlagEnabled(flag) || isPremiumFeature(flag); }; const shouldShowPaywall = featureFlag => { const flag = unref(featureFlag); if (!flag) return false; if (isACustomBrandedInstance.value) { // custom branded instances never show paywall return false; } if (isPremiumFeature(flag)) { return !isFeatureFlagEnabled(flag); } return false; }; return { checkPermissions, shouldShowPaywall, isFeatureFlagEnabled, shouldShow, }; }