Skip to content

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

feat: add auth v1 store (#4847)

boojackc1c06184432c
diff --git a/frontend/src/store/modules/user.ts b/frontend/src/store/modules/user.ts
index 480eb5a506..ba56d66bc0 100644
--- a/frontend/src/store/modules/user.ts
+++ b/frontend/src/store/modules/user.ts
@@ -1,6 +1,8 @@
import { defineStore } from "pinia";
import { authServiceClient } from "@/grpcweb";
import { User } from "@/types/proto/v1/auth_service";
+import { isEqual, isUndefined } from "lodash-es";
+import { userNamePrefix } from "./v1/common";
interface UserState {
userMapByName: Map<string, User>;
@@ -16,6 +18,13 @@ export const useUserStore = defineStore("user", {
},
},
actions: {
+ async fetchUserList() {
+ const { users } = await authServiceClient().listUsers({});
+ for (const user of users) {
+ this.userMapByName.set(user.name, user);
+ }
+ return users;
+ },
async fetchUser(name: string) {
const user = await authServiceClient().getUser({
name,
@@ -23,5 +32,70 @@ export const useUserStore = defineStore("user", {
this.userMapByName.set(user.name, user);
return user;
},
+ async createUser(create: User) {
+ const user = await authServiceClient().createUser({
+ user: create,
+ });
+ this.userMapByName.set(user.name, user);
+ return user;
+ },
+ async updateUser(update: Partial<User>) {
+ const originData = await this.getOrFetchUserByName(update.name || "");
+ if (!originData) {
+ throw new Error(`user with name ${update.name} not found`);
+ }
+
+ const user = await authServiceClient().updateUser({
+ user: update,
+ updateMask: getUpdateMaskFromUsers(originData, update),
+ });
+ this.userMapByName.set(user.name, user);
+ return user;
+ },
+ async getOrFetchUserByName(name: string) {
+ const cachedData = this.userMapByName.get(name);
+ if (cachedData) {
+ return cachedData;
+ }
+ const user = await authServiceClient().getUser({
+ name,
+ });
+ this.userMapByName.set(user.name, user);
+ return user;
+ },
+ getUserByName(name: string) {
+ return this.userMapByName.get(name);
+ },
+ async getOrFetchUserById(id: number) {
+ const name = `${userNamePrefix}${id}`;
+ return await this.getOrFetchUserByName(name);
+ },
+ getUserById(id: number) {
+ const name = `${userNamePrefix}${id}`;
+ return this.userMapByName.get(name);
+ },
},
});
+
+const getUpdateMaskFromUsers = (
+ origin: User,
+ update: Partial<User>
+): string[] => {
+ const updateMask: string[] = [];
+ if (!isUndefined(update.title) && !isEqual(origin.title, update.title)) {
+ updateMask.push("user.title");
+ }
+ if (!isUndefined(update.email) && !isEqual(origin.email, update.email)) {
+ updateMask.push("user.email");
+ }
+ if (!isUndefined(update.password)) {
+ updateMask.push("user.password");
+ }
+ if (
+ !isUndefined(update.userRole) &&
+ !isEqual(origin.userRole, update.userRole)
+ ) {
+ updateMask.push("user.role");
+ }
+ return updateMask;
+};
diff --git a/frontend/src/store/modules/v1/auth.ts b/frontend/src/store/modules/v1/auth.ts
new file mode 100644
index 0000000000..54cb7a637a
--- /dev/null
+++ b/frontend/src/store/modules/v1/auth.ts
@@ -0,0 +1,104 @@
+import axios from "axios";
+import { isEqual } from "lodash-es";
+import { defineStore } from "pinia";
+import { computed } from "vue";
+import { authServiceClient } from "@/grpcweb";
+import { ActivateInfo, SignupInfo } from "@/types";
+import { LoginRequest, User } from "@/types/proto/v1/auth_service";
+import { getIntCookie } from "@/utils";
+import { unknown } from "@/utils/common";
+import { useUserStore } from "../user";
+import { getUserId } from "./common";
+
+interface AuthState {
+ currentUser: User;
+}
+
+export const useAuthV1Store = defineStore("auth_v1", {
+ state: (): AuthState => ({
+ currentUser: User.fromPartial({}),
+ }),
+ actions: {
+ isLoggedIn: () => {
+ return getIntCookie("user") != undefined;
+ },
+ async login(request: Partial<LoginRequest>) {
+ await axios.post("/v1/auth/login", request);
+ const userId = getIntCookie("user");
+ if (userId) {
+ const loggedInUser = await useUserStore().getOrFetchUserById(userId);
+ this.currentUser = loggedInUser;
+ return loggedInUser;
+ }
+ return unknown("USER");
+ },
+ async signup(signupInfo: SignupInfo) {
+ await authServiceClient().createUser({
+ user: {
+ email: signupInfo.email,
+ title: signupInfo.name,
+ password: signupInfo.password,
+ },
+ });
+ const user = await this.login({
+ email: signupInfo.email,
+ password: signupInfo.password,
+ });
+ return user;
+ },
+ async logout() {
+ const unknownUser = unknown("USER");
+ try {
+ await axios.post("/v1/auth/logout");
+ } finally {
+ this.currentUser = unknownUser;
+ }
+ return unknownUser;
+ },
+ async activate(activateInfo: ActivateInfo) {
+ const activatedUser = (
+ await axios.post("/api/auth/activate", {
+ data: { type: "activateInfo", attributes: activateInfo },
+ })
+ ).data.data;
+
+ // Refresh the corresponding user.
+ const user = await useUserStore().getOrFetchUserById(activatedUser.id);
+ this.currentUser = user;
+ return user;
+ },
+ async restoreUser() {
+ const userId = getIntCookie("user");
+ if (userId) {
+ const loggedInUser = await useUserStore().getOrFetchUserById(userId);
+ this.currentUser = loggedInUser;
+ return loggedInUser;
+ }
+ return unknown("USER");
+ },
+ async refreshUserIfNeeded(name: string) {
+ if (name === this.currentUser.name) {
+ const refreshedUser = await useUserStore().getOrFetchUserByName(name);
+ if (!isEqual(refreshedUser, this.currentUser)) {
+ this.currentUser = refreshedUser;
+ }
+ }
+ },
+ },
+});
+
+export const useCurrentUser = () => {
+ const authStore = useAuthV1Store();
+
+ return computed(() => {
+ return {
+ ...authStore.currentUser,
+ id: getUserId(authStore.currentUser.name),
+ };
+ });
+};
+
+export const useIsLoggedIn = () => {
+ const store = useAuthV1Store();
+ return computed(() => store.isLoggedIn() && store.currentUser.name !== "");
+};
diff --git a/frontend/src/store/modules/v1/common.ts b/frontend/src/store/modules/v1/common.ts
new file mode 100644
index 0000000000..b0cddb98af
--- /dev/null
+++ b/frontend/src/store/modules/v1/common.ts
@@ -0,0 +1,36 @@
+export const userNamePrefix = "users/";
+export const environmentNamePrefix = "environments/";
+
+export const getNameParentTokens = (
+ name: string,
+ tokenPrefixes: string[]
+): string[] => {
+ const parts = name.split("/");
+ if (parts.length !== 2 * tokenPrefixes.length) {
+ return [];
+ }
+
+ const tokens: string[] = [];
+ for (let i = 0; i < tokenPrefixes.length; i++) {
+ if (parts[2 * i] !== tokenPrefixes[i]) {
+ return [];
+ }
+ if (parts[2 * i + 1] === "") {
+ return [];
+ }
+ tokens.push(parts[2 * i + 1]);
+ }
+ return tokens;
+};
+
+export const getUserId = (name: string): number => {
+ const tokens = getNameParentTokens(name, [userNamePrefix]);
+ const userId = Number(tokens[0]);
+ return userId;
+};
+
+export const getEnvironmentId = (name: string): number => {
+ const tokens = getNameParentTokens(name, [environmentNamePrefix]);
+ const environmentId = Number(tokens[0]);
+ return environmentId;
+};
diff --git a/frontend/src/store/modules/v1/environment.ts b/frontend/src/store/modules/v1/environment.ts
index a450f2c015..e431449cd9 100644
--- a/frontend/src/store/modules/v1/environment.ts
+++ b/frontend/src/store/modules/v1/environment.ts
@@ -1,9 +1,12 @@
import { defineStore } from "pinia";
import { environmentServiceClient } from "@/grpcweb";
import { Environment } from "@/types/proto/v1/environment_service";
+import { ResourceId } from "@/types";
+import { isEqual, isUndefined } from "lodash-es";
+import { State } from "@/types/proto/v1/common";
interface EnvironmentState {
- environmentMapByName: Map<string, Environment>;
+ environmentMapByName: Map<ResourceId, Environment>;
}
export const useEnvironmentV1Store = defineStore("environment_v1", {
@@ -38,8 +41,69 @@ export const useEnvironmentV1Store = defineStore("environment_v1", {
);
return createdEnvironment;
},
+ async updateEnvironment(update: Partial<Environment>) {
+ const originData = await this.getOrFetchEnvironmentByName(
+ update.name || ""
+ );
+ if (!originData) {
+ throw new Error(`environment with name ${update.name} not found`);
+ }
+
+ const environment = await environmentServiceClient().updateEnvironment({
+ environment: update,
+ updateMask: getUpdateMaskFromEnvironments(originData, update),
+ });
+ this.environmentMapByName.set(environment.name, environment);
+ return environment;
+ },
+ async deleteEnvironment(name: string) {
+ await environmentServiceClient().deleteEnvironment({
+ name,
+ });
+ const cachedData = this.getEnvironmentByName(name);
+ if (cachedData) {
+ this.environmentMapByName.set(name, {
+ ...cachedData,
+ state: State.DELETED,
+ });
+ }
+ },
+ async undeleteEnvironment(name: string) {
+ const environment = await environmentServiceClient().undeleteEnvironment({
+ name,
+ });
+ this.environmentMapByName.set(environment.name, environment);
+ },
+ async getOrFetchEnvironmentByName(name: string) {
+ const cachedData = this.environmentMapByName.get(name);
+ if (cachedData) {
+ return cachedData;
+ }
+ const environment = await environmentServiceClient().getEnvironment({
+ name,
+ });
+ this.environmentMapByName.set(environment.name, environment);
+ return environment;
+ },
getEnvironmentByName(name: string) {
return this.environmentMapByName.get(name);
},
},
});
+
+const getUpdateMaskFromEnvironments = (
+ origin: Environment,
+ update: Partial<Environment>
+): string[] => {
+ const updateMask: string[] = [];
+ if (!isUndefined(update.title) && !isEqual(origin.title, update.title)) {
+ updateMask.push("environment.title");
+ }
+ if (!isUndefined(update.order) && !isEqual(origin.order, update.order)) {
+ updateMask.push("environment.order");
+ }
+ if (!isUndefined(update.tier) && !isEqual(origin.tier, update.tier)) {
+ updateMask.push("environment.tier");
+ }
+ return updateMask;
+};
diff --git a/frontend/src/utils/common.ts b/frontend/src/utils/common.ts
new file mode 100644
index 0000000000..015a64e94e
--- /dev/null
+++ b/frontend/src/utils/common.ts
@@ -0,0 +1,43 @@
+import { User } from "@/types/proto/v1/auth_service";
+import { Environment } from "@/types/proto/v1/environment_service";
+
+export type ResourceType = "USER" | "ENVIRONMENT";
+
+interface ResourceMaker {
+ (type: "USER"): User;
+ (type: "ENVIRONMENT"): Environment;
+}
+
+const makeUnknown = (type: ResourceType) => {
+ const UNKNOWN_USER: User = User.fromPartial({
+ title: "<<Unknown user>>",
+ });
+
+ const UNKNOWN_ENVIRONMENT: Environment = Environment.fromPartial({
+ title: "<<Unknown environment>>",
+ });
+
+ switch (type) {
+ case "USER":
+ return UNKNOWN_USER;
+ case "ENVIRONMENT":
+ return UNKNOWN_ENVIRONMENT;
+ }
+};
+
+export const unknown = makeUnknown as ResourceMaker;
+
+const makeEmpty = (type: ResourceType) => {
+ const EMPTY_USER: User = User.fromPartial({});
+
+ const EMPTY_ENVIRONMENT: Environment = Environment.fromPartial({});
+
+ switch (type) {
+ case "USER":
+ return EMPTY_USER;
+ case "ENVIRONMENT":
+ return EMPTY_ENVIRONMENT;
+ }
+};
+
+export const empty = makeEmpty as ResourceMaker;