Skip to content

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

refactor(frontend): ship project composed member list to project iam policy (#6115)

Liu Jif10756d585df
diff --git a/frontend/src/components/DatabaseDetail/Settings/components/Secrets.vue b/frontend/src/components/DatabaseDetail/Settings/components/Secrets.vue
index bf810ad5ae..eb87a4e6fd 100644
--- a/frontend/src/components/DatabaseDetail/Settings/components/Secrets.vue
+++ b/frontend/src/components/DatabaseDetail/Settings/components/Secrets.vue
@@ -199,9 +199,11 @@ import {
useDatabaseSecretStore,
hasFeature,
useCurrentUser,
+ useCurrentUserV1,
+ useProjectV1Store,
} from "@/store";
import { useGracefulRequest } from "@/store/modules/utils";
-import { hasPermissionInProject, hasWorkspacePermission } from "@/utils";
+import { hasPermissionInProjectV1, hasWorkspacePermission } from "@/utils";
export type Detail = {
secret: Secret;
@@ -228,6 +230,7 @@ const parent = computed(() => {
const detail = ref<Detail>();
const showFeatureModal = ref(false);
const currentUser = useCurrentUser();
+const currentUserV1 = useCurrentUserV1();
const COLUMNS = computed(() => {
const columns: BBGridColumn[] = [
@@ -248,14 +251,17 @@ const COLUMNS = computed(() => {
});
const allowAdmin = computed(() => {
+ const projectV1 = useProjectV1Store().getProjectByUID(
+ String(props.database.project.id)
+ );
return (
hasWorkspacePermission(
"bb.permission.workspace.manage-database-secrets",
currentUser.value.role
) ||
- hasPermissionInProject(
- props.database.project,
- currentUser.value,
+ hasPermissionInProjectV1(
+ projectV1.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.manage-database-secrets"
)
);
diff --git a/frontend/src/components/DatabaseOverviewPanel.vue b/frontend/src/components/DatabaseOverviewPanel.vue
index d11d305f4f..8382673e24 100644
--- a/frontend/src/components/DatabaseOverviewPanel.vue
+++ b/frontend/src/components/DatabaseOverviewPanel.vue
@@ -233,7 +233,7 @@
<script lang="ts" setup>
import { cloneDeep, head, isEqual } from "lodash-es";
import { computed, reactive, watchEffect, PropType } from "vue";
-import { hasWorkspacePermission, memberListInProject } from "../utils";
+import { hasWorkspacePermission, roleListInProjectV1 } from "../utils";
import {
Anomaly,
Database,
@@ -247,6 +247,8 @@ import {
useDataSourceStore,
useAnomalyList,
useDBSchemaStore,
+ useProjectV1Store,
+ useCurrentUserV1,
} from "@/store";
import { BBTableSectionDataSource } from "../bbkit/types";
import AnomalyTable from "../components/AnomalyTable.vue";
@@ -275,6 +277,7 @@ const state = reactive<LocalState>({
});
const currentUser = useCurrentUser();
+const currentUserV1 = useCurrentUserV1();
const dbSchemaStore = useDBSchemaStore();
const databaseEngine = computed(() => {
@@ -378,13 +381,11 @@ const allowViewDataSource = computed(() => {
return true;
}
- return (
- memberListInProject(
- props.database.project,
- currentUser.value,
- /* empty array to "ALL" */ []
- ).length > 0
+ const project = useProjectV1Store().getProjectByUID(
+ String(props.database.project.id)
);
+
+ return roleListInProjectV1(project.iamPolicy, currentUserV1.value).length > 0;
});
const dataSourceList = computed(() => {
diff --git a/frontend/src/components/Issue/rollback/common.ts b/frontend/src/components/Issue/rollback/common.ts
index 91bba6c3a6..99cc503ae7 100644
--- a/frontend/src/components/Issue/rollback/common.ts
+++ b/frontend/src/components/Issue/rollback/common.ts
@@ -16,7 +16,7 @@ import {
UNKNOWN_ID,
} from "@/types";
import {
- hasPermissionInProject,
+ hasPermissionInProjectV1,
hasWorkspacePermission,
isTaskCreate,
isTaskSkipped,
@@ -26,8 +26,10 @@ import { flattenTaskList, useIssueLogic } from "../logic";
import {
useActivityStore,
useCurrentUser,
+ useCurrentUserV1,
useDatabaseStore,
useIssueStore,
+ useProjectV1Store,
} from "@/store";
const MIN_ROLLBACK_SQL_MYSQL_VERSION = "5.7.0";
@@ -39,6 +41,7 @@ export type RollbackUIType =
export const useRollbackLogic = () => {
const currentUser = useCurrentUser();
+ const currentUserV1 = useCurrentUserV1();
const context = useIssueLogic();
const {
create,
@@ -114,10 +117,13 @@ export const useRollbackLogic = () => {
return true;
}
+ const projectV1 = useProjectV1Store().getProjectByUID(
+ String(issueEntity.project.id)
+ );
if (
- hasPermissionInProject(
- issueEntity.project,
- user,
+ hasPermissionInProjectV1(
+ projectV1.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.admin-database"
)
) {
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 5134338225..6c835c50e2 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -201,7 +201,7 @@ const prepareSheet = async () => {
}
return false;
}
- if (!isSheetReadable(sheet, currentUser.value)) {
+ if (!isSheetReadable(sheet)) {
pushNotification({
module: "bytebase",
style: "CRITICAL",
diff --git a/frontend/src/layouts/ProjectLayout.vue b/frontend/src/layouts/ProjectLayout.vue
index fbbb3395ff..3d0679a634 100644
--- a/frontend/src/layouts/ProjectLayout.vue
+++ b/frontend/src/layouts/ProjectLayout.vue
@@ -47,7 +47,7 @@ import { startCase } from "lodash-es";
import {
idFromSlug,
hasWorkspacePermission,
- hasPermissionInProject,
+ hasPermissionInProjectV1,
} from "../utils";
import ArchiveBanner from "../components/ArchiveBanner.vue";
import { BBTabFilterItem } from "../bbkit/types";
@@ -56,7 +56,9 @@ import { Project, DEFAULT_PROJECT_ID } from "../types";
import {
useCurrentUser,
useCurrentUserIamPolicy,
+ useCurrentUserV1,
useLegacyProjectStore,
+ useProjectV1Store,
} from "@/store";
type ProjectTabItem = {
@@ -88,10 +90,17 @@ export default defineComponent({
const { t } = useI18n();
const currentUser = useCurrentUser();
- const projectStore = useLegacyProjectStore();
+ const currentUserV1 = useCurrentUserV1();
+ const legacyProjectStore = useLegacyProjectStore();
+ const projectV1Store = useProjectV1Store();
const project = computed((): Project => {
- return projectStore.getProjectById(idFromSlug(props.projectSlug));
+ return legacyProjectStore.getProjectById(idFromSlug(props.projectSlug));
+ });
+ const projectV1 = computed(() => {
+ return projectV1Store.getProjectByUID(
+ String(idFromSlug(props.projectSlug))
+ );
});
const currentUserIamPolicy = useCurrentUserIamPolicy();
@@ -182,9 +191,9 @@ export default defineComponent({
}
if (
- hasPermissionInProject(
- project.value,
- currentUser.value,
+ hasPermissionInProjectV1(
+ projectV1.value.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.manage-general"
)
) {
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 27145d35c5..e2a958eeb8 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -23,11 +23,11 @@ import {
UNKNOWN_ID,
} from "../types";
import {
- hasPermissionInProject,
+ hasPermissionInProjectV1,
hasWorkspacePermission,
idFromSlug,
- memberListInProject,
migrationHistoryIdFromSlug,
+ roleListInProjectV1,
} from "../utils";
import Signin from "../views/auth/Signin.vue";
import Signup from "../views/auth/Signup.vue";
@@ -58,9 +58,11 @@ import {
useProjectV1Store,
useProjectWebhookV1Store,
useEnvironmentV1Store,
+ useCurrentUserV1,
} from "@/store";
import { useConversationStore } from "@/plugins/ai/store";
import { PlanType } from "@/types/proto/v1/subscription_service";
+import { State } from "@/types/proto/v1/common";
const HOME_MODULE = "workspace.home";
const AUTH_MODULE = "auth";
@@ -692,14 +694,15 @@ const routes: Array<RouteRecordRaw> = [
meta: {
quickActionListByRole: (route: RouteLocationNormalized) => {
const slug = route.params.projectSlug as string;
- const project = useLegacyProjectStore().getProjectById(
- idFromSlug(slug)
+ const project = useProjectV1Store().getProjectByUID(
+ String(idFromSlug(slug))
);
- if (project.rowStatus == "NORMAL") {
+ if (project.state === State.ACTIVE) {
const actionList: string[] = [];
const currentUser = useCurrentUser();
+ const currentUserV1 = useCurrentUserV1();
let allowAlterSchemaOrChangeData = false;
let allowCreateDB = false;
let allowTransferDB = false;
@@ -715,24 +718,24 @@ const routes: Array<RouteRecordRaw> = [
allowTransferDB = true;
allowTransferOutDB = true;
} else {
- const memberList = memberListInProject(
- project,
- currentUser.value
+ const roleList = roleListInProjectV1(
+ project.iamPolicy,
+ currentUserV1.value
);
- if (memberList.length > 0) {
- allowAlterSchemaOrChangeData = hasPermissionInProject(
- project,
- currentUser.value,
+ if (roleList.length > 0) {
+ allowAlterSchemaOrChangeData = hasPermissionInProjectV1(
+ project.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.change-database"
);
- allowTransferDB = hasPermissionInProject(
- project,
- currentUser.value,
+ allowTransferDB = hasPermissionInProjectV1(
+ project.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.transfer-database"
);
- allowTransferOutDB = hasPermissionInProject(
- project,
- currentUser.value,
+ allowTransferOutDB = hasPermissionInProjectV1(
+ project.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.transfer-database"
);
@@ -747,15 +750,15 @@ const routes: Array<RouteRecordRaw> = [
} else {
// See RBAC otherwise.
// AKA yes if project owner.
- allowCreateDB = hasPermissionInProject(
- project,
- currentUser.value,
+ allowCreateDB = hasPermissionInProjectV1(
+ project.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.create-database"
);
}
}
}
- if (project.id === DEFAULT_PROJECT_ID) {
+ if (project.uid === String(DEFAULT_PROJECT_ID)) {
allowAlterSchemaOrChangeData = false;
}
if (allowAlterSchemaOrChangeData) {
diff --git a/frontend/src/store/modules/sheet.ts b/frontend/src/store/modules/sheet.ts
index b6f9cd3eea..86a48933fd 100644
--- a/frontend/src/store/modules/sheet.ts
+++ b/frontend/src/store/modules/sheet.ts
@@ -117,7 +117,6 @@ export const useSheetStore = defineStore("sheet", {
* b) If the sheet's visibility is project, will be checked whether the current user is the `OWNER` of the project, only the current user is the `OWNER` of the project, it can be edited.
*/
isReadOnly() {
- const currentUser = useCurrentUser();
const currentSheet = this.currentSheet as Sheet;
// We don't have a selected sheet, we've got nothing to edit.
@@ -135,7 +134,7 @@ export const useSheetStore = defineStore("sheet", {
return true;
}
- return !isSheetWritable(currentSheet, currentUser.value);
+ return !isSheetWritable(currentSheet);
},
},
diff --git a/frontend/src/utils/project.ts b/frontend/src/utils/project.ts
index f0746a385c..cbdac36978 100644
--- a/frontend/src/utils/project.ts
+++ b/frontend/src/utils/project.ts
@@ -2,11 +2,8 @@ import {
DEFAULT_PROJECT_ID,
Principal,
PrincipalId,
- ProjectMember,
- ProjectRoleType,
type Project,
} from "../types";
-import { hasProjectPermission, ProjectPermissionType } from "./role";
export function projectName(project: Project) {
if (project.id === DEFAULT_PROJECT_ID) {
@@ -20,59 +17,6 @@ export function projectName(project: Project) {
return name;
}
-/**
- * Returns the members for a user in a project
- * @param project
- * @param user
- * @param roles roles filter. Empty array `[]` to "ALL"
- */
-export const memberListInProject = (
- project: Project,
- user: Principal,
- roles: ProjectRoleType[] = []
-): ProjectMember[] => {
- const memberList = project.memberList.filter(
- (member) => member.principal.id === user.id
- );
- if (roles.length === 0) return memberList;
-
- return memberList.filter((member) => roles.includes(member.role));
-};
-
-/**
- * Returns one of the membership in a project
- * @param project
- * @param user
- * @param role Empty to "ANY"
- */
-export const memberInProject = (
- project: Project,
- user: Principal,
- role?: ProjectRoleType
-): ProjectMember | undefined => {
- if (role) {
- return project.memberList.find((member) => member.principal.id === user.id);
- }
- return project.memberList.find(
- (member) => member.principal.id === user.id && member.role === role
- );
-};
-
-export const hasPermissionInProject = (
- project: Project,
- user: Principal,
- permission: ProjectPermissionType
-) => {
- const memberList = memberListInProject(project, user);
- if (
- memberList.some((member) => hasProjectPermission(permission, member.role))
- ) {
- return true;
- }
-
- return false;
-};
-
export const isMemberOfProject = (
project: Project,
userOrId: Principal | PrincipalId
diff --git a/frontend/src/utils/sheet.ts b/frontend/src/utils/sheet.ts
index ceb5460ba0..2cd8bf2e73 100644
--- a/frontend/src/utils/sheet.ts
+++ b/frontend/src/utils/sheet.ts
@@ -1,9 +1,13 @@
import { isUndefined, uniq } from "lodash-es";
-import { useSheetStore } from "@/store";
+import {
+ useCurrentUser,
+ useCurrentUserV1,
+ useProjectV1Store,
+ useSheetStore,
+} from "@/store";
import {
Issue,
- Principal,
Sheet,
SheetId,
SheetIssueBacktracePayload,
@@ -17,19 +21,22 @@ import {
TaskDatabaseSchemaUpdateSDLPayload,
} from "@/types";
import {
- hasPermissionInProject,
+ hasPermissionInProjectV1,
hasWorkspacePermission,
- isMemberOfProject,
+ isMemberOfProjectV1,
} from "../utils";
import { flattenTaskList } from "@/components/Issue/logic";
-export const isSheetReadable = (sheet: Sheet, currentUser: Principal) => {
+export const isSheetReadable = (sheet: Sheet) => {
+ const currentUser = useCurrentUser();
+ const currentUserV1 = useCurrentUserV1();
+
// readable to
// PRIVATE: the creator only
// PROJECT: the creator and members in the project, workspace Owner and DBA
// PUBLIC: everyone
- if (sheet.creator.id === currentUser.id) {
+ if (sheet.creator.id === currentUser.value.id) {
// Always readable to the creator
return true;
}
@@ -41,30 +48,36 @@ export const isSheetReadable = (sheet: Sheet, currentUser: Principal) => {
if (
hasWorkspacePermission(
"bb.permission.workspace.manage-project",
- currentUser.role
+ currentUser.value.role
)
) {
return true;
}
- return isMemberOfProject(sheet.project, currentUser);
+ const projectV1 = useProjectV1Store().getProjectByUID(
+ String(sheet.project.id)
+ );
+ return isMemberOfProjectV1(projectV1.iamPolicy, currentUserV1.value);
}
// visibility === "PUBLIC"
return true;
};
-export const isSheetWritable = (sheet: Sheet, currentUser: Principal) => {
+export const isSheetWritable = (sheet: Sheet) => {
// If the sheet is linked to an issue, it's NOT writable
if (getSheetIssueBacktracePayload(sheet)) {
return false;
}
+ const currentUser = useCurrentUser();
+ const currentUserV1 = useCurrentUserV1();
+
// writable to
// PRIVATE: the creator only
// PROJECT: the creator or project role can manage sheet, workspace Owner and DBA
// PUBLIC: the creator only
- if (sheet.creator.id === currentUser.id) {
+ if (sheet.creator.id === currentUser.value.id) {
// Always writable to the creator
return true;
}
@@ -76,16 +89,19 @@ export const isSheetWritable = (sheet: Sheet, currentUser: Principal) => {
if (
hasWorkspacePermission(
"bb.permission.workspace.manage-project",
- currentUser.role
+ currentUser.value.role
)
) {
return true;
}
+ const projectV1 = useProjectV1Store().getProjectByUID(
+ String(sheet.project.id)
+ );
const isCurrentUserProjectOwner = () => {
- return hasPermissionInProject(
- sheet.project,
- currentUser,
+ return hasPermissionInProjectV1(
+ projectV1.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.manage-sheet"
);
};
diff --git a/frontend/src/views/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail.vue
index d54735e9c9..f3fedcc5cc 100644
--- a/frontend/src/views/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail.vue
@@ -330,7 +330,7 @@ import {
instanceHasBackupRestore,
instanceHasAlterSchema,
instanceSupportSlowQuery,
- hasPermissionInProject,
+ hasPermissionInProjectV1,
} from "@/utils";
import {
UNKNOWN_ID,
@@ -347,8 +347,10 @@ import {
pushNotification,
useCurrentUser,
useCurrentUserIamPolicy,
+ useCurrentUserV1,
useDatabaseStore,
useDBSchemaStore,
+ useProjectV1ByUID,
useSQLStore,
} from "@/store";
import { usePolicyByParentAndType } from "@/store/modules/v1/policy";
@@ -409,11 +411,17 @@ const state = reactive<LocalState>({
});
const currentUser = useCurrentUser();
+const currentUserV1 = useCurrentUserV1();
const currentUserIamPolicy = useCurrentUserIamPolicy();
const database = computed((): Database => {
return databaseStore.getDatabaseById(idFromSlug(props.databaseSlug));
});
+const { project: projectV1 } = useProjectV1ByUID(
+ computed(() => {
+ return String(database.value.project.id);
+ })
+);
const allowToChangeDatabase = computed(() => {
return currentUserIamPolicy.allowToChangeDatabaseOfProject(
@@ -460,9 +468,9 @@ const allowTransferProject = computed(() => {
}
if (
- hasPermissionInProject(
- database.value.project,
- currentUser.value,
+ hasPermissionInProjectV1(
+ projectV1.value.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.transfer-database"
)
) {
@@ -508,9 +516,9 @@ const allowAdmin = computed(() => {
}
if (
- hasPermissionInProject(
- database.value.project,
- currentUser.value,
+ hasPermissionInProjectV1(
+ projectV1.value.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.admin-database"
)
) {
@@ -540,9 +548,9 @@ const allowEdit = computed(() => {
}
if (
- hasPermissionInProject(
- database.value.project,
- currentUser.value,
+ hasPermissionInProjectV1(
+ projectV1.value.iamPolicy,
+ currentUserV1.value,
"bb.permission.project.change-database"
)
) {
diff --git a/frontend/src/views/EnvironmentDetail.vue b/frontend/src/views/EnvironmentDetail.vue
index d3ee43a502..b19a5931ca 100644
--- a/frontend/src/views/EnvironmentDetail.vue
+++ b/frontend/src/views/EnvironmentDetail.vue
@@ -203,7 +203,7 @@ const success = () => {
module: "bytebase",
style: "SUCCESS",
title: t("environment.successfully-updated-environment", {
- name: state.environment.name,
+ name: state.environment.title,
}),
});
};
diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vue
index 2bb6ffea59..3f40f8bdd9 100644
--- a/frontend/src/views/InstanceDetail.vue
+++ b/frontend/src/views/InstanceDetail.vue
@@ -149,7 +149,7 @@ import {
idFromSlug,
hasWorkspacePermission,
instanceHasCreateDatabase,
- isMemberOfProject,
+ isMemberOfProjectV1,
} from "../utils";
import ArchiveBanner from "../components/ArchiveBanner.vue";
import DatabaseTable from "../components/DatabaseTable.vue";
@@ -175,6 +175,8 @@ import {
useSubscriptionStore,
useSQLStore,
useDBSchemaStore,
+ useProjectV1Store,
+ useCurrentUserV1,
} from "@/store";
const DATABASE_TAB = 0;
@@ -202,6 +204,7 @@ const subscriptionStore = useSubscriptionStore();
const { t } = useI18n();
const currentUser = useCurrentUser();
+const currentUserV1 = useCurrentUserV1();
const sqlStore = useSQLStore();
const state = reactive<LocalState>({
@@ -307,7 +310,10 @@ const databaseList = computed(() => {
// databases not meeting this criteria and we need to filter out them.
const filteredList: Database[] = [];
for (const database of list) {
- if (isMemberOfProject(database.project, currentUser.value)) {
+ const projectV1 = useProjectV1Store().getProjectByUID(
+ String(database.project.id)
+ );
+ if (isMemberOfProjectV1(projectV1.iamPolicy, currentUserV1.value)) {
filteredList.push(database);
}
}
diff --git a/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue b/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue
index af6c674454..23f5aeabc8 100644
--- a/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue
+++ b/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue
@@ -19,7 +19,7 @@ import { type DropdownOption, NDropdown, useDialog } from "naive-ui";
import type { Sheet, SheetCreate, SheetOrganizerUpsert } from "@/types";
import type { SheetViewMode } from "../types";
import { getDefaultSheetPayloadWithSource, isSheetWritable } from "@/utils";
-import { useCurrentUser, useSheetStore } from "@/store";
+import { useSheetStore } from "@/store";
const props = defineProps<{
view: SheetViewMode;
@@ -31,7 +31,6 @@ const emit = defineEmits<{
}>();
const { t } = useI18n();
-const currentUser = useCurrentUser();
const sheetStore = useSheetStore();
const dialog = useDialog();
@@ -51,7 +50,7 @@ const options = computed(() => {
});
}
- const canDeleteSheet = isSheetWritable(sheet, currentUser.value);
+ const canDeleteSheet = isSheetWritable(sheet);
if (view === "my") {
if (canDeleteSheet) {
options.push({