Skip to content

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

fix: the visibility of projects and dbs for workspace owner and DBA (#6131)

boojackab93e18ecb9f
diff --git a/frontend/src/components/DatabaseListSidePanel.vue b/frontend/src/components/DatabaseListSidePanel.vue
index 375dbac349..c005ae2191 100644
--- a/frontend/src/components/DatabaseListSidePanel.vue
+++ b/frontend/src/components/DatabaseListSidePanel.vue
@@ -7,8 +7,8 @@
/>
</template>
-<script lang="ts">
-import { computed, defineComponent, watchEffect } from "vue";
+<script lang="ts" setup>
+import { computed, watchEffect } from "vue";
import { cloneDeep, uniqBy } from "lodash-es";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
@@ -23,155 +23,144 @@ import {
useCurrentUserV1,
} from "@/store";
-export default defineComponent({
- name: "DatabaseListSidePanel",
- setup() {
- const { t } = useI18n();
- const databaseStore = useDatabaseStore();
- const router = useRouter();
+const { t } = useI18n();
+const databaseStore = useDatabaseStore();
+const router = useRouter();
- const currentUser = useCurrentUser();
- const currentUserV1 = useCurrentUserV1();
+const currentUser = useCurrentUser();
+const currentUserV1 = useCurrentUserV1();
- const rawEnvironmentList = useEnvironmentV1List();
+const rawEnvironmentList = useEnvironmentV1List();
- // Reserve the environment list, put "Prod" to the top.
- const environmentList = computed(() =>
- cloneDeep(rawEnvironmentList.value).reverse()
- );
+// Reserve the environment list, put "Prod" to the top.
+const environmentList = computed(() =>
+ cloneDeep(rawEnvironmentList.value).reverse()
+);
- const prepareList = () => {
- // It will also be called when user logout
- if (currentUser.value.id !== UNKNOWN_ID) {
- databaseStore.fetchDatabaseList();
- }
- };
-
- watchEffect(prepareList);
-
- // Use this to make the list reactive when project is transferred.
- const databaseList = computed((): Database[] => {
- return databaseStore
- .getDatabaseListByUser(currentUserV1.value)
- .filter((db) => db.syncStatus === "OK");
- });
+const prepareList = () => {
+ // It will also be called when user logout
+ if (currentUser.value.id !== UNKNOWN_ID) {
+ databaseStore.fetchDatabaseList();
+ }
+};
- const databaseListByEnvironment = computed(() => {
- const envToDbMap: Map<string, BBOutlineItem[]> = new Map();
- for (const environment of environmentList.value) {
- envToDbMap.set(environment.uid, []);
- }
- const list = [...databaseList.value].filter(
- (db) => db.project.tenantMode !== "TENANT"
- );
- list.sort((a: any, b: any) => {
- return a.name.localeCompare(b.name);
- });
- for (const database of list) {
- const dbList = envToDbMap.get(
- String(database.instance.environment.id)
- )!;
- // dbList may be undefined if the environment is archived
- if (dbList) {
- dbList.push({
- id: `bb.database.${database.id}`,
- name: database.name,
- link: `/db/${databaseSlug(database)}`,
- });
- }
- }
+watchEffect(prepareList);
- return environmentList.value
- .filter((environment) => {
- const items = envToDbMap.get(environment.uid) ?? [];
- return items.length > 0;
- })
- .map((environment): BBOutlineItem => {
- return {
- id: `bb.env.${environment.uid}`,
- name: environmentV1Name(environment),
- childList: envToDbMap.get(environment.uid),
- childCollapse: true,
- };
- });
- });
+// Use this to make the list reactive when project is transferred.
+const databaseList = computed((): Database[] => {
+ return databaseStore
+ .getDatabaseListByUser(currentUserV1.value)
+ .filter((db) => db.syncStatus === "OK");
+});
- const tenantDatabaseListByProject = computed((): BBOutlineItem[] => {
- const list = databaseList.value.filter(
- (db) => db.project.tenantMode === "TENANT"
- );
- // In case that each `db.project` is not reference equal
- // we run a uniq() on the list by project.id
- const projectList = uniqBy(
- list.map((db) => db.project),
- (project) => project.id
- );
- // Sort the list as what <ProjectListSidePanel /> does
- projectList.sort((a, b) => a.name.localeCompare(b.name));
- // Then group databaseList by project
- const databaseListGroupByProject = projectList.map((project) => {
- const databaseList = list.filter((db) => db.project.id === project.id);
- return {
- project,
- databaseList,
- };
+const databaseListByEnvironment = computed(() => {
+ const envToDbMap: Map<string, BBOutlineItem[]> = new Map();
+ for (const environment of environmentList.value) {
+ envToDbMap.set(environment.uid, []);
+ }
+ const list = [...databaseList.value].filter(
+ (db) => db.project.tenantMode !== "TENANT"
+ );
+ list.sort((a: any, b: any) => {
+ return a.name.localeCompare(b.name);
+ });
+ for (const database of list) {
+ const dbList = envToDbMap.get(String(database.instance.environment.id))!;
+ // dbList may be undefined if the environment is archived
+ if (dbList) {
+ dbList.push({
+ id: `bb.database.${database.id}`,
+ name: database.name,
+ link: `/db/${databaseSlug(database)}`,
});
- // Map groups to `BBOutlineItem[]`
- const itemList = databaseListGroupByProject.map(
- ({ project, databaseList }) => {
- return {
- id: `bb.project.${project.id}.databases`,
- name: project.name,
- childList: databaseList.map<BBOutlineItem>((db) => ({
- id: `bb.project.${project.id}.database.${db.name}`,
- name: db.name,
- link: `/project/${projectSlug(project)}#databases`,
- })),
- childCollapse: true,
- } as BBOutlineItem;
- }
- );
- return itemList;
- });
+ }
+ }
- const mixedDatabaseList = computed(() => {
- return [
- ...databaseListByEnvironment.value,
- ...tenantDatabaseListByProject.value,
- ];
+ return environmentList.value
+ .filter((environment) => {
+ const items = envToDbMap.get(environment.uid) ?? [];
+ return items.length > 0;
+ })
+ .map((environment): BBOutlineItem => {
+ return {
+ id: `bb.env.${environment.uid}`,
+ name: environmentV1Name(environment),
+ childList: envToDbMap.get(environment.uid),
+ childCollapse: true,
+ };
});
+});
- const kbarActions = computed((): Action[] => {
- const actions = mixedDatabaseList.value.flatMap((group: BBOutlineItem) =>
- group.childList!.map((item) =>
- defineAction({
- // `item.id` is namespaced already
- // so here `id` looks like
- // "bb.database.7001" for non-tenant databases
- // "bb.project.3007.database.db3" for tenant databases
- id: item.id,
- section: t("common.databases"),
- name: item.name,
- // `group.name` is also a keyword to provide better search
- // e.g. "blog" under "staged" now can be searched by "bl st"
- // also "blog" under "HR system" (a project) can be searched by "bl hr"
- keywords: `database db ${group.name}`,
- data: {
- tags: [group.name],
- },
- perform: () => {
- router.push(item.link!);
- },
- })
- )
- );
- return actions;
- });
- useRegisterActions(kbarActions);
-
+const tenantDatabaseListByProject = computed((): BBOutlineItem[] => {
+ const list = databaseList.value.filter(
+ (db) => db.project.tenantMode === "TENANT"
+ );
+ // In case that each `db.project` is not reference equal
+ // we run a uniq() on the list by project.id
+ const projectList = uniqBy(
+ list.map((db) => db.project),
+ (project) => project.id
+ );
+ // Sort the list as what <ProjectListSidePanel /> does
+ projectList.sort((a, b) => a.name.localeCompare(b.name));
+ // Then group databaseList by project
+ const databaseListGroupByProject = projectList.map((project) => {
+ const databaseList = list.filter((db) => db.project.id === project.id);
return {
- mixedDatabaseList,
+ project,
+ databaseList,
};
- },
+ });
+ // Map groups to `BBOutlineItem[]`
+ const itemList = databaseListGroupByProject.map(
+ ({ project, databaseList }) => {
+ return {
+ id: `bb.project.${project.id}.databases`,
+ name: project.name,
+ childList: databaseList.map<BBOutlineItem>((db) => ({
+ id: `bb.project.${project.id}.database.${db.name}`,
+ name: db.name,
+ link: `/project/${projectSlug(project)}#databases`,
+ })),
+ childCollapse: true,
+ } as BBOutlineItem;
+ }
+ );
+ return itemList;
+});
+
+const mixedDatabaseList = computed(() => {
+ return [
+ ...databaseListByEnvironment.value,
+ ...tenantDatabaseListByProject.value,
+ ];
+});
+
+const kbarActions = computed((): Action[] => {
+ const actions = mixedDatabaseList.value.flatMap((group: BBOutlineItem) =>
+ group.childList!.map((item) =>
+ defineAction({
+ // `item.id` is namespaced already
+ // so here `id` looks like
+ // "bb.database.7001" for non-tenant databases
+ // "bb.project.3007.database.db3" for tenant databases
+ id: item.id,
+ section: t("common.databases"),
+ name: item.name,
+ // `group.name` is also a keyword to provide better search
+ // e.g. "blog" under "staged" now can be searched by "bl st"
+ // also "blog" under "HR system" (a project) can be searched by "bl hr"
+ keywords: `database db ${group.name}`,
+ data: {
+ tags: [group.name],
+ },
+ perform: () => {
+ router.push(item.link!);
+ },
+ })
+ )
+ );
+ return actions;
});
+useRegisterActions(kbarActions);
</script>
diff --git a/frontend/src/components/Issue/form/GrantRequestQuerierForm.vue b/frontend/src/components/Issue/form/GrantRequestQuerierForm.vue
index 33200921e0..ccfa09b46c 100644
--- a/frontend/src/components/Issue/form/GrantRequestQuerierForm.vue
+++ b/frontend/src/components/Issue/form/GrantRequestQuerierForm.vue
@@ -15,7 +15,7 @@
/>
</div>
<div class="w-full flex flex-row justify-start items-start">
- <span class="flex w-40 items-center textlabel !leading-6">
+ <span class="flex w-40 items-center textlabel shrink-0 !leading-6">
{{ $t("common.databases") }}
<RequiredStar />
</span>
@@ -30,7 +30,7 @@
:value="true"
:label="$t('issue.grant-request.all-databases')"
/>
- <div class="flex flex-row justify-start flex-wrap">
+ <div class="flex flex-row justify-start flex-wrap gap-y-2">
<NRadio
class="!leading-6"
:value="false"
@@ -47,7 +47,7 @@
</button>
<div
v-if="selectedDatabaseList.length > 0"
- class="ml-6 flex flex-row justify-start items-start gap-4"
+ class="ml-6 flex flex-row justify-start items-start flex-wrap gap-2 gap-x-4"
>
<div
v-for="database in selectedDatabaseList"
diff --git a/frontend/src/components/ProjectListSidePanel.vue b/frontend/src/components/ProjectListSidePanel.vue
index 61898c52d1..0d26512074 100644
--- a/frontend/src/components/ProjectListSidePanel.vue
+++ b/frontend/src/components/ProjectListSidePanel.vue
@@ -13,16 +13,23 @@ import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import { Action, defineAction, useRegisterActions } from "@bytebase/vue-kbar";
-import { projectV1Slug } from "../utils";
+import { isMemberOfProjectV1, projectV1Slug } from "../utils";
import { BBOutlineItem } from "../bbkit/types";
-import { useProjectV1ListByCurrentUser } from "@/store";
+import { useCurrentUserV1, useProjectV1ListByCurrentUser } from "@/store";
+import { DEFAULT_PROJECT_ID } from "@/types";
const { t } = useI18n();
const router = useRouter();
+const currentUserV1 = useCurrentUserV1();
const { projectList } = useProjectV1ListByCurrentUser();
const outlineItemList = computed((): BBOutlineItem[] => {
return projectList.value
+ .filter(
+ (project) =>
+ project.uid != String(DEFAULT_PROJECT_ID) &&
+ isMemberOfProjectV1(project.iamPolicy, currentUserV1.value)
+ )
.map((project): BBOutlineItem => {
return {
id: project.uid,
diff --git a/frontend/src/components/ProjectSelect.vue b/frontend/src/components/ProjectSelect.vue
index 6bbf5d3921..3efca306c0 100644
--- a/frontend/src/components/ProjectSelect.vue
+++ b/frontend/src/components/ProjectSelect.vue
@@ -18,7 +18,11 @@
import { computed, defineComponent, PropType, reactive, watch } from "vue";
import { UNKNOWN_ID, DEFAULT_PROJECT_ID, unknownProject } from "../types";
import { useCurrentUserV1, useProjectV1Store } from "@/store";
-import { isMemberOfProjectV1, projectV1Name } from "@/utils";
+import {
+ hasWorkspacePermissionV1,
+ isMemberOfProjectV1,
+ projectV1Name,
+} from "@/utils";
import { Project, TenantMode } from "@/types/proto/v1/project_service";
import { State } from "@/types/proto/v1/common";
@@ -71,13 +75,20 @@ export default defineComponent({
const currentUserV1 = useCurrentUserV1();
const projectV1Store = useProjectV1Store();
+ const canManageProject = hasWorkspacePermissionV1(
+ "bb.permission.workspace.manage-project",
+ currentUserV1.value.userRole
+ );
const rawProjectList = computed((): Project[] => {
let list = projectV1Store.getProjectList(true /* showDeleted */);
if (props.onlyUserself) {
list = list.filter((project) => {
- return isMemberOfProjectV1(project.iamPolicy, currentUserV1.value);
+ return (
+ canManageProject ||
+ isMemberOfProjectV1(project.iamPolicy, currentUserV1.value)
+ );
});
}
diff --git a/frontend/src/store/modules/database.ts b/frontend/src/store/modules/database.ts
index 510c521229..bedf08531f 100644
--- a/frontend/src/store/modules/database.ts
+++ b/frontend/src/store/modules/database.ts
@@ -23,7 +23,7 @@ import {
import { useDataSourceStore } from "./dataSource";
import { useInstanceStore } from "./instance";
import { useLegacyProjectStore } from "./project";
-import { isMemberOfProjectV1 } from "@/utils";
+import { hasWorkspacePermissionV1, isMemberOfProjectV1 } from "@/utils";
import { useProjectV1Store } from "./v1";
import { User } from "@/types/proto/v1/auth_service";
@@ -172,13 +172,20 @@ export const useDatabaseStore = defineStore("database", {
return this.databaseListByInstanceId.get(instanceId) || [];
},
getDatabaseListByUser(user: User): Database[] {
+ const canManageDatabase = hasWorkspacePermissionV1(
+ "bb.permission.workspace.manage-database",
+ user.userRole
+ );
const list: Database[] = [];
for (const [_, databaseList] of this.databaseListByInstanceId) {
databaseList.forEach((item: Database) => {
const projectV1 = useProjectV1Store().getProjectByUID(
String(item.project.id)
);
- if (isMemberOfProjectV1(projectV1.iamPolicy, user)) {
+ if (
+ canManageDatabase ||
+ isMemberOfProjectV1(projectV1.iamPolicy, user)
+ ) {
list.push(item);
}
});
diff --git a/frontend/src/store/modules/project.ts b/frontend/src/store/modules/project.ts
index e9e7101f61..8f07b3521c 100644
--- a/frontend/src/store/modules/project.ts
+++ b/frontend/src/store/modules/project.ts
@@ -16,7 +16,6 @@ import {
UNKNOWN_ID,
} from "@/types";
import { getPrincipalFromIncludedList } from "./principal";
-import { isMemberOfProject } from "@/utils";
function convert(
project: ResourceObject,
@@ -95,25 +94,6 @@ export const useLegacyProjectStore = defineStore("project_legacy", {
return convert(instance, includedList || []);
},
- getProjectListByUser(
- userId: PrincipalId,
- rowStatusList?: RowStatus[]
- ): Project[] {
- const result: Project[] = [];
- for (const [_, project] of this.projectById) {
- if (
- (!rowStatusList && project.rowStatus == "NORMAL") ||
- (rowStatusList && rowStatusList.includes(project.rowStatus))
- ) {
- if (isMemberOfProject(project, userId)) {
- result.push(project);
- }
- }
- }
-
- return result;
- },
-
getProjectById(projectId: ProjectId): Project {
if (projectId == EMPTY_ID) {
return empty("PROJECT") as Project;
diff --git a/frontend/src/store/modules/v1/project.ts b/frontend/src/store/modules/v1/project.ts
index 8525c92ef9..7863d12845 100644
--- a/frontend/src/store/modules/v1/project.ts
+++ b/frontend/src/store/modules/v1/project.ts
@@ -16,6 +16,7 @@ import { useProjectIamPolicyStore } from "./projectIamPolicy";
import { State } from "@/types/proto/v1/common";
import { User } from "@/types/proto/v1/auth_service";
import { useCurrentUserV1 } from "../auth";
+import { hasWorkspacePermissionV1 } from "@/utils";
export const useProjectV1Store = defineStore("project_v1", () => {
const projectMapByName = reactive(new Map<ResourceId, ComposedProject>());
@@ -48,7 +49,16 @@ export const useProjectV1Store = defineStore("project_v1", () => {
);
};
const getProjectListByUser = (user: User, showDeleted = false) => {
- return getProjectList(showDeleted).filter((project) => {
+ const canManageProject = hasWorkspacePermissionV1(
+ "bb.permission.workspace.manage-project",
+ user.userRole
+ );
+ const projectList = getProjectList(showDeleted);
+ if (canManageProject) {
+ return projectList;
+ }
+
+ return projectList.filter((project) => {
return project.iamPolicy.bindings.some((binding) => {
return binding.members.some((email) => {
return email === `user:${unref(user).email}`;
diff --git a/frontend/src/store/modules/v1/projectIamPolicy.ts b/frontend/src/store/modules/v1/projectIamPolicy.ts
index 699bdfeebd..f9486e983c 100644
--- a/frontend/src/store/modules/v1/projectIamPolicy.ts
+++ b/frontend/src/store/modules/v1/projectIamPolicy.ts
@@ -7,7 +7,7 @@ import { Database, MaybeRef, PresetRoleType } from "@/types";
import { useLegacyProjectStore } from "../project";
import { useProjectV1Store } from "./project";
import { useCurrentUserV1 } from "../auth";
-import { hasWorkspacePermissionV1 } from "@/utils";
+import { hasWorkspacePermissionV1, isMemberOfProjectV1 } from "@/utils";
export const useProjectIamPolicyStore = defineStore(
"project-iam-policy",
@@ -145,16 +145,7 @@ export const useCurrentUserIamPolicy = () => {
if (!policy) {
return false;
}
- for (const binding of policy.bindings) {
- if (
- binding.members.find(
- (member) => member === `user:${currentUser.value.email}`
- )
- ) {
- return true;
- }
- }
- return false;
+ return isMemberOfProjectV1(policy, currentUser.value);
};
const allowToChangeDatabaseOfProject = (projectName: string) => {
diff --git a/frontend/src/utils/project.ts b/frontend/src/utils/project.ts
index cbdac36978..a2ae15e810 100644
--- a/frontend/src/utils/project.ts
+++ b/frontend/src/utils/project.ts
@@ -1,9 +1,4 @@
-import {
- DEFAULT_PROJECT_ID,
- Principal,
- PrincipalId,
- type Project,
-} from "../types";
+import { DEFAULT_PROJECT_ID, type Project } from "../types";
export function projectName(project: Project) {
if (project.id === DEFAULT_PROJECT_ID) {
@@ -16,13 +11,3 @@ export function projectName(project: Project) {
}
return name;
}
-
-export const isMemberOfProject = (
- project: Project,
- userOrId: Principal | PrincipalId
-) => {
- const id = typeof userOrId === "object" ? userOrId.id : userOrId;
- return (
- project.memberList.findIndex((member) => member.principal.id === id) >= 0
- );
-};
diff --git a/frontend/src/views/DatabaseDashboard.vue b/frontend/src/views/DatabaseDashboard.vue
index decb75fe4c..16b2896a7c 100644
--- a/frontend/src/views/DatabaseDashboard.vue
+++ b/frontend/src/views/DatabaseDashboard.vue
@@ -61,7 +61,6 @@
import { computed, watchEffect, onMounted, reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
import { NInputGroup, NTooltip } from "naive-ui";
-import { cloneDeep } from "lodash-es";
import {
EnvironmentTabFilter,
@@ -82,9 +81,9 @@ import {
} from "../utils";
import {
useCurrentUser,
+ useCurrentUserV1,
useDatabaseStore,
useEnvironmentV1Store,
- useProjectV1ListByCurrentUser,
useUIStateStore,
} from "@/store";
@@ -108,7 +107,8 @@ const state = reactive<LocalState>({
});
const currentUser = useCurrentUser();
-const { projectList } = useProjectV1ListByCurrentUser();
+const currentUserV1 = useCurrentUserV1();
+const databaseStore = useDatabaseStore();
const selectedEnvironment = computed(() => {
const { environment } = route.query;
@@ -133,24 +133,19 @@ onMounted(() => {
}
});
-const prepareDatabaseList = () => {
+const prepareDatabaseList = async () => {
// It will also be called when user logout
if (currentUser.value.id != UNKNOWN_ID) {
- const projectIdList = projectList.value.map((project) => project.uid);
state.loading = true;
- useDatabaseStore()
- .fetchDatabaseList()
- .then((list) => {
- state.databaseList = sortDatabaseListByEnvironmentV1(
- cloneDeep(list).filter((db) =>
- projectIdList.includes(String(db.projectId))
- ),
- environmentV1Store.getEnvironmentList()
- );
- })
- .finally(() => {
- state.loading = false;
- });
+ await databaseStore.fetchDatabaseList();
+ const databaseList = databaseStore.getDatabaseListByUser(
+ currentUserV1.value
+ );
+ state.databaseList = sortDatabaseListByEnvironmentV1(
+ databaseList,
+ environmentV1Store.getEnvironmentList()
+ );
+ state.loading = false;
}
};
diff --git a/frontend/src/views/ProjectDashboard.vue b/frontend/src/views/ProjectDashboard.vue
index 2e2aa19eed..6f3bce6868 100644
--- a/frontend/src/views/ProjectDashboard.vue
+++ b/frontend/src/views/ProjectDashboard.vue
@@ -16,8 +16,12 @@
<script lang="ts" setup>
import { computed, onMounted, reactive } from "vue";
-import { useUIStateStore, useProjectV1ListByCurrentUser } from "@/store";
-import { filterProjectV1ListByKeyword } from "@/utils";
+import {
+ useUIStateStore,
+ useProjectV1ListByCurrentUser,
+ useCurrentUserV1,
+} from "@/store";
+import { filterProjectV1ListByKeyword, isMemberOfProjectV1 } from "@/utils";
import { DEFAULT_PROJECT_ID } from "@/types";
import { SearchBox, ProjectV1Table } from "@/components/v2";
@@ -28,6 +32,7 @@ interface LocalState {
const state = reactive<LocalState>({
searchText: "",
});
+const currentUserV1 = useCurrentUserV1();
const { projectList } = useProjectV1ListByCurrentUser();
const changeSearchText = (searchText: string) => {
@@ -36,7 +41,9 @@ const changeSearchText = (searchText: string) => {
const filteredProjectList = computed(() => {
const list = projectList.value.filter(
- (project) => project.uid != String(DEFAULT_PROJECT_ID)
+ (project) =>
+ project.uid != String(DEFAULT_PROJECT_ID) &&
+ isMemberOfProjectV1(project.iamPolicy, currentUserV1.value)
);
return filterProjectV1ListByKeyword(list, state.searchText);
});