Skip to content

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

Access role via Principal instead of RoleMapping

天舟3297210064ec
diff --git a/frontend/src/components/MemberInvite.vue b/frontend/src/components/MemberInvite.vue
index dda78f025b..c3f4b8b378 100644
--- a/frontend/src/components/MemberInvite.vue
+++ b/frontend/src/components/MemberInvite.vue
@@ -156,10 +156,7 @@ export default {
const principal = store.getters["principal/principalByEmail"](
invite.email
);
- if (
- principal &&
- store.getters["roleMapping/roleMappingByPrincipalId"](principal.id)
- ) {
+ if (principal && principal.role != "GUEST") {
return "Already a member";
}
}
diff --git a/frontend/src/components/MemberTable.vue b/frontend/src/components/MemberTable.vue
index f5bd308ee1..cee04d3519 100644
--- a/frontend/src/components/MemberTable.vue
+++ b/frontend/src/components/MemberTable.vue
@@ -182,10 +182,7 @@ export default {
});
const allowEdit = computed(() => {
- const myRoleMapping = store.getters[
- "roleMapping/roleMappingByPrincipalId"
- ](currentUser.value.id);
- return myRoleMapping.role == "OWNER";
+ return currentUser.value.role == "OWNER";
});
const changeRole = (id: RoleMappingId, role: RoleType) => {
diff --git a/frontend/src/layouts/BodyLayout.vue b/frontend/src/layouts/BodyLayout.vue
index 43afaf3529..7381da5b0a 100644
--- a/frontend/src/layouts/BodyLayout.vue
+++ b/frontend/src/layouts/BodyLayout.vue
@@ -207,12 +207,13 @@ export default {
const quickActionList = computed(() => {
const user = store.getters["auth/currentUser"]();
- const role = store.getters["roleMapping/roleMappingByPrincipalId"](
- user.id
- ).role;
+ const role = user.role;
const listByRole = router.currentRoute.value.meta.quickActionListByRole;
- if (listByRole?.get(role)?.length > 0) {
- return listByRole.get(role);
+ if (listByRole) {
+ const list = listByRole.get(role);
+ if (list && list.length > 0) {
+ return list;
+ }
}
return undefined;
});
diff --git a/frontend/src/plugins/types.ts b/frontend/src/plugins/types.ts
index 4f7217a617..0113bbeabd 100644
--- a/frontend/src/plugins/types.ts
+++ b/frontend/src/plugins/types.ts
@@ -1,4 +1,4 @@
-import { Environment, TaskNew, User } from "../types";
+import { Environment, Principal, TaskNew } from "../types";
// Task
export type TaskFieldId = number;
@@ -39,7 +39,7 @@ export type TaskField = {
// Template
export type TemplateContext = {
environmentList: Environment[];
- currentUser: User;
+ currentUser: Principal;
};
export type TaskTemplate = {
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index f066f5e332..48bf721448 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -17,7 +17,7 @@ import Activate from "../views/auth/Activate.vue";
import PasswordReset from "../views/auth/PasswordReset.vue";
import { store } from "../store";
import { isDev, idFromSlug } from "../utils";
-import { User } from "../types";
+import { Principal } from "../types";
const HOME_MODULE = "workspace.home";
const AUTH_MODULE = "auth";
@@ -398,7 +398,7 @@ export const router = createRouter({
});
router.beforeEach((to, from, next) => {
- const loginUser: User = store.getters["auth/currentUser"]();
+ const loginUser: Principal = store.getters["auth/currentUser"]();
const isGuest = loginUser.id == "0";
const fromModule = from.name
diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.ts
index 286c006756..5dc66e2a13 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -1,6 +1,6 @@
import axios from "axios";
import {
- User,
+ Principal,
AuthState,
LoginInfo,
SignupInfo,
@@ -8,26 +8,24 @@ import {
ResourceObject,
} from "../../types";
-const GUEST: User = {
+const GUEST: Principal = {
id: "0",
status: "ACTIVE",
name: "Guest",
+ role: "GUEST",
};
-function convert(user: ResourceObject): User {
- return {
- id: user.id,
- ...(user.attributes as Omit<User, "id">),
- };
+function convert(user: ResourceObject, rootGetters: any): Principal {
+ return rootGetters["principal/principalById"](user.id);
}
const state: () => AuthState = () => ({
- currentUser: undefined,
+ currentUser: GUEST,
});
const getters = {
- currentUser: (state: AuthState) => (): User => {
+ currentUser: (state: AuthState) => (): Principal => {
if (state.currentUser) {
return state.currentUser;
}
@@ -40,13 +38,14 @@ const getters = {
};
const actions = {
- async login({ commit }: any, loginInfo: LoginInfo) {
+ async login({ commit, rootGetters }: any, loginInfo: LoginInfo) {
const loggedInUser = convert(
(
await axios.post("/api/auth/login", {
data: { type: "loginInfo", attributes: loginInfo },
})
- ).data.data
+ ).data.data,
+ rootGetters
);
localStorage.setItem("bb.auth.user", JSON.stringify(loggedInUser));
@@ -54,13 +53,14 @@ const actions = {
return loggedInUser;
},
- async signup({ commit }: any, signupInfo: SignupInfo) {
+ async signup({ commit, rootGetters }: any, signupInfo: SignupInfo) {
const newUser = convert(
(
await axios.post("/api/auth/signup", {
data: { type: "signupInfo", attributes: signupInfo },
})
- ).data.data
+ ).data.data,
+ rootGetters
);
localStorage.setItem("bb.auth.user", JSON.stringify(newUser));
@@ -68,13 +68,14 @@ const actions = {
return newUser;
},
- async activate({ commit }: any, activateInfo: ActivateInfo) {
+ async activate({ commit, rootGetters }: any, activateInfo: ActivateInfo) {
const activatedUser = convert(
(
await axios.post("/api/auth/activate", {
data: { type: "activateInfo", attributes: activateInfo },
})
- ).data.data
+ ).data.data,
+ rootGetters
);
localStorage.setItem("bb.auth.user", JSON.stringify(activatedUser));
@@ -85,7 +86,7 @@ const actions = {
async restoreUser({ commit }: any) {
const jsonUser = localStorage.getItem("bb.auth.user");
if (jsonUser) {
- const user: User = JSON.parse(jsonUser);
+ const user: Principal = JSON.parse(jsonUser);
commit("setCurrentUser", user);
return user;
}
@@ -100,7 +101,7 @@ const actions = {
};
const mutations = {
- setCurrentUser(state: AuthState, user: User) {
+ setCurrentUser(state: AuthState, user: Principal) {
state.currentUser = user;
},
};
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 8dfd4e8cdd..19bee9bb0b 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -39,16 +39,6 @@ export type CommandId = string;
export type CommandRegisterId = string;
// Persistent State Models
-// User
-export type UserStatus = "INVITED" | "ACTIVE";
-
-export type User = {
- id: UserId;
- status: UserStatus;
- name: string;
- email: string;
-};
-export type NewUser = Omit<User, "id">;
// RoleMapping
export type RoleType = "OWNER" | "DBA" | "DEVELOPER" | "GUEST";
@@ -75,6 +65,9 @@ export type RoleMappingPatch = {
};
// Principal
+// This is a facet of the underlying identity entity.
+// For now, there is only user type. In the future,
+// we may support application/bot identity.
export type PrincipalStatus = "UNKNOWN" | "INVITED" | "ACTIVE";
export type Principal = {
@@ -94,6 +87,14 @@ export type PrincipalPatch = {
name?: string;
};
+export type User = {
+ id: UserId;
+ status: PrincipalStatus;
+ name: string;
+ email: string;
+};
+export type NewUser = Omit<User, "id">;
+
// Bookmark
export type Bookmark = {
id: BookmarkId;
@@ -377,7 +378,7 @@ export type QuickActionType =
// Store
export interface AuthState {
- currentUser: User | undefined;
+ currentUser: Principal;
}
export interface RoleMappingState {
diff --git a/frontend/src/views/DataSourceDetail.vue b/frontend/src/views/DataSourceDetail.vue
index 6d9a592cd5..773f3ed107 100644
--- a/frontend/src/views/DataSourceDetail.vue
+++ b/frontend/src/views/DataSourceDetail.vue
@@ -445,10 +445,9 @@ export default {
});
const allowEdit = computed(() => {
- const myRoleMapping = store.getters[
- "roleMapping/roleMappingByPrincipalId"
- ](currentUser.value.id);
- return myRoleMapping.role == "OWNER" || myRoleMapping.role == "DBA";
+ return (
+ currentUser.value.role == "OWNER" || currentUser.value.role == "DBA"
+ );
});
const allowSave = computed(() => {
diff --git a/frontend/src/views/ProfileDashboard.vue b/frontend/src/views/ProfileDashboard.vue
index 3d099ea4c4..c6f2c1eb56 100644
--- a/frontend/src/views/ProfileDashboard.vue
+++ b/frontend/src/views/ProfileDashboard.vue
@@ -192,13 +192,11 @@ export default {
if (props.principalId) {
return store.getters["principal/principalById"](props.principalId);
}
- return store.getters["principal/principalById"](currentUser.value.id);
+ return store.getters["auth/currentUser"]();
});
const role = computed(() => {
- const role = store.getters["roleMapping/roleMappingByPrincipalId"](
- principal.value.id
- ).role;
+ const role = principal.value.role;
return role == "DBA"
? role
: role.charAt(0).toUpperCase() + role.slice(1).toLowerCase();
diff --git a/frontend/src/views/SettingWorkspaceMember.vue b/frontend/src/views/SettingWorkspaceMember.vue
index 85170096c2..dd0add6ef3 100644
--- a/frontend/src/views/SettingWorkspaceMember.vue
+++ b/frontend/src/views/SettingWorkspaceMember.vue
@@ -22,13 +22,7 @@ export default {
const currentUser = computed(() => store.getters["auth/currentUser"]());
const allowInvite = computed(() => {
- const myRoleMapping = store.getters[
- "roleMapping/roleMappingByPrincipalId"
- ](currentUser.value.id);
- if (myRoleMapping.role != "OWNER") {
- return false;
- }
- return true;
+ return currentUser.value.role == "OWNER";
});
return {