Skip to content

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

Create a new principal (user) during invite if no user exists

Tianzhou Chend44d2992ecae
diff --git a/frontend/src/components/MemberInvite.vue b/frontend/src/components/MemberInvite.vue
index 1c70686d69..4a019b9cfc 100644
--- a/frontend/src/components/MemberInvite.vue
+++ b/frontend/src/components/MemberInvite.vue
@@ -116,7 +116,7 @@
import { computed, reactive } from "vue";
import { useStore } from "vuex";
import RoleSelect from "../components/RoleSelect.vue";
-import { RoleMappingNew } from "../types";
+import { UNKNOWN_ID, Principal, RoleMappingNew } from "../types";
import { isValidEmail } from "../utils";
interface LocalState {
@@ -140,6 +140,7 @@ export default {
for (let i = 0; i < 3; i++) {
state.inviteList.push({
+ principalId: UNKNOWN_ID,
email: "",
role: "DEVELOPER",
updaterId: currentUser.value.id,
@@ -169,6 +170,7 @@ export default {
const addInvite = () => {
state.inviteList.push({
+ principalId: UNKNOWN_ID,
email: "",
role: "DEVELOPER",
updaterId: currentUser.value.id,
@@ -195,11 +197,35 @@ export default {
const sendInvite = () => {
for (const invite of state.inviteList) {
if (isValidEmail(invite.email)) {
- store.dispatch("roleMapping/createdRoleMapping", invite);
+ // 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);
+ });
+ }
}
}
state.inviteList = [
{
+ principalId: UNKNOWN_ID,
email: "",
role: "DEVELOPER",
updaterId: currentUser.value.id,
diff --git a/frontend/src/components/MemberTable.vue b/frontend/src/components/MemberTable.vue
index 35286ddbcc..5076c2db62 100644
--- a/frontend/src/components/MemberTable.vue
+++ b/frontend/src/components/MemberTable.vue
@@ -21,31 +21,37 @@
<template v-slot:body="{ rowData: roleMapping }">
<BBTableCell :leftPadding="4" class="table-cell">
<div class="flex flex-row items-center space-x-2">
- <BBAvatar :username="roleMapping.principal.name" />
- <div class="flex flex-col">
- <div class="flex flex-row items-center space-x-2">
- <router-link
- :to="`/u/${roleMapping.principal.id}`"
- class="normal-link"
- >{{ roleMapping.principal.name }}
- </router-link>
- <span
- v-if="currentUser.id == roleMapping.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>
- <span
- v-if="'INVITED' == roleMapping.principal.status"
- class="inline-flex items-center px-2 py-0.5 rounded-lg text-xs font-semibold bg-main text-main-text"
- >
- Invited
- </span>
- </div>
+ <template v-if="'INVITED' == roleMapping.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 }}
</span>
- </div>
+ </template>
+ <template v-else>
+ <BBAvatar :username="roleMapping.principal.name" />
+ <div class="flex flex-col">
+ <div class="flex flex-row items-center space-x-2">
+ <router-link
+ :to="`/u/${roleMapping.principal.id}`"
+ class="normal-link"
+ >{{ roleMapping.principal.name }}
+ </router-link>
+ <span
+ v-if="currentUser.id == roleMapping.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 }}
+ </span>
+ </div>
+ </template>
</div>
</BBTableCell>
<BBTableCell class="">
diff --git a/frontend/src/miragejs/factories/index.js b/frontend/src/miragejs/factories/index.js
index 1c52a8ba78..642b7188c8 100644
--- a/frontend/src/miragejs/factories/index.js
+++ b/frontend/src/miragejs/factories/index.js
@@ -12,6 +12,7 @@ import roleMapping from "./roleMapping";
import batchUpdate from "./batchUpdate";
import loginInfo from "./loginInfo";
+import signupInfo from "./signupInfo";
/*
* factories are contained in a single object, that's why we
@@ -33,4 +34,5 @@ export default {
...roleMapping,
...batchUpdate,
...loginInfo,
+ ...signupInfo,
};
diff --git a/frontend/src/miragejs/factories/signupInfo.js b/frontend/src/miragejs/factories/signupInfo.js
new file mode 100644
index 0000000000..27f9ca9ce3
--- /dev/null
+++ b/frontend/src/miragejs/factories/signupInfo.js
@@ -0,0 +1,12 @@
+import { Factory } from "miragejs";
+
+export default {
+ signupInfo: Factory.extend({
+ usename() {
+ return "[email protected]";
+ },
+ password() {
+ return "blablabla";
+ },
+ }),
+};
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index e18361e095..9877f70727 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -18,6 +18,15 @@ export default function routes() {
this.get("/user");
+ this.post("/user", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("user");
+ const user = schema.users.findBy({ email: attrs.email });
+ if (user) {
+ return new Response(200, {}, user);
+ }
+ return schema.users.create(attrs);
+ });
+
// Auth
this.post("/auth/login", function (schema, request) {
const loginInfo = this.normalizedRequestAttrs("login-info");
@@ -45,7 +54,10 @@ export default function routes() {
{ errors: signupInfo.username + " already exists" }
);
}
- return schema.users.create(signupInfo);
+ return schema.users.create({
+ status: "ACTIVE",
+ ...signupInfo,
+ });
});
// RoleMapping
diff --git a/frontend/src/store/modules/principal.ts b/frontend/src/store/modules/principal.ts
index a81200534b..20a5c8ded1 100644
--- a/frontend/src/store/modules/principal.ts
+++ b/frontend/src/store/modules/principal.ts
@@ -1,12 +1,15 @@
import axios from "axios";
import {
User,
+ UNKNOWN_ID,
PrincipalId,
Principal,
+ PrincipalNew,
PrincipalState,
PrincipalStatus,
ResourceObject,
} from "../../types";
+import { isDevOrDemo, randomString } from "../../utils";
function convert(user: ResourceObject): Principal {
return {
@@ -26,29 +29,52 @@ const getters = {
return state.principalList;
},
+ principalByEmail: (state: PrincipalState) => (email: string): Principal => {
+ if (!email) {
+ return {
+ id: UNKNOWN_ID,
+ status: "UNKNOWN",
+ name: "<<Email Missing>>",
+ email: "",
+ };
+ }
+
+ const principal = state.principalList.find((item) => item.email == email);
+ if (principal) {
+ return principal;
+ }
+
+ return {
+ id: UNKNOWN_ID,
+ status: "UNKNOWN",
+ name: `<<Email ${email} not found>>`,
+ email: "",
+ };
+ },
+
principalById: (state: PrincipalState) => (
principalId: PrincipalId
): Principal => {
if (!principalId) {
return {
- id: "-1",
+ id: UNKNOWN_ID,
status: "UNKNOWN",
name: "<<ID Missing>>",
email: "",
};
}
- for (const principal of state.principalList) {
- if (principal.id == principalId) {
- return principal;
- }
+ const principal = state.principalList.find(
+ (item) => item.id == principalId
+ );
+ if (principal) {
+ return principal;
}
- // Return id as the name if no matching is found.
return {
id: principalId,
status: "UNKNOWN",
- name: principalId,
+ name: `<<ID ${principalId} not found>>`,
email: "",
};
},
@@ -75,6 +101,28 @@ const actions = {
return principal;
},
+
+ // Returns existing user if already created.
+ async createPrincipal({ commit }: any, newPrincipal: PrincipalNew) {
+ const createdPrincipal = convert(
+ (
+ await axios.post(`/api/user`, {
+ data: {
+ type: "user",
+ attributes: {
+ status: "INVITED",
+ email: newPrincipal.email,
+ password: isDevOrDemo() ? "aaa" : randomString(),
+ },
+ },
+ })
+ ).data.data
+ );
+
+ commit("appendPrincipal", createdPrincipal);
+
+ return createdPrincipal;
+ },
};
const mutations = {
@@ -82,6 +130,10 @@ const mutations = {
state.principalList = principalList;
},
+ appendPrincipal(state: PrincipalState, newPrincipal: Principal) {
+ state.principalList.push(newPrincipal);
+ },
+
replacePrincipalInList(state: PrincipalState, updatedPrincipal: Principal) {
const i = state.principalList.findIndex(
(item: Principal) => item.id == updatedPrincipal.id
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index f8a6060f58..5f042b0928 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -2,6 +2,8 @@ import { ResourceObject } from "./jsonapi";
import { BBNotificationStyle } from "../bbkit/types";
import { TaskFieldId } from "../plugins";
+export const UNKNOWN_ID = "-1";
+
// These ID format may change in the future, so we encapsulate with a type.
// Also good for readability.
export type UserId = string;
@@ -53,6 +55,10 @@ export type Principal = {
email: string;
};
+export type PrincipalNew = {
+ email: string;
+};
+
// RoleMapping
export type RoleType = "OWNER" | "DBA" | "DEVELOPER";
@@ -66,6 +72,7 @@ export type RoleMapping = {
};
export type RoleMappingNew = {
+ principalId: PrincipalId;
email: string;
role: RoleType;
updaterId: PrincipalId;
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index b2a3e697a5..dc16574b53 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -53,3 +53,17 @@ export function isValidEmail(email: string) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
+
+export function randomString(n?: number): string {
+ if (!n) {
+ n = 10;
+ }
+ var result = "";
+ var characters =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?";
+ var charactersLength = characters.length;
+ for (var i = 0; i < n; i++) {
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
+ }
+ return result;
+}