Skip to content

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

Add role to the Principal object

天舟fd109c89100e
diff --git a/frontend/src/components/MemberInvite.vue b/frontend/src/components/MemberInvite.vue
index dd31d0b312..dda78f025b 100644
--- a/frontend/src/components/MemberInvite.vue
+++ b/frontend/src/components/MemberInvite.vue
@@ -152,10 +152,16 @@ export default {
if (invite.email) {
if (!isValidEmail(invite.email)) {
return "Invalid email address";
- } else if (
- store.getters["roleMapping/roleMappingByEmail"](invite.email)
- ) {
- return "Already a member";
+ } else {
+ const principal = store.getters["principal/principalByEmail"](
+ invite.email
+ );
+ if (
+ principal &&
+ store.getters["roleMapping/roleMappingByPrincipalId"](principal.id)
+ ) {
+ return "Already a member";
+ }
}
}
return "";
diff --git a/frontend/src/components/MemberTable.vue b/frontend/src/components/MemberTable.vue
index 67f76c6ea3..f5bd308ee1 100644
--- a/frontend/src/components/MemberTable.vue
+++ b/frontend/src/components/MemberTable.vue
@@ -20,37 +20,37 @@
:title="columnList[3].title"
/>
</template>
- <template v-slot:body="{ rowData: roleMapping }">
+ <template v-slot:body="{ rowData: roleMappingUI }">
<BBTableCell :leftPadding="4" class="table-cell">
<div class="flex flex-row items-center space-x-2">
- <template v-if="'INVITED' == roleMapping.principal.status">
+ <template v-if="'INVITED' == roleMappingUI.principal.status">
<span
class="inline-flex items-center px-2 py-0.5 rounded-lg text-xs font-semibold bg-main text-main-text"
>
Invited
</span>
<span class="textlabel">
- {{ roleMapping.principal.email }}
+ {{ roleMappingUI.principal.email }}
</span>
</template>
<template v-else>
- <BBAvatar :username="roleMapping.principal.name" />
+ <BBAvatar :username="roleMappingUI.principal.name" />
<div class="flex flex-col">
<div class="flex flex-row items-center space-x-2">
<router-link
- :to="`/u/${roleMapping.principal.id}`"
+ :to="`/u/${roleMappingUI.principal.id}`"
class="normal-link"
- >{{ roleMapping.principal.name }}
+ >{{ roleMappingUI.principal.name }}
</router-link>
<span
- v-if="currentUser.id == roleMapping.principal.id"
+ v-if="currentUser.id == roleMappingUI.principal.id"
class="inline-flex items-center px-2 py-0.5 rounded-lg text-xs font-semibold bg-green-100 text-green-800"
>
You
</span>
</div>
<span class="textlabel">
- {{ roleMapping.principal.email }}
+ {{ roleMappingUI.principal.email }}
</span>
</div>
</template>
@@ -58,11 +58,11 @@
</BBTableCell>
<BBTableCell class="">
<RoleSelect
- :selectedRole="roleMapping.role"
+ :selectedRole="roleMappingUI.role"
:disabled="!allowEdit"
@change-role="
(role) => {
- changeRole(roleMapping.id, role);
+ changeRole(roleMappingUI.id, role);
}
"
/>
@@ -70,11 +70,13 @@
<BBTableCell class="table-cell">
<div class="flex flex-row items-center space-x-1">
<span>
- {{ humanizeTs(roleMapping.lastUpdatedTs) }}
+ {{ humanizeTs(roleMappingUI.lastUpdatedTs) }}
</span>
<span>by</span>
- <router-link :to="`/u/${roleMapping.updater.id}`" class="normal-link"
- >{{ roleMapping.updater.name }}
+ <router-link
+ :to="`/u/${roleMappingUI.updater.id}`"
+ class="normal-link"
+ >{{ roleMappingUI.updater.name }}
</router-link>
</div>
</BBTableCell>
@@ -83,8 +85,8 @@
v-if="allowEdit"
:requireConfirm="true"
:okText="'Revoke'"
- :confirmTitle="`Are you sure to revoke '${roleMapping.role}' from '${roleMapping.principal.name}'`"
- @confirm="deleteRole(roleMapping.id)"
+ :confirmTitle="`Are you sure to revoke '${roleMappingUI.role}' from '${roleMappingUI.principal.name}'`"
+ @confirm="deleteRole(roleMappingUI.id)"
/>
</BBTableCell>
</template>
@@ -95,9 +97,14 @@
import { computed, reactive, watchEffect } from "vue";
import { useStore } from "vuex";
import RoleSelect from "../components/RoleSelect.vue";
-import { RoleMapping, RoleMappingId, RoleType } from "../types";
+import { Principal, RoleMapping, RoleMappingId, RoleType } from "../types";
import { BBTableColumn } from "../bbkit/types";
+type RoleMappingUI = RoleMapping & {
+ principal: Principal;
+ updater: Principal;
+};
+
const columnList: BBTableColumn[] = [
{
title: "Account",
@@ -135,18 +142,27 @@ export default {
watchEffect(prepareRoleMappingList);
const dataSource = computed(() => {
- const ownerList = [];
- const dbaList = [];
- const developerList = [];
+ const ownerList: RoleMappingUI[] = [];
+ const dbaList: RoleMappingUI[] = [];
+ const developerList: RoleMappingUI[] = [];
for (const roleMapping of store.getters[
"roleMapping/roleMappingList"
]()) {
- if (roleMapping.role === "OWNER") {
- ownerList.push(roleMapping);
- } else if (roleMapping.role === "DBA") {
- dbaList.push(roleMapping);
- } else if (roleMapping.role === "DEVELOPER") {
- developerList.push(roleMapping);
+ const roleMappingUI = {
+ ...roleMapping,
+ principal: store.getters["principal/principalById"](
+ roleMapping.principalId
+ ),
+ updater: store.getters["principal/principalById"](
+ roleMapping.updaterId
+ ),
+ };
+ if (roleMappingUI.role === "OWNER") {
+ ownerList.push(roleMappingUI);
+ } else if (roleMappingUI.role === "DBA") {
+ dbaList.push(roleMappingUI);
+ } else if (roleMappingUI.role === "DEVELOPER") {
+ developerList.push(roleMappingUI);
}
}
const dataSource = [];
diff --git a/frontend/src/components/PrincipalSelect.vue b/frontend/src/components/PrincipalSelect.vue
index 0d9e3d53b8..ff5db164e9 100644
--- a/frontend/src/components/PrincipalSelect.vue
+++ b/frontend/src/components/PrincipalSelect.vue
@@ -55,7 +55,9 @@ export default {
});
}
return list.map((roleMapping: RoleMapping) => {
- return roleMapping.principal;
+ return store.getters["principal/principalById"](
+ roleMapping.principalId
+ );
});
});
diff --git a/frontend/src/components/ProvideContext.vue b/frontend/src/components/ProvideContext.vue
index 8cc3a32ba6..4e9b9b0f8a 100644
--- a/frontend/src/components/ProvideContext.vue
+++ b/frontend/src/components/ProvideContext.vue
@@ -16,8 +16,9 @@ export default {
// context here.
await Promise.all([
store.dispatch("environment/fetchEnvironmentList"),
- store.dispatch("principal/fetchPrincipalList"),
store.dispatch("roleMapping/fetchRoleMappingList"),
+ // fetchPrincipalList relies on fetchRoleMappingList to find the role of the principal.
+ store.dispatch("principal/fetchPrincipalList"),
store.dispatch("instance/fetchInstanceList"),
store.dispatch("uistate/restoreState"),
]);
diff --git a/frontend/src/store/modules/principal.ts b/frontend/src/store/modules/principal.ts
index e6ca40f461..14ee9683b6 100644
--- a/frontend/src/store/modules/principal.ts
+++ b/frontend/src/store/modules/principal.ts
@@ -11,12 +11,16 @@ import {
} from "../../types";
import { isDevOrDemo, randomString } from "../../utils";
-function convert(user: ResourceObject): Principal {
+function convert(user: ResourceObject, rootGetters: any): Principal {
+ const roleMapping = rootGetters["roleMapping/roleMappingByPrincipalId"](
+ user.id
+ );
return {
id: user.id,
status: user.attributes.status as PrincipalStatus,
name: user.attributes.name as string,
email: user.attributes.email as string,
+ role: roleMapping.role,
};
}
@@ -36,6 +40,7 @@ const getters = {
status: "UNKNOWN",
name: "<<Email Missing>>",
email: "",
+ role: "GUEST",
};
}
@@ -49,6 +54,7 @@ const getters = {
status: "UNKNOWN",
name: `<<Email ${email} not found>>`,
email: "",
+ role: "GUEST",
};
},
@@ -61,6 +67,7 @@ const getters = {
status: "UNKNOWN",
name: "<<ID Missing>>",
email: "",
+ role: "GUEST",
};
}
@@ -76,25 +83,30 @@ const getters = {
status: "UNKNOWN",
name: `<<ID ${principalId} not found>>`,
email: "",
+ role: "GUEST",
};
},
};
const actions = {
- async fetchPrincipalList({ commit }: any) {
+ async fetchPrincipalList({ commit, rootGetters }: any) {
const userList: ResourceObject[] = (await axios.get(`/api/user`)).data.data;
const principalList = userList.map((user) => {
- return convert(user);
+ return convert(user, rootGetters);
});
commit("setPrincipalList", principalList);
return userList;
},
- async fetchPrincipalById({ commit }: any, principalId: PrincipalId) {
+ async fetchPrincipalById(
+ { commit, rootGetters }: any,
+ principalId: PrincipalId
+ ) {
const principal = convert(
- (await axios.get(`/api/user/${principalId}`)).data.data
+ (await axios.get(`/api/user/${principalId}`)).data.data,
+ rootGetters
);
commit("replacePrincipalInList", principal);
@@ -103,7 +115,10 @@ const actions = {
},
// Returns existing user if already created.
- async createPrincipal({ commit }: any, newPrincipal: PrincipalNew) {
+ async createPrincipal(
+ { commit, rootGetters }: any,
+ newPrincipal: PrincipalNew
+ ) {
const createdPrincipal = convert(
(
await axios.post(`/api/user`, {
@@ -116,7 +131,8 @@ const actions = {
},
},
})
- ).data.data
+ ).data.data,
+ rootGetters
);
commit("appendPrincipal", createdPrincipal);
@@ -124,7 +140,10 @@ const actions = {
return createdPrincipal;
},
- async patchPrincipal({ commit }: any, principal: PrincipalPatch) {
+ async patchPrincipal(
+ { commit, rootGetters }: any,
+ principal: PrincipalPatch
+ ) {
const { id, ...attrs } = principal;
const updatedPrincipal = convert(
(
@@ -134,7 +153,8 @@ const actions = {
attributes: attrs,
},
})
- ).data.data
+ ).data.data,
+ rootGetters
);
commit("replacePrincipalInList", updatedPrincipal);
diff --git a/frontend/src/store/modules/roleMapping.ts b/frontend/src/store/modules/roleMapping.ts
index 3eebb868cc..901cee86bf 100644
--- a/frontend/src/store/modules/roleMapping.ts
+++ b/frontend/src/store/modules/roleMapping.ts
@@ -9,22 +9,10 @@ import {
PrincipalId,
} from "../../types";
-function convert(roleMapping: ResourceObject, rootGetters: any): RoleMapping {
- const principal = rootGetters["principal/principalById"](
- roleMapping.attributes.principalId
- );
- const updater = rootGetters["principal/principalById"](
- roleMapping.attributes.updaterId
- );
-
+function convert(roleMapping: ResourceObject): RoleMapping {
return {
id: roleMapping.id,
- principal,
- updater,
- ...(roleMapping.attributes as Omit<
- RoleMapping,
- "id" | "principal" | "updater"
- >),
+ ...(roleMapping.attributes as Omit<RoleMapping, "id">),
};
}
@@ -33,26 +21,31 @@ const state: () => RoleMappingState = () => ({
});
const getters = {
- roleMappingList: (state: RoleMappingState) => () => {
+ roleMappingList: (state: RoleMappingState) => (): RoleMapping[] => {
return state.roleMappingList;
},
roleMappingByPrincipalId: (state: RoleMappingState) => (
id: PrincipalId
- ): RoleMapping | undefined => {
- return state.roleMappingList.find((item) => item.principal.id == id);
- },
- roleMappingByEmail: (state: RoleMappingState) => (
- email: string
- ): RoleMapping | undefined => {
- return state.roleMappingList.find((item) => item.principal.email == email);
+ ): RoleMapping => {
+ const ts = Date.now();
+ return (
+ state.roleMappingList.find((item) => item.principalId == id) || {
+ id: "-1",
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ role: "GUEST",
+ principalId: "-1",
+ updaterId: "-1",
+ }
+ );
},
};
const actions = {
- async fetchRoleMappingList({ commit, rootGetters }: any) {
+ async fetchRoleMappingList({ commit }: any) {
const roleMappingList = (await axios.get(`/api/rolemapping`)).data.data.map(
(roleMapping: ResourceObject) => {
- return convert(roleMapping, rootGetters);
+ return convert(roleMapping);
}
);
@@ -60,10 +53,7 @@ const actions = {
return roleMappingList;
},
- async createdRoleMapping(
- { commit, rootGetters }: any,
- newRoleMapping: RoleMappingNew
- ) {
+ async createdRoleMapping({ commit }: any, newRoleMapping: RoleMappingNew) {
const createdRoleMapping = convert(
(
await axios.post(`/api/rolemapping`, {
@@ -72,8 +62,7 @@ const actions = {
attributes: newRoleMapping,
},
})
- ).data.data,
- rootGetters
+ ).data.data
);
commit("appendRoleMapping", createdRoleMapping);
@@ -81,10 +70,7 @@ const actions = {
return createdRoleMapping;
},
- async patchRoleMapping(
- { commit, rootGetters }: any,
- roleMapping: RoleMappingPatch
- ) {
+ async patchRoleMapping({ commit }: any, roleMapping: RoleMappingPatch) {
const { id, ...attrs } = roleMapping;
const updatedRoleMapping = convert(
(
@@ -94,8 +80,7 @@ const actions = {
attributes: attrs,
},
})
- ).data.data,
- rootGetters
+ ).data.data
);
commit("replaceRoleMappingInList", updatedRoleMapping);
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 5524412663..8dfd4e8cdd 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -50,35 +50,16 @@ export type User = {
};
export type NewUser = Omit<User, "id">;
-// Principal
-export type PrincipalStatus = "UNKNOWN" | "INVITED" | "ACTIVE";
-
-export type Principal = {
- id: PrincipalId;
- status: PrincipalStatus;
- name: string;
- email: string;
-};
-
-export type PrincipalNew = {
- email: string;
-};
-
-export type PrincipalPatch = {
- id: PrincipalId;
- name?: string;
-};
-
// RoleMapping
-export type RoleType = "OWNER" | "DBA" | "DEVELOPER";
+export type RoleType = "OWNER" | "DBA" | "DEVELOPER" | "GUEST";
export type RoleMapping = {
id: RoleMappingId;
createdTs: number;
lastUpdatedTs: number;
role: RoleType;
- principal: Principal;
- updater: Principal;
+ principalId: PrincipalId;
+ updaterId: PrincipalId;
};
export type RoleMappingNew = {
@@ -93,6 +74,26 @@ export type RoleMappingPatch = {
role: RoleType;
};
+// Principal
+export type PrincipalStatus = "UNKNOWN" | "INVITED" | "ACTIVE";
+
+export type Principal = {
+ id: PrincipalId;
+ status: PrincipalStatus;
+ name: string;
+ email: string;
+ role: RoleType;
+};
+
+export type PrincipalNew = {
+ email: string;
+};
+
+export type PrincipalPatch = {
+ id: PrincipalId;
+ name?: string;
+};
+
// Bookmark
export type Bookmark = {
id: BookmarkId;