Skip to content

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

feat(sql-editor): implement the sheet accessibility correctly (#2733)

Liu Jiab73ac5c7ca9
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 1554a37468..fdff1ee317 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -14,6 +14,7 @@ import {
useSheetStore,
useDebugStore,
useInstanceStore,
+ pushNotification,
} from "@/store";
import {
Instance,
@@ -28,7 +29,9 @@ import {
mapConnectionAtom,
sheetSlug as makeSheetSlug,
connectionSlug as makeConnectionSlug,
+ isSheetReadable,
} from "@/utils";
+import { useI18n } from "vue-i18n";
type LocalState = {
instanceList: Instance[];
@@ -37,6 +40,7 @@ type LocalState = {
const route = useRoute();
const router = useRouter();
+const { t } = useI18n();
const state = reactive<LocalState>({
instanceList: [],
@@ -59,9 +63,11 @@ const prepareAccessibleConnectionByProject = async () => {
// `databaseList` is the database list accessible by current user.
// Only accessible instances and databases will be listed in the tree.
- const databaseList = await databaseStore.fetchDatabaseList({
- syncStatus: "OK",
- });
+ const databaseList = (
+ await databaseStore.fetchDatabaseList({
+ syncStatus: "OK",
+ })
+ ).filter((db) => db.project.id !== DEFAULT_PROJECT_ID);
state.instanceList = uniqBy(
databaseList.map((db) => db.instance),
(instance) => instance.id
@@ -103,6 +109,14 @@ const prepareSheet = async () => {
if (sheet.id === UNKNOWN_ID) {
return false;
}
+ if (!isSheetReadable(sheet, currentUser.value)) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("common.access-denied"),
+ });
+ return false;
+ }
const openingSheetTab = tabStore.tabList.find(
(tab) => tab.sheetId === sheet.id
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 82fbcd7153..20806fde1d 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -208,7 +208,8 @@
"sync-now": "Sync Now",
"show-help": "Show help",
"success": "Succeed",
- "load-more": "Load more"
+ "load-more": "Load more",
+ "access-denied": "Access denied"
},
"error-page": {
"go-back-home": "Go back home"
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index e8a5adc74c..e5a4d9b34a 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -208,7 +208,8 @@
"sync-now": "现在同步",
"show-help": "查看帮助",
"success": "成功",
- "load-more": "加载更多"
+ "load-more": "加载更多",
+ "access-denied": "拒绝访问"
},
"error-page": {
"go-back-home": "回到主页"
diff --git a/frontend/src/store/modules/sheet.ts b/frontend/src/store/modules/sheet.ts
index e44fb66e11..9fd0f10aa5 100644
--- a/frontend/src/store/modules/sheet.ts
+++ b/frontend/src/store/modules/sheet.ts
@@ -23,6 +23,7 @@ import { useAuthStore } from "./auth";
import { useDatabaseStore } from "./database";
import { useProjectStore } from "./project";
import { useTabStore } from "./tab";
+import { isSheetWritable } from "@/utils";
function convertSheet(
sheet: ResourceObject,
@@ -104,37 +105,20 @@ export const useSheetStore = defineStore("sheet", {
return true;
}
- // The sheet is not saved yet, it is editable.
+ // The tab is not saved yet, it is editable.
if (currentSheet.id === UNKNOWN_ID) {
return false;
}
- // Always editable if current user is the creator of the sheet.
- if (currentSheet.creator.id === currentUser.id) {
- return false;
- }
-
- // Check the role of the current user in the sheet's project.
- if (currentSheet.visibility === "PROJECT") {
- const isCurrentUserProjectOwner = () => {
- const projectMemberList = currentSheet.project.memberList || [];
- const memberInProject = projectMemberList.find((member) => {
- return member.principal.id === currentUser.id;
- });
-
- return memberInProject && memberInProject.role === "OWNER";
- };
-
- return !isCurrentUserProjectOwner();
- }
-
- // visibility === "PRIVATE" | "PUBLIC"
- // Readonly if the sheet is private or public.
- return true;
+ return !isSheetWritable(currentSheet, currentUser);
},
},
actions: {
+ getSheetById(sheetId: SheetId) {
+ const sheet = this.sheetById.get(sheetId);
+ return sheet ?? unknown("SHEET");
+ },
setSheetList(payload: Sheet[]) {
this.sheetList = payload;
},
diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts
index 96454b43b4..e578c7e090 100644
--- a/frontend/src/utils/index.ts
+++ b/frontend/src/utils/index.ts
@@ -17,4 +17,5 @@ export * from "./tab";
export * from "./task";
export * from "./principal";
export * from "./sql-editor";
+export * from "./sheet";
export * from "./pev2";
diff --git a/frontend/src/utils/sheet.ts b/frontend/src/utils/sheet.ts
new file mode 100644
index 0000000000..8bf65624a6
--- /dev/null
+++ b/frontend/src/utils/sheet.ts
@@ -0,0 +1,56 @@
+import { Principal, Sheet } from "@/types";
+
+export const isSheetReadable = (sheet: Sheet, currentUser: Principal) => {
+ // readable to
+ // PRIVATE: the creator only
+ // PROJECT: the creator and members in the project
+ // PUBLIC: everyone
+
+ if (sheet.creator.id === currentUser.id) {
+ // Always readable to the creator
+ return true;
+ }
+ const { visibility } = sheet;
+ if (visibility === "PRIVATE") {
+ return false;
+ }
+ if (visibility === "PROJECT") {
+ const projectMemberList = sheet.project.memberList;
+ return (
+ projectMemberList.findIndex(
+ (member) => member.principal.id === currentUser.id
+ ) >= 0
+ );
+ }
+ // visibility === "PUBLIC"
+ return true;
+};
+
+export const isSheetWritable = (sheet: Sheet, currentUser: Principal) => {
+ // writable to
+ // PRIVATE: the creator only
+ // PROJECT: the creator and the OWNER of project
+ // PUBLIC: no one but the creator
+
+ if (sheet.creator.id === currentUser.id) {
+ // Always readable to the creator
+ return true;
+ }
+ const { visibility } = sheet;
+ if (visibility === "PRIVATE") {
+ return false;
+ }
+ if (visibility === "PROJECT") {
+ const isCurrentUserProjectOwner = () => {
+ const projectMemberList = sheet.project.memberList || [];
+ const memberInProject = projectMemberList.find((member) => {
+ return member.principal.id === currentUser.id;
+ });
+
+ return memberInProject && memberInProject.role === "OWNER";
+ };
+ return isCurrentUserProjectOwner();
+ }
+ // visibility === "PUBLIC"
+ return false;
+};
diff --git a/frontend/src/views/sql-editor/EditorPanel/SharePopover.vue b/frontend/src/views/sql-editor/EditorPanel/SharePopover.vue
index 1317bb7bb5..3b4e9b3e12 100644
--- a/frontend/src/views/sql-editor/EditorPanel/SharePopover.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/SharePopover.vue
@@ -25,10 +25,8 @@
:key="option.label"
class="p-2 rounded-sm flex justify-between"
:class="[
- creatorAccessStyle,
- {
- 'bg-accent text-white': option.value === currentAccess.value,
- },
+ isCreator && 'cursor-pointer hover:bg-gray-200',
+ option.value === currentAccess.value && 'bg-gray-200',
]"
@click="handleChangeAccess(option)"
>
@@ -46,7 +44,7 @@
<h2 class="text-md flex">
{{ option.label }}
</h2>
- <h3 class="text-xs" :class="creatorAccessDescStyle">
+ <h3 class="text-xs">
{{ option.description }}
</h3>
</section>
@@ -116,20 +114,11 @@ const accessOptions = computed<AccessOption[]>(() => {
});
const sheet = computed(() => sheetStore.currentSheet);
-const creator = computed(() => sheetStore.isCreator);
+const isCreator = computed(() => sheetStore.isCreator);
const currentAccess = ref<AccessOption>(accessOptions.value[0]);
const isShowAccessPopover = ref(false);
-const creatorAccessStyle = computed(() => {
- return creator.value
- ? "cursor-pointer hover:bg-accent hover:text-white"
- : "bg-transparent text-gray-300 cursor-not-allowed";
-});
-const creatorAccessDescStyle = computed(() => {
- return creator.value ? "text-gray-400" : "text-gray-300 cursor-not-allowed";
-});
-
const updateSheet = () => {
if (tabStore.currentTab.sheetId) {
sheetStore.patchSheetById({
@@ -141,7 +130,7 @@ const updateSheet = () => {
const handleChangeAccess = (option: AccessOption) => {
// only creator can change access
- if (creator.value) {
+ if (isCreator.value) {
currentAccess.value = option;
updateSheet();
}