Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Move plan/feature to a module as it requires to maintain mutatable state (when changing plan)
天舟a31edf21c9a6
diff --git a/frontend/src/components/PrincipalSelect.vue b/frontend/src/components/PrincipalSelect.vueindex 483a8eb016..822f0ae08b 100644--- a/frontend/src/components/PrincipalSelect.vue+++ b/frontend/src/components/PrincipalSelect.vue@@ -38,7 +38,7 @@import { reactive, computed, watch, PropType } from "vue";import { useStore } from "vuex";import { RoleMapping, Principal, PrincipalId, RoleType } from "../types";-import { feature, isDBA, isDeveloper, isOwner } from "../utils";+import { isDBA, isDeveloper, isOwner } from "../utils";interface LocalState {selectedId?: PrincipalId;diff --git a/frontend/src/main.js b/frontend/src/main.jsindex bf87b5946e..494bcb3be8 100644--- a/frontend/src/main.js+++ b/frontend/src/main.js@@ -41,6 +41,7 @@ import {instanceSlug,databaseSlug,dataSourceSlug,+ registerStoreWithRoleUtil,} from "./utils";if (isDevOrDemo()) {@@ -62,6 +63,8 @@ app.config.globalProperties.instanceSlug = instanceSlug;app.config.globalProperties.databaseSlug = databaseSlug;app.config.globalProperties.dataSourceSlug = dataSourceSlug;+registerStoreWithRoleUtil(store);+app// Need to use a directive on the element.// The normal hljs.initHighlightingOnLoad() won't work because router change would cause vuediff --git a/frontend/src/store/index.ts b/frontend/src/store/index.tsindex 0781567cf8..7b8ff4c451 100644--- a/frontend/src/store/index.ts+++ b/frontend/src/store/index.ts@@ -9,6 +9,7 @@ import instance from "./modules/instance";import dataSource from "./modules/dataSource";import database from "./modules/database";import principal from "./modules/principal";+import plan from "./modules/plan";import auth from "./modules/auth";import task from "./modules/task";@@ -32,6 +33,7 @@ export const store: Store<any> = createStore({dataSource,database,principal,+ plan,auth,task,uistate,diff --git a/frontend/src/store/modules/plan.ts b/frontend/src/store/modules/plan.tsnew file mode 100644index 0000000000..18a5aaa4a8--- /dev/null+++ b/frontend/src/store/modules/plan.ts@@ -0,0 +1,30 @@+import { FeatureType, PlanState, PlanType } from "../../types";++// A map from the a particular feature to the respective enablement of a particular plan+const FEATURE_MATRIX: Map<FeatureType, boolean[]> = new Map([+ ["bytebase.admin", [false, true, true]],+]);++const state: () => PlanState = () => ({});++const getters = {+ currentPlan: (state: PlanState) => (): PlanType => {+ return PlanType.FREE;+ },++ feature: (state: PlanState, getters: any) => (type: FeatureType): boolean => {+ return FEATURE_MATRIX.get(type)![getters["currentPlan"]()];+ },+};++const actions = {};++const mutations = {};++export default {+ namespaced: true,+ state,+ getters,+ actions,+ mutations,+};diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.tsindex fda012f967..ec2765fe47 100644--- a/frontend/src/types/bytebase.ts+++ b/frontend/src/types/bytebase.ts@@ -586,6 +586,17 @@ export type ActivateInfo = {token: string;};+// Plan+export type FeatureType =+ // - Task can only be assigned to DBA and Owner+ "bytebase.admin";++export enum PlanType {+ FREE = 0,+ STANDARD = 1,+ PREMIUM = 2,+}+// UI State Modelsexport type RouterSlug = {taskSlug?: string;@@ -646,6 +657,8 @@ export interface AuthState {currentUser: Principal;}+export interface PlanState {}+export interface RoleMappingState {roleMappingList: RoleMapping[];}diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.tsindex 4fde78e399..b53bf52c0a 100644--- a/frontend/src/utils/index.ts+++ b/frontend/src/utils/index.ts@@ -3,6 +3,5 @@ export * from "./database";export * from "./link";export * from "./role";export * from "./slug";-export * from "./plan";export * from "./task";export * from "./util";diff --git a/frontend/src/utils/plan.ts b/frontend/src/utils/plan.tsdeleted file mode 100644index 7c92e0c28c..0000000000--- a/frontend/src/utils/plan.ts+++ /dev/null@@ -1,22 +0,0 @@-export type FeatureType =- // - Task can only be assigned to DBA and Owner- "bytebase.admin";--export enum PlanType {- FREE = 0,- STANDARD = 1,- PREMIUM = 2,-}--// A map from the a particular feature to the respective enablement of a particular plan-const FEATURE_MATRIX: Map<FeatureType, boolean[]> = new Map([- ["bytebase.admin", [false, true, true]],-]);--// Feature gate. Returns true if the particular "type" feature is supported-export function feature(type: FeatureType): boolean {- const currentPlanType = PlanType.STANDARD;- // It must be a typo or we forget adding the entry to the FEATURE_MATRIX,- // so let's just crash it.- return FEATURE_MATRIX.get(type)![currentPlanType];-}diff --git a/frontend/src/utils/role.ts b/frontend/src/utils/role.tsindex f31334975b..f9788d2ed1 100644--- a/frontend/src/utils/role.ts+++ b/frontend/src/utils/role.ts@@ -1,17 +1,29 @@+import { Store } from "vuex";import { RoleType } from "../types";-import { feature } from "./plan";++let store: Store<any>;++export function registerStoreWithRoleUtil(theStore: Store<any>) {+ store = theStore;+}// Returns true if admin feature is NOT supported or the principal is OWNERexport function isOwner(role: RoleType): boolean {- return !feature("bytebase.admin") || role == "OWNER";+ return !store.getters["plan/feature"]("bytebase.admin") || role == "OWNER";}// Returns true if admin feature is NOT supported or the principal DBA or OWNERexport function isDBA(role: RoleType): boolean {- return !feature("bytebase.admin") || role == "DBA" || role == "OWNER";+ return (+ !store.getters["plan/feature"]("bytebase.admin") ||+ role == "DBA" ||+ role == "OWNER"+ );}// Returns true if admin feature is NOT supported or the principal is DEVELOPERexport function isDeveloper(role: RoleType): boolean {- return !feature("bytebase.admin") || role == "DEVELOPER";+ return (+ !store.getters["plan/feature"]("bytebase.admin") || role == "DEVELOPER"+ );}