Skip to content

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

Introduce PrincipalType with “END_USR” and “BOT”

天舟008b4d2cb46d
diff --git a/frontend/src/miragejs/factories/index.js b/frontend/src/miragejs/factories/index.js
index 4c9e19561e..14b6b95f68 100644
--- a/frontend/src/miragejs/factories/index.js
+++ b/frontend/src/miragejs/factories/index.js
@@ -1,4 +1,4 @@
-import user from "./user";
+import principal from "./principal";
import bookmark from "./bookmark";
import environment from "./environment";
import project from "./project";
@@ -26,7 +26,7 @@ import activateInfo from "./activateInfo";
* be done for all future factories
*/
export default {
- ...user,
+ ...principal,
...bookmark,
...environment,
...project,
diff --git a/frontend/src/miragejs/factories/user.js b/frontend/src/miragejs/factories/principal.js
similarity index 100%
rename from frontend/src/miragejs/factories/user.js
rename to frontend/src/miragejs/factories/principal.js
diff --git a/frontend/src/miragejs/fixtures/index.js b/frontend/src/miragejs/fixtures/index.js
index 57d5d990ed..377cc90939 100644
--- a/frontend/src/miragejs/fixtures/index.js
+++ b/frontend/src/miragejs/fixtures/index.js
@@ -4,12 +4,12 @@
import workspaces from "./workspaceList.json";
import members from "./memberList.json";
-import users from "./userList.json";
+import principals from "./principalList.json";
import projects from "./projectList.json";
export default {
workspaces,
members,
- users,
+ principals,
projects,
};
diff --git a/frontend/src/miragejs/fixtures/memberList.json b/frontend/src/miragejs/fixtures/memberList.json
index 21a1c00fd6..fcfc21ba2d 100644
--- a/frontend/src/miragejs/fixtures/memberList.json
+++ b/frontend/src/miragejs/fixtures/memberList.json
@@ -1,7 +1,7 @@
[
{
"id": "1",
- "workspaceId": "1001",
+ "workspaceId": "1",
"updaterId": "1001",
"updatedTs": 1614571200000,
"createdTs": 1614528000000,
diff --git a/frontend/src/miragejs/fixtures/userList.json b/frontend/src/miragejs/fixtures/principalList.json
similarity index 63%
rename from frontend/src/miragejs/fixtures/userList.json
rename to frontend/src/miragejs/fixtures/principalList.json
index aa297b1bd4..00759361e4 100644
--- a/frontend/src/miragejs/fixtures/userList.json
+++ b/frontend/src/miragejs/fixtures/principalList.json
@@ -1,66 +1,84 @@
[
+ {
+ "id": "1",
+ "updaterId": "1",
+ "updatedTs": 1614571200000,
+ "createdTs": 1614528000000,
+ "creatorId": "1",
+ "status": "ACTIVE",
+ "type": "BOT",
+ "name": "Bytebase",
+ "email": "",
+ "passwordHash": ""
+ },
{
"id": "1001",
- "updaterId": "1001",
+ "updaterId": "1",
"updatedTs": 1614571200000,
"createdTs": 1614528000000,
- "creatorId": "1001",
+ "creatorId": "1",
"status": "ACTIVE",
+ "type": "END_USER",
"name": "Demo Owner",
"email": "[email protected]",
"passwordHash": "1024"
},
{
"id": "1002",
- "updaterId": "1001",
+ "updaterId": "1",
"updatedTs": 1614571200000,
"createdTs": 1614528000000,
- "creatorId": "1001",
+ "creatorId": "1",
"status": "ACTIVE",
+ "type": "END_USER",
"name": "Jerry DBA",
"email": "[email protected]",
"passwordHash": "aaa"
},
{
"id": "1003",
- "updaterId": "1001",
+ "updaterId": "1",
"updatedTs": 1614571200000,
"createdTs": 1614528000000,
- "creatorId": "1001",
+ "creatorId": "1",
"status": "ACTIVE",
+ "type": "END_USER",
"name": "Tom Dev",
"email": "[email protected]",
"passwordHash": "aaa"
},
{
"id": "1004",
- "updaterId": "1001",
+ "updaterId": "1",
"updatedTs": 1614571200000,
"createdTs": 1614528000000,
- "creatorId": "1001",
+ "creatorId": "1",
"status": "ACTIVE",
+ "type": "END_USER",
"name": "Alice DBA",
"email": "[email protected]",
"passwordHash": "aaa"
},
{
"id": "1005",
- "updaterId": "1001",
+ "updaterId": "1",
"updatedTs": 1614571200000,
"createdTs": 1614528000000,
- "creatorId": "1001",
+ "creatorId": "1",
"status": "ACTIVE",
+ "type": "END_USER",
"name": "Jane Dev",
"email": "[email protected]",
"passwordHash": "aaa"
},
{
- "id": "6",
- "updaterId": "1001",
+ "id": "1006",
+ "updaterId": "1",
"updatedTs": 1614571200000,
"createdTs": 1614528000000,
- "creatorId": "1001",
+ "creatorId": "1",
"status": "INVITED",
+ "type": "END_USER",
"name": "Bob Invited",
"email": "[email protected]",
"passwordHash": "aaa"
diff --git a/frontend/src/miragejs/models/index.js b/frontend/src/miragejs/models/index.js
index 1eaa387688..092ee3957a 100644
--- a/frontend/src/miragejs/models/index.js
+++ b/frontend/src/miragejs/models/index.js
@@ -12,10 +12,10 @@ import { Model, hasMany, belongsTo } from "miragejs";
* Mirage JS guide on Relationships: https://miragejs.com/docs/main-concepts/relationships/
*/
export default {
- // User
- user: Model.extend({}),
- userNew: Model,
- userPatch: Model,
+ // Principal
+ principal: Model.extend({}),
+ principalNew: Model,
+ principalPatch: Model,
// Workspace
workspace: Model.extend({
diff --git a/frontend/src/miragejs/routes/auth.js b/frontend/src/miragejs/routes/auth.js
index d6e2e46769..7e178e55d3 100644
--- a/frontend/src/miragejs/routes/auth.js
+++ b/frontend/src/miragejs/routes/auth.js
@@ -5,7 +5,7 @@ import { postMessageToOwnerAndDBA } from "../utils";
export default function configureAuth(route) {
route.post("/auth/login", function (schema, request) {
const loginInfo = this.normalizedRequestAttrs("login-info");
- const user = schema.users.findBy({
+ const user = schema.principals.findBy({
email: loginInfo.email,
passwordHash: loginInfo.password,
});
@@ -21,7 +21,7 @@ export default function configureAuth(route) {
route.post("/auth/signup", function (schema, request) {
const signupInfo = this.normalizedRequestAttrs("signup-info");
- const user = schema.users.findBy({ email: signupInfo.email });
+ const user = schema.principals.findBy({ email: signupInfo.email });
if (user) {
return new Response(
409,
@@ -30,7 +30,7 @@ export default function configureAuth(route) {
);
}
const ts = Date.now();
- const createdUser = schema.users.create({
+ const createdUser = schema.principals.create({
createdTs: ts,
updatedTs: ts,
status: "ACTIVE",
@@ -79,7 +79,7 @@ export default function configureAuth(route) {
return new Response(400, {}, { errors: "Missing activation token" });
}
- const user = schema.users.findBy({ email: activateInfo.email });
+ const user = schema.principals.findBy({ email: activateInfo.email });
if (user) {
const ts = Date.now();
user.update({
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index 8be5773b0c..c631bd6528 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -1,7 +1,7 @@
/*
* Mirage JS guide on Routes: https://miragejs.com/docs/route-handlers/functions
*/
-import configureUser from "./user";
+import configurePrincipal from "./principal";
import configureAuth from "./auth";
import configureMember from "./member";
import configureActivity from "./activity";
@@ -29,8 +29,8 @@ export default function routes() {
this.namespace = "api";
- // User
- configureUser(this);
+ // Principal
+ configurePrincipal(this);
// Auth
configureAuth(this);
diff --git a/frontend/src/miragejs/routes/member.js b/frontend/src/miragejs/routes/member.js
index f6a331dbe8..ae308ea3a5 100644
--- a/frontend/src/miragejs/routes/member.js
+++ b/frontend/src/miragejs/routes/member.js
@@ -32,7 +32,7 @@ export default function configureMember(route) {
const createdMember = schema.members.create(newMember);
- const user = schema.users.find(attrs.principalId);
+ const user = schema.principals.find(attrs.principalId);
const type =
user.status == "INVITED"
? "bb.msg.member.invite"
diff --git a/frontend/src/miragejs/routes/principal.js b/frontend/src/miragejs/routes/principal.js
new file mode 100644
index 0000000000..ce85c11b8c
--- /dev/null
+++ b/frontend/src/miragejs/routes/principal.js
@@ -0,0 +1,43 @@
+export default function configurePrincipal(route) {
+ route.get("/principal/:id");
+
+ route.get("/principal");
+
+ route.post("/principal", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("principal-new");
+ const principal = schema.principals.findBy({ email: attrs.email });
+ if (principal) {
+ return principal;
+ }
+
+ const ts = Date.now();
+ return schema.principals.create({
+ ...attrs,
+ creatorId: attrs.creatorId,
+ createdTs: ts,
+ updaterId: attrs.creatorId,
+ updatedTs: ts,
+ });
+ });
+
+ route.patch("/principal/:principalId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("principal-patch");
+ const principal = schema.principals.find(request.params.principalId);
+
+ if (!principal) {
+ return new Response(
+ 404,
+ {},
+ {
+ errors: "Principal id " + request.params.principalId + " not found",
+ }
+ );
+ }
+
+ const ts = Date.now();
+ return principal.update({
+ ...attrs,
+ updatedTs: ts,
+ });
+ });
+}
diff --git a/frontend/src/miragejs/routes/task.js b/frontend/src/miragejs/routes/task.js
index 9f0763070c..1d2ba73dfc 100644
--- a/frontend/src/miragejs/routes/task.js
+++ b/frontend/src/miragejs/routes/task.js
@@ -1,6 +1,6 @@
import { Response } from "miragejs";
import { WORKSPACE_ID } from "./index";
-import { IssueBuiltinFieldId } from "../../plugins";
+import { SYSTEM_BOT_ID } from "../../types";
export default function configureTask(route) {
route.patch(
@@ -73,9 +73,9 @@ export default function configureTask(route) {
});
schema.activities.create({
- creatorId: attrs.updaterId,
+ creatorId: SYSTEM_BOT_ID,
createdTs: ts,
- updaterId: attrs.updaterId,
+ updaterId: SYSTEM_BOT_ID,
updatedTs: ts,
actionType: "bytebase.pipeline.task.status.update",
containerId: attrs.containerId,
diff --git a/frontend/src/miragejs/routes/user.js b/frontend/src/miragejs/routes/user.js
deleted file mode 100644
index 1fbc815ec6..0000000000
--- a/frontend/src/miragejs/routes/user.js
+++ /dev/null
@@ -1,43 +0,0 @@
-export default function configureUser(route) {
- route.get("/user/:id");
-
- route.get("/user");
-
- route.post("/user", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("user-new");
- const user = schema.users.findBy({ email: attrs.email });
- if (user) {
- return user;
- }
-
- const ts = Date.now();
- return schema.users.create({
- ...attrs,
- creatorId: attrs.creatorId,
- createdTs: ts,
- updaterId: attrs.creatorId,
- updatedTs: ts,
- });
- });
-
- route.patch("/user/:userId", function (schema, request) {
- const attrs = this.normalizedRequestAttrs("user-patch");
- const user = schema.users.find(request.params.userId);
-
- if (!user) {
- return new Response(
- 404,
- {},
- {
- errors: "User id " + request.params.userId + " not found",
- }
- );
- }
-
- const ts = Date.now();
- return user.update({
- ...attrs,
- updatedTs: ts,
- });
- });
-}
diff --git a/frontend/src/miragejs/seeds/index.ts b/frontend/src/miragejs/seeds/index.ts
index bbb6ddaf6e..36b417c60a 100644
--- a/frontend/src/miragejs/seeds/index.ts
+++ b/frontend/src/miragejs/seeds/index.ts
@@ -25,15 +25,15 @@ const workspacesSeeder = (server: any) => {
const workspace2 = server.schema.workspaces.find(101);
// User
- const ws1Owner = server.schema.users.find(1001);
- const ws1DBA = server.schema.users.find(1002);
- const ws1Dev1 = server.schema.users.find(1003);
- const ws1Dev2 = server.schema.users.find(1005);
+ const ws1Owner = server.schema.principals.find(1001);
+ const ws1DBA = server.schema.principals.find(1002);
+ const ws1Dev1 = server.schema.principals.find(1003);
+ const ws1Dev2 = server.schema.principals.find(1005);
const ws1UserList = [ws1Owner, ws1DBA, ws1Dev1, ws1Dev2];
- const ws2DBA = server.schema.users.find(1004);
- const ws2Dev = server.schema.users.find(1001);
+ const ws2DBA = server.schema.principals.find(1004);
+ const ws2Dev = server.schema.principals.find(1001);
const ws2UserList = [ws2DBA, ws2Dev];
// Environment
diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.ts
index 79440939cb..6a3c472b17 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -18,6 +18,7 @@ const GUEST: Principal = {
createdTs: 0,
updatedTs: 0,
status: "ACTIVE",
+ type: "END_USER",
name: "Guest",
role: "GUEST",
diff --git a/frontend/src/store/modules/principal.ts b/frontend/src/store/modules/principal.ts
index f5981d4121..361535f508 100644
--- a/frontend/src/store/modules/principal.ts
+++ b/frontend/src/store/modules/principal.ts
@@ -1,4 +1,5 @@
import axios from "axios";
+import { cloneDeep } from "lodash";
import {
UNKNOWN_ID,
PrincipalId,
@@ -8,31 +9,33 @@ import {
PrincipalState,
PrincipalStatus,
ResourceObject,
- UserPatch,
unknown,
empty,
EMPTY_ID,
+ PrincipalType,
+ SYSTEM_BOT_ID,
} from "../../types";
import { isDevOrDemo, randomString } from "../../utils";
-function convert(user: ResourceObject, rootGetters: any): Principal {
+function convert(principal: ResourceObject, rootGetters: any): Principal {
const creator = rootGetters["principal/principalById"](
- user.attributes.creatorId
+ principal.attributes.creatorId
);
const updater = rootGetters["principal/principalById"](
- user.attributes.updaterId
+ principal.attributes.updaterId
);
- const member = rootGetters["member/memberByPrincipalId"](user.id);
+ const member = rootGetters["member/memberByPrincipalId"](principal.id);
return {
- id: user.id,
+ id: principal.id,
creator,
updater,
- createdTs: user.attributes.createdTs as number,
- updatedTs: user.attributes.updatedTs as number,
- status: user.attributes.status as PrincipalStatus,
- name: user.attributes.name as string,
- email: user.attributes.email as string,
+ createdTs: principal.attributes.createdTs as number,
+ updatedTs: principal.attributes.updatedTs as number,
+ status: principal.attributes.status as PrincipalStatus,
+ type: principal.attributes.type as PrincipalType,
+ name: principal.attributes.name as string,
+ email: principal.attributes.email as string,
role: member.role,
};
}
@@ -68,8 +71,22 @@ const getters = {
};
const actions = {
- async fetchPrincipalList({ commit, rootGetters }: any) {
- const userList: ResourceObject[] = (await axios.get(`/api/user`)).data.data;
+ async fetchPrincipalList({ state, commit, rootGetters }: any) {
+ const userList: ResourceObject[] = (await axios.get(`/api/principal`)).data
+ .data;
+
+ // Dealing with bootstraping here. The system bot is the first user and we
+ // assign itself as the creator/updater.
+ // We also convert the system bot alone first so that the remaining users
+ // can convert their creater/updater properly.
+ const systemBot = userList.find((item) => item.id == SYSTEM_BOT_ID);
+ if (systemBot) {
+ const theBot = convert(systemBot, rootGetters);
+ // Use cloneDeep to avoid infinite recursion during JSON.stringify
+ theBot.creator = cloneDeep(theBot);
+ theBot.updater = cloneDeep(theBot);
+ commit("upsertPrincipalInList", theBot);
+ }
const principalList = userList.map((user) => {
return convert(user, rootGetters);
@@ -84,7 +101,7 @@ const actions = {
principalId: PrincipalId
) {
const principal = convert(
- (await axios.get(`/api/user/${principalId}`)).data.data,
+ (await axios.get(`/api/principal/${principalId}`)).data.data,
rootGetters
);
@@ -100,9 +117,9 @@ const actions = {
) {
const createdPrincipal = convert(
(
- await axios.post(`/api/user`, {
+ await axios.post(`/api/principal`, {
data: {
- type: "usernew",
+ type: "principalnew",
attributes: {
status: "INVITED",
name: newPrincipal.name,
@@ -130,16 +147,12 @@ const actions = {
principalPatch: PrincipalPatch;
}
) {
- const userPatch: UserPatch = {
- updaterId: principalPatch.updaterId,
- name: principalPatch.name,
- };
const updatedPrincipal = convert(
(
- await axios.patch(`/api/user/${principalId}`, {
+ await axios.patch(`/api/principal/${principalId}`, {
data: {
- type: "userpatch",
- attributes: userPatch,
+ type: "principalpatch",
+ attributes: principalPatch,
},
})
).data.data,
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 0e35c833da..53ceb64e9c 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -2,6 +2,9 @@ import { ResourceObject } from "./jsonapi";
import { BBNotificationStyle } from "../bbkit/types";
import { FieldId } from "../plugins";
+// System bot id
+export const SYSTEM_BOT_ID = "1";
+
// The project to hold those databases synced from the instance but haven't been assigned an application
// project yet. We can't use UNKNOWN_ID because of referential integrity.
export const DEFAULT_PROJECT_ID = "1";
@@ -17,7 +20,6 @@ export const EMPTY_ID = "0";
export type ResourceType =
| "PRINCIPAL"
| "EXECUTION"
- | "USER"
| "MEMBER"
| "ENVIRONMENT"
| "PROJECT"
@@ -40,7 +42,6 @@ export const unknown = (
):
| Execution
| Principal
- | User
| Member
| Environment
| Project
@@ -66,20 +67,12 @@ export const unknown = (
createdTs: 0,
updatedTs: 0,
status: "UNKNOWN",
+ type: "END_USER",
name: "<<Unknown principal>>",
email: "",
role: "GUEST",
} as Principal;
- const UNKNOWN_USER: User = {
- id: UNKNOWN_ID,
- createdTs: 0,
- updatedTs: 0,
- status: "UNKNOWN",
- name: "<<Unknown user>>",
- email: "[email protected]",
- };
-
const UNKNOWN_MEMBER: Member = {
id: UNKNOWN_ID,
creator: UNKNOWN_PRINCIPAL,
@@ -258,8 +251,6 @@ export const unknown = (
return UNKNOWN_EXECUTION;
case "PRINCIPAL":
return UNKNOWN_PRINCIPAL;
- case "USER":
- return UNKNOWN_USER;
case "MEMBER":
return UNKNOWN_MEMBER;
case "ENVIRONMENT":
@@ -297,7 +288,6 @@ export const empty = (
):
| Execution
| Principal
- | User
| Member
| Environment
| Project
@@ -323,20 +313,12 @@ export const empty = (
createdTs: 0,
updatedTs: 0,
status: "UNKNOWN",
+ type: "END_USER",
name: "",
email: "",
role: "GUEST",
} as Principal;
- const EMPTY_USER: User = {
- id: EMPTY_ID,
- createdTs: 0,
- updatedTs: 0,
- status: "UNKNOWN",
- name: "",
- email: "",
- };
-
const EMPTY_MEMBER: Member = {
id: EMPTY_ID,
creator: EMPTY_PRINCIPAL,
@@ -515,8 +497,6 @@ export const empty = (
return EMPTY_EXECUTION;
case "PRINCIPAL":
return EMPTY_PRINCIPAL;
- case "USER":
- return EMPTY_USER;
case "MEMBER":
return EMPTY_MEMBER;
case "ENVIRONMENT":
@@ -690,6 +670,8 @@ export type ProjectMemberPatch = {
// we may support application/bot identity.
export type PrincipalStatus = "UNKNOWN" | "INVITED" | "ACTIVE";
+export type PrincipalType = "END_USER" | "BOT";
+
export type Principal = {
id: PrincipalId;
@@ -701,6 +683,7 @@ export type Principal = {
// Domain specific fields
status: PrincipalStatus;
+ type: PrincipalType;
name: string;
email: string;
role: RoleType;
@@ -718,30 +701,6 @@ export type PrincipalNew = {
export type PrincipalPatch = {
// Standard fields
updaterId: PrincipalId;
- rowStatus?: RowStatus;
-
- // Domain specific fields
- name?: string;
-};
-
-export type User = {
- id: UserId;
-
- // Standard fields
- // [TODO] User doesn't have updater, creator fields because of bootstrap issue.
- // Who is the updater, creator for the 1st user?
- createdTs: number;
- updatedTs: number;
-
- // Domain specific fields
- status: PrincipalStatus;
- name: string;
- email: string;
-};
-
-export type UserPatch = {
- // Standard fields
- updaterId: PrincipalId;
// Domain specific fields
name?: string;