mirror of
https://github.com/bitwarden/browser
synced 2026-02-23 16:13:21 +00:00
* Use typescript-strict-plugin to iteratively turn on strict * Add strict testing to pipeline Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files. * turn on strict for scripts directory * Use plugin for all tsconfigs in monorepo vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes. * remove plugin from configs that extend one that already has it * Update workspace settings to honor strict plugin * Apply strict-plugin to native message test runner * Update vscode workspace to use root tsc version * `./node_modules/.bin/update-strict-comments` 🤖 This is a one-time operation. All future files should adhere to strict type checking. * Add fixme to `ts-strict-ignore` comments * `update-strict-comments` 🤖 repeated for new merge files
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
import { inject } from "@angular/core";
|
|
import { CanActivateFn, Router } from "@angular/router";
|
|
|
|
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
|
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
|
import { ToastService } from "@bitwarden/components";
|
|
|
|
// Replace this with a type safe lookup of the feature flag values in PM-2282
|
|
type FlagValue = boolean | number | string;
|
|
|
|
/**
|
|
* Returns a CanActivateFn that checks if the feature flag is enabled. If not, it shows an "Access Denied!"
|
|
* toast and optionally redirects to the specified url.
|
|
* @param featureFlag - The feature flag to check
|
|
* @param requiredFlagValue - Optional value to the feature flag must be equal to, defaults to true
|
|
* @param redirectUrlOnDisabled - Optional url to redirect to if the feature flag is disabled
|
|
* @param showToast - Optional boolean to show a toast if the feature flag is disabled - defaults to true
|
|
*/
|
|
export const canAccessFeature = (
|
|
featureFlag: FeatureFlag,
|
|
requiredFlagValue: FlagValue = true,
|
|
redirectUrlOnDisabled?: string,
|
|
showToast = true,
|
|
): CanActivateFn => {
|
|
return async () => {
|
|
const configService = inject(ConfigService);
|
|
const toastService = inject(ToastService);
|
|
const router = inject(Router);
|
|
const i18nService = inject(I18nService);
|
|
const logService = inject(LogService);
|
|
|
|
try {
|
|
const flagValue = await configService.getFeatureFlag(featureFlag);
|
|
|
|
if (flagValue === requiredFlagValue) {
|
|
return true;
|
|
}
|
|
|
|
if (showToast) {
|
|
toastService.showToast({
|
|
variant: "error",
|
|
title: null,
|
|
message: i18nService.t("accessDenied"),
|
|
});
|
|
}
|
|
|
|
if (redirectUrlOnDisabled != null) {
|
|
return router.createUrlTree([redirectUrlOnDisabled]);
|
|
}
|
|
return false;
|
|
} catch (e) {
|
|
logService.error(e);
|
|
return false;
|
|
}
|
|
};
|
|
};
|