Allow call site to ignore feature flag allowlists

This commit is contained in:
Vincent 2023-10-11 14:39:28 +02:00 коммит произвёл Vincent
Родитель e0649c6660
Коммит 20c37680c2
5 изменённых файлов: 27 добавлений и 45 удалений

Просмотреть файл

@ -11,7 +11,7 @@ import { getCountryCode } from "../functions/server/getCountryCode";
import { headers } from "next/headers";
import AppConstants from "../../appConstants";
import { getNonce } from "./functions/server/getNonce";
import { getFlagsEnabledForEveryone } from "../../db/tables/featureFlags";
import { getEnabledFeatureFlags } from "../../db/tables/featureFlags";
export default async function MigrationLayout({
children,
@ -21,7 +21,7 @@ export default async function MigrationLayout({
const headersList = headers();
const l10nBundles = getL10nBundles();
const countryCode = getCountryCode(headersList);
const enabledFlags = await getFlagsEnabledForEveryone();
const enabledFlags = await getEnabledFeatureFlags({ ignoreAllowlist: true });
const waitlistLink = AppConstants.FALSE_DOOR_TEST_LINK_PHASE_ONE;
const acceptLanguage = headersList.get("Accept-Language");

Просмотреть файл

@ -22,7 +22,7 @@ import {
} from "../../../../../functions/server/onerep";
import getPremiumSubscriptionUrl from "../../../../../functions/server/getPremiumSubscriptionUrl";
import { refreshStoredScanResults } from "../../../../../functions/server/refreshStoredScanResults";
import { getFlagsEnabledForEmail } from "../../../../../../db/tables/featureFlags";
import { getEnabledFeatureFlags } from "../../../../../../db/tables/featureFlags";
export default async function DashboardPage() {
const session = await getServerSession(authOptions);
if (!session?.user?.subscriber?.id) {
@ -51,14 +51,18 @@ export default async function DashboardPage() {
session.user,
countryCode
);
const enabledFlags = await getFlagsEnabledForEmail(session.user.email);
const enabledFlags = await getEnabledFeatureFlags({
email: session.user.email,
});
const userIsEligibleForPremium = isEligibleForPremium(
session.user,
countryCode,
enabledFlags
);
const enabledFeatureFlags = await getFlagsEnabledForEmail(session.user.email);
const enabledFeatureFlags = await getEnabledFeatureFlags({
email: session.user.email,
});
const monthlySubscriptionUrl = getPremiumSubscriptionUrl({ type: "monthly" });
const yearlySubscriptionUrl = getPremiumSubscriptionUrl({ type: "yearly" });

Просмотреть файл

@ -6,7 +6,7 @@ import { NextRequest, NextResponse } from "next/server";
import { bearerToken } from "../../../utils/auth";
import { PubSub } from "@google-cloud/pubsub";
import { getFlagsEnabledForEveryone } from "../../../../../db/tables/featureFlags";
import { getEnabledFeatureFlags } from "../../../../../db/tables/featureFlags";
const projectId = process.env.GCP_PUBSUB_PROJECT_ID;
const topicName = process.env.GCP_PUBSUB_TOPIC_NAME;
@ -21,7 +21,7 @@ const subscriptionName = process.env.GCP_PUBSUB_SUBSCRIPTION_NAME;
export async function POST(req: NextRequest) {
let pubsub;
let json;
const enabledFlags = await getFlagsEnabledForEveryone();
const enabledFlags = await getEnabledFeatureFlags({ ignoreAllowlist: true });
try {
if (!enabledFlags.includes("HibpBreachNotifications")) {
console.info("Feature flag not enabled: HibpBreachNotifications");

Просмотреть файл

@ -14,7 +14,7 @@ import { getLatestOnerepScanResults } from "../../../db/tables/onerep_scans";
import { RemovalStatus } from "../universal/scanResult.js";
import {
FeatureFlagName,
getFlagsEnabledForEmail,
getEnabledFeatureFlags,
} from "../../../db/tables/featureFlags";
const log = mozlog("external.onerep");
@ -319,7 +319,7 @@ export async function isEligibleForFreeScan(
throw new Error("No session");
}
const enabledFlags = await getFlagsEnabledForEmail(user.email);
const enabledFlags = await getEnabledFeatureFlags({ email: user.email });
if (!enabledFlags.includes("FreeBrokerScan")) {
return false;
}

Просмотреть файл

@ -35,46 +35,24 @@ export type FeatureFlagName =
| "FalseDoorTest"
| "HibpBreachNotifications";
/**
* Get all feature flags that are enabled for everyone
*
* If you have a user that is logged in, it is recommended to use
* [[getFlagsEnabledForEmail]], which also returns flags that are enabled for
* that user specifically.
*
* @returns A list of feature flags that are enabled for everyone
*/
export async function getFlagsEnabledForEveryone(): Promise<FeatureFlagName[]> {
const enabledFlagNames = await knex("feature_flags")
.select("name")
.where("deleted_at", null)
.and.where("expired_at", null)
.and.where("is_enabled", true)
.and.whereRaw("ARRAY_LENGTH(allow_list, 1) IS NULL")
.orderBy("created_at", "desc");
return enabledFlagNames.map((row) => row.name as FeatureFlagName);
}
/**
* Get all feature flags that are enabled for the user with a given email address
*
* This returns a list of all the feature flags that are enabled for everyone,
* and those that are enabled for just the user whose email is given.
*
* @param email Email address of the current user
* @returns A list of feature flags that are enabled for this user
*/
export async function getFlagsEnabledForEmail(
email: string
export async function getEnabledFeatureFlags(
options:
| { ignoreAllowlist?: false; email: string }
| { ignoreAllowlist: true }
): Promise<FeatureFlagName[]> {
const enabledFlagNames = await knex("feature_flags")
let query = knex("feature_flags")
.select("name")
.where("deleted_at", null)
.and.where("expired_at", null)
.and.where("is_enabled", true)
.and.whereRaw("ARRAY_LENGTH(allow_list, 1) IS NULL")
.orWhereRaw("? = ANY(allow_list)", email);
.and.where("is_enabled", true);
if (!options.ignoreAllowlist) {
query = query.and
.whereRaw("ARRAY_LENGTH(allow_list, 1) IS NULL")
.orWhereRaw("? = ANY(allow_list)", options.email);
}
const enabledFlagNames = await query;
return enabledFlagNames.map((row) => row.name as FeatureFlagName);
}