hasConsoleBillingAccess function
bool
hasConsoleBillingAccess({
- required bool isDisableCostWarnings(),
- required bool isNeomageAiSubscriber(),
- required AuthTokenSource getAuthTokenSource(),
- required bool hasApiKey(),
- required BillingConfig getGlobalConfig(),
Check if user has Console billing access.
Checks authentication state, subscriber status, and org/workspace roles to determine if the user can see billing information.
Implementation
bool hasConsoleBillingAccess({
required bool Function() isDisableCostWarnings,
required bool Function() isNeomageAiSubscriber,
required AuthTokenSource Function() getAuthTokenSource,
required bool Function() hasApiKey,
required BillingConfig Function() getGlobalConfig,
}) {
// Check if cost reporting is disabled via environment variable.
if (isDisableCostWarnings()) {
return false;
}
final isSubscriber = isNeomageAiSubscriber();
// This might be wrong if user is signed into Max but also using an API key,
// but we already show a warning on launch in that case.
if (isSubscriber) return false;
// Check if user has any form of authentication.
final authSource = getAuthTokenSource();
final apiKeyPresent = hasApiKey();
// If user has no authentication at all (logged out), don't show costs.
if (!authSource.hasToken && !apiKeyPresent) {
return false;
}
final config = getGlobalConfig();
final orgRole = config.oauthAccount?.organizationRole;
final workspaceRole = config.oauthAccount?.workspaceRole;
if (orgRole == null || workspaceRole == null) {
return false; // hide cost for grandfathered users who have not re-authed
}
// Users have billing access if they are admins or billing roles at either
// workspace or organization level.
return ['admin', 'billing'].contains(orgRole) ||
['workspace_admin', 'workspace_billing'].contains(workspaceRole);
}