Skip to content

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

Cleanup RoleMapping module

天舟07f7eeaa733b
diff --git a/frontend/src/components/MemberInvite.vue b/frontend/src/components/MemberInvite.vue
index 52fe946b7c..de0f63af53 100644
--- a/frontend/src/components/MemberInvite.vue
+++ b/frontend/src/components/MemberInvite.vue
@@ -119,11 +119,16 @@
import { computed, reactive } from "vue";
import { useStore } from "vuex";
import RoleSelect from "../components/RoleSelect.vue";
-import { UNKNOWN_ID, Principal, RoleMappingNew } from "../types";
+import { Principal, RoleMappingNew, RoleType } from "../types";
import { isValidEmail } from "../utils";
+type Invite = {
+ email: string;
+ role: RoleType;
+};
+
interface LocalState {
- inviteList: RoleMappingNew[];
+ inviteList: Invite[];
errorList: string[];
}
@@ -147,15 +152,13 @@ export default {
for (let i = 0; i < 3; i++) {
state.inviteList.push({
- principalId: UNKNOWN_ID,
email: "",
role: "DEVELOPER",
- updaterId: currentUser.value.id,
});
state.errorList.push("");
}
- const validateInviteInternal = (invite: RoleMappingNew): string => {
+ const validateInviteInternal = (invite: Invite): string => {
if (invite.email) {
if (!isValidEmail(invite.email)) {
return "Invalid email address";
@@ -171,7 +174,7 @@ export default {
return "";
};
- const validateInvite = (invite: RoleMappingNew, index: number): boolean => {
+ const validateInvite = (invite: Invite, index: number): boolean => {
state.errorList[index] = validateInviteInternal(invite);
return state.errorList[index].length == 0;
};
@@ -182,10 +185,8 @@ export default {
const addInvite = () => {
state.inviteList.push({
- principalId: UNKNOWN_ID,
email: "",
role: "DEVELOPER",
- updaterId: currentUser.value.id,
});
state.errorList.push("");
};
@@ -212,30 +213,27 @@ export default {
if (!hasAdminFeature.value) {
invite.role = "OWNER";
}
- // We created a new principal for that email if not exists.
// Note "principal/createPrincipal" would return the existing principal.
// This could happen if another client has just created the principal
// with this email.
- if (invite.principalId == UNKNOWN_ID) {
- invite.principalId = store.getters["principal/principalByEmail"](
- invite.email
- ).id;
- }
- if (invite.principalId != UNKNOWN_ID) {
- store.dispatch("roleMapping/createdRoleMapping", invite);
- } else {
- store
- .dispatch("principal/createPrincipal", {
- email: invite.email,
- })
- .then((principal: Principal) => {
- invite.principalId = principal.id;
- store.dispatch("roleMapping/createdRoleMapping", invite);
- })
- .catch((error) => {
- console.error(error);
- });
- }
+ store
+ .dispatch("principal/createPrincipal", {
+ email: invite.email,
+ })
+ .then((principal: Principal) => {
+ const newRoleMapping: RoleMappingNew = {
+ principalId: principal.id,
+ role: invite.role,
+ updaterId: currentUser.value.id,
+ };
+ // Note "principal/createdRoleMapping" would return the existing role mapping.
+ // This could happen if another client has just created the role mapping with
+ // this principal.
+ store.dispatch("roleMapping/createdRoleMapping", newRoleMapping);
+ })
+ .catch((error) => {
+ console.error(error);
+ });
store.dispatch("uistate/saveIntroStateByKey", {
key: "member.invite",
@@ -245,10 +243,8 @@ export default {
}
state.inviteList = [
{
- principalId: UNKNOWN_ID,
email: "",
role: "DEVELOPER",
- updaterId: currentUser.value.id,
},
];
state.errorList = [""];
diff --git a/frontend/src/miragejs/routes/roleMapping.js b/frontend/src/miragejs/routes/roleMapping.js
index de8ab4b43a..cbaef80981 100644
--- a/frontend/src/miragejs/routes/roleMapping.js
+++ b/frontend/src/miragejs/routes/roleMapping.js
@@ -13,6 +13,13 @@ export default function configureRoleMapping(route) {
...this.normalizedRequestAttrs("role-mapping"),
workspaceId: WORKSPACE_ID,
};
+ const roleMapping = schema.roleMappings.findBy({
+ principalId: attrs.principalId,
+ workspaceId: WORKSPACE_ID,
+ });
+ if (roleMapping) {
+ return roleMapping;
+ }
const newRoleMapping = {
...attrs,
createdTs: ts,
diff --git a/frontend/src/store/modules/roleMapping.ts b/frontend/src/store/modules/roleMapping.ts
index 901cee86bf..2c495f093f 100644
--- a/frontend/src/store/modules/roleMapping.ts
+++ b/frontend/src/store/modules/roleMapping.ts
@@ -7,6 +7,7 @@ import {
RoleMappingState,
ResourceObject,
PrincipalId,
+ unknown,
} from "../../types";
function convert(roleMapping: ResourceObject): RoleMapping {
@@ -27,16 +28,9 @@ const getters = {
roleMappingByPrincipalId: (state: RoleMappingState) => (
id: PrincipalId
): 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",
- }
+ state.roleMappingList.find((item) => item.principalId == id) ||
+ (unknown("ROLE_MAPPING") as RoleMapping)
);
},
};
@@ -53,6 +47,7 @@ const actions = {
return roleMappingList;
},
+ // Returns existing roleMapping if the principalId has already been created.
async createdRoleMapping({ commit }: any, newRoleMapping: RoleMappingNew) {
const createdRoleMapping = convert(
(
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 19364bc73f..6f3df2c5c0 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -8,6 +8,7 @@ export type ResourceType =
| "PRINCIPAL"
| "EXECUTION"
| "USER"
+ | "ROLE_MAPPING"
| "ENVIRONMENT"
| "INSTANCE"
| "DATABASE"
@@ -23,6 +24,7 @@ export const unknown = (
| Execution
| Principal
| User
+ | RoleMapping
| Environment
| Instance
| Database
@@ -49,6 +51,15 @@ export const unknown = (
};
+ const UNKNOWN_ROLE_MAPPING: RoleMapping = {
+ id: UNKNOWN_ID,
+ createdTs: 0,
+ lastUpdatedTs: 0,
+ role: "GUEST",
+ principalId: UNKNOWN_ID,
+ updaterId: UNKNOWN_ID,
+ };
+
const UNKNOWN_ENVIRONMENT: Environment = {
id: UNKNOWN_ID,
name: "<<Unknown environment>>",
@@ -126,6 +137,8 @@ export const unknown = (
return UNKNOWN_PRINCIPAL;
case "USER":
return UNKNOWN_USER;
+ case "ROLE_MAPPING":
+ return UNKNOWN_ROLE_MAPPING;
case "ENVIRONMENT":
return UNKNOWN_ENVIRONMENT;
case "INSTANCE":
@@ -202,7 +215,6 @@ export type RoleMapping = {
export type RoleMappingNew = {
principalId: PrincipalId;
- email: string;
role: RoleType;
updaterId: PrincipalId;
};