Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(frontend): disable SQL editor link button when access denied (#3639)

Liu Ji25140b2e4b35
diff --git a/frontend/src/components/DatabaseTable.vue b/frontend/src/components/DatabaseTable.vue
index ee6e20036b..c01292cbcc 100644
--- a/frontend/src/components/DatabaseTable.vue
+++ b/frontend/src/components/DatabaseTable.vue
@@ -41,11 +41,12 @@
<div class="flex items-center space-x-2">
<button
v-if="showSQLEditorLink"
- class="btn-icon tooltip-wrapper"
+ class="btn-icon tooltip-wrapper disabled:hover:text-control"
+ :disabled="!allowQuery(database)"
@click.stop="gotoSQLEditor(database)"
>
<heroicons-solid:terminal class="w-5 h-5" />
- <div class="tooltip whitespace-nowrap">
+ <div v-if="allowQuery(database)" class="tooltip whitespace-nowrap">
{{ $t("sql-editor.self") }}
</div>
</button>
@@ -229,12 +230,18 @@ import { useRouter } from "vue-router";
import { NTooltip } from "naive-ui";
import { useI18n } from "vue-i18n";
import cloneDeep from "lodash-es/cloneDeep";
-import { connectionSlug, databaseSlug, isPITRDatabase } from "../utils";
-import type { Database } from "../types";
+import {
+ connectionSlug,
+ databaseSlug,
+ isDatabaseAccessible,
+ isPITRDatabase,
+} from "../utils";
+import type { Database, Policy } from "../types";
import { DEFAULT_PROJECT_ID, UNKNOWN_ID } from "../types";
import { BBTableColumn } from "../bbkit/types";
import InstanceEngineIcon from "./InstanceEngineIcon.vue";
import TenantIcon from "./TenantIcon.vue";
+import { useCurrentUser } from "@/store";
type Mode =
| "ALL"
@@ -279,11 +286,16 @@ const props = defineProps({
required: true,
type: Object as PropType<Database[]>,
},
+ policyList: {
+ type: Array as PropType<Policy[]>,
+ default: () => [],
+ },
});
const emit = defineEmits(["select-database"]);
const router = useRouter();
+const currentUser = useCurrentUser();
const { t } = useI18n();
const state = reactive<State>({
showIncorrectProjectModal: false,
@@ -491,11 +503,18 @@ const showSQLEditorLink = computed(() => {
return true;
});
+const allowQuery = (database: Database) => {
+ return isDatabaseAccessible(database, props.policyList, currentUser.value);
+};
+
const showTenantIcon = computed(() => {
return ["ALL", "ALL_SHORT", "INSTANCE"].includes(props.mode);
});
const gotoSQLEditor = (database: Database) => {
+ if (!allowQuery(database)) {
+ return;
+ }
// SQL editors can only query databases in the projects available to the user.
if (
database.projectId === UNKNOWN_ID ||
diff --git a/frontend/src/components/ProjectOverviewPanel.vue b/frontend/src/components/ProjectOverviewPanel.vue
index 96079e83ad..4e11eb1408 100644
--- a/frontend/src/components/ProjectOverviewPanel.vue
+++ b/frontend/src/components/ProjectOverviewPanel.vue
@@ -71,7 +71,12 @@
<NSpin />
</div>
</template>
- <DatabaseTable v-else :mode="'PROJECT'" :database-list="databaseList" />
+ <DatabaseTable
+ v-else
+ :mode="'PROJECT'"
+ :database-list="databaseList"
+ :policy-list="accessControlPolicyList"
+ />
</template>
<div v-else class="text-center textinfolabel">
<i18n-t keypath="project.overview.no-db-prompt" tag="p">
@@ -165,7 +170,11 @@ import TenantDatabaseTable, { YAxisRadioGroup } from "./TenantDatabaseTable";
import { IssueTable } from "../components/Issue";
import { Activity, Database, Issue, Project, LabelKeyType } from "../types";
import { findDefaultGroupByLabel } from "../utils";
-import { useActivityStore, useLabelList } from "@/store";
+import {
+ useActivityStore,
+ useLabelList,
+ usePolicyListByResourceTypeAndPolicyType,
+} from "@/store";
import PagedIssueTable from "@/components/Issue/PagedIssueTable.vue";
// Show at most 5 activity
@@ -213,6 +222,10 @@ export default defineComponent({
yAxisLabel: undefined,
});
const activityStore = useActivityStore();
+ const accessControlPolicyList = usePolicyListByResourceTypeAndPolicyType({
+ resourceType: "database",
+ policyType: "bb.policy.access-control",
+ });
const prepareActivityList = () => {
state.isFetchingActivityList = true;
@@ -277,6 +290,7 @@ export default defineComponent({
filteredDatabaseList,
labelList,
selectableLabelList,
+ accessControlPolicyList,
};
},
});
diff --git a/frontend/src/store/modules/policy.ts b/frontend/src/store/modules/policy.ts
index c55b16e901..e8f51bfe2b 100644
--- a/frontend/src/store/modules/policy.ts
+++ b/frontend/src/store/modules/policy.ts
@@ -1,8 +1,10 @@
import { defineStore } from "pinia";
+import { computed, Ref, unref, watchEffect } from "vue";
import axios from "axios";
import {
DatabaseId,
EnvironmentId,
+ MaybeRef,
PolicyState,
ResourceIdentifier,
ResourceObject,
@@ -19,7 +21,6 @@ import {
} from "@/types/policy";
import { getPrincipalFromIncludedList } from "./principal";
import { useEnvironmentStore } from "./environment";
-import { computed, Ref, watchEffect } from "vue";
import { useCurrentUser } from "./auth";
function convertEnvironment(
@@ -339,42 +340,38 @@ export const usePolicyByEnvironmentAndType = (
};
export const usePolicyByDatabaseAndType = (
- params: Ref<{ databaseId: DatabaseId; type: PolicyType }>
+ params: MaybeRef<{ databaseId: DatabaseId; type: PolicyType }>
) => {
const store = usePolicyStore();
const currentUser = useCurrentUser();
watchEffect(() => {
if (currentUser.value.id === UNKNOWN_ID) return;
-
- store.fetchPolicyByDatabaseAndType(params.value);
+ store.fetchPolicyByDatabaseAndType(unref(params));
});
- return computed(() =>
- store.getPolicyByDatabaseIdAndType(
- params.value.databaseId,
- params.value.type
- )
- );
+ return computed(() => {
+ const { databaseId, type } = unref(params);
+ return store.getPolicyByDatabaseIdAndType(databaseId, type);
+ });
};
export const usePolicyListByResourceTypeAndPolicyType = (
- params: Ref<{ resourceType: PolicyResourceType; policyType: PolicyType }>
+ params: MaybeRef<{ resourceType: PolicyResourceType; policyType: PolicyType }>
) => {
const store = usePolicyStore();
const currentUser = useCurrentUser();
watchEffect(() => {
if (currentUser.value.id === UNKNOWN_ID) return;
+ const { resourceType, policyType } = unref(params);
- store.fetchPolicyListByResourceTypeAndPolicyType(
- params.value.resourceType,
- params.value.policyType
- );
+ store.fetchPolicyListByResourceTypeAndPolicyType(resourceType, policyType);
});
- return computed(() =>
- store.getPolicyListByResourceTypeAndPolicyType(
- params.value.resourceType,
- params.value.policyType
- )
- );
+ return computed(() => {
+ const { resourceType, policyType } = unref(params);
+ return store.getPolicyListByResourceTypeAndPolicyType(
+ resourceType,
+ policyType
+ );
+ });
};
diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts
index 06c2912a91..3198df0588 100644
--- a/frontend/src/utils/index.ts
+++ b/frontend/src/utils/index.ts
@@ -16,6 +16,7 @@ export * from "./deployment";
export * from "./tab";
export * from "./task";
export * from "./principal";
+export * from "./policy";
export * from "./sql-editor";
export * from "./sheet";
export * from "./pev2";
diff --git a/frontend/src/utils/policy.ts b/frontend/src/utils/policy.ts
new file mode 100644
index 0000000000..8603a446cf
--- /dev/null
+++ b/frontend/src/utils/policy.ts
@@ -0,0 +1,46 @@
+import { hasFeature } from "@/store";
+import type { Database, Policy, Principal } from "@/types";
+import { hasWorkspacePermission } from "./role";
+
+export const isDatabaseAccessible = (
+ database: Database,
+ policyList: Policy[],
+ user: Principal
+) => {
+ if (!hasFeature("bb.feature.access-control")) {
+ // The current plan doesn't have access control feature.
+ // Fallback to true.
+ return true;
+ }
+
+ if (
+ hasWorkspacePermission(
+ "bb.permission.workspace.manage-access-control",
+ user.role
+ )
+ ) {
+ // The current user has the super privilege to access all databases.
+ // AKA. Owners and DBAs
+ return true;
+ }
+
+ const { environment } = database.instance;
+ if (environment.tier === "UNPROTECTED") {
+ return true;
+ }
+
+ const policy = policyList.find((policy) => {
+ const { type, resourceId, rowStatus } = policy;
+ return (
+ type === "bb.policy.access-control" &&
+ resourceId === database.id &&
+ rowStatus === "NORMAL"
+ );
+ });
+ if (policy) {
+ // The database is in the allowed list
+ return true;
+ }
+ // denied otherwise
+ return false;
+};
diff --git a/frontend/src/views/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail.vue
index 1d94d36af2..c99932f4e2 100644
--- a/frontend/src/views/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail.vue
@@ -96,7 +96,12 @@
</dd>
</template>
<dd
- class="flex items-center text-sm md:mr-4 cursor-pointer textlabel hover:text-accent"
+ class="flex items-center text-sm md:mr-4"
+ :class="
+ allowQuery
+ ? ['textlabel cursor-pointer hover:text-accent']
+ : ['text-gray-400 cursor-not-allowed']
+ "
@click.prevent="gotoSQLEditor"
>
<span class="mr-1">{{ $t("sql-editor.self") }}</span>
@@ -309,6 +314,7 @@ import {
hidePrefix,
allowGhostMigration,
isPITRDatabase,
+ isDatabaseAccessible,
} from "@/utils";
import {
ProjectId,
@@ -327,6 +333,7 @@ import {
pushNotification,
useCurrentUser,
useDatabaseStore,
+ usePolicyByDatabaseAndType,
useRepositoryStore,
useSQLStore,
} from "@/store";
@@ -383,6 +390,18 @@ const database = computed((): Database => {
return databaseStore.getDatabaseById(idFromSlug(props.databaseSlug));
});
+const accessControlPolicy = usePolicyByDatabaseAndType(
+ computed(() => ({
+ databaseId: database.value.id,
+ type: "bb.policy.access-control",
+ }))
+);
+const allowQuery = computed(() => {
+ const policy = accessControlPolicy.value;
+ const list = policy ? [policy] : [];
+ return isDatabaseAccessible(database.value, list, currentUser.value);
+});
+
// Project can be transferred if meets either of the condition below:
// - Database is in default project
// - Workspace role can manage instance
@@ -641,6 +660,10 @@ const selectDatabaseTabOnHash = () => {
};
const gotoSQLEditor = () => {
+ if (!allowQuery.value) {
+ return;
+ }
+
// SQL editors can only query databases in the projects available to the user.
if (
database.value.projectId === UNKNOWN_ID ||