Skip to content

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

Move restoreUser from router to the initial ProvideAppContext

天舟320c4c8aed37
diff --git a/frontend/src/components/ProvideAppContext.vue b/frontend/src/components/ProvideAppContext.vue
index aa3891afeb..df05ae6c14 100644
--- a/frontend/src/components/ProvideAppContext.vue
+++ b/frontend/src/components/ProvideAppContext.vue
@@ -10,7 +10,13 @@ export default {
async setup() {
const store = useStore();
- await Promise.all([store.dispatch("actuator/info")]);
+ // Restore login user first.
+ await store.dispatch("auth/restoreUser");
+
+ await Promise.all([
+ store.dispatch("actuator/info"),
+ store.dispatch("plan/fetchCurrentPlan"),
+ ]);
},
};
</script>
diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vue
index 76e186b08e..6df0717012 100644
--- a/frontend/src/components/ProvideDashboardContext.vue
+++ b/frontend/src/components/ProvideDashboardContext.vue
@@ -18,7 +18,6 @@ export default {
);
await Promise.all([
- store.dispatch("plan/fetchCurrentPlan"),
// Fetch member and principal list so PrincipalSelect can have the data.
store.dispatch("member/fetchMemberList"),
store.dispatch("principal/fetchPrincipalList"),
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 5c5d70c66b..aa87031c68 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -684,262 +684,248 @@ router.beforeEach((to, from, next) => {
}
}
- const checkRouteAuth = (user: Principal) => {
- if (to.name?.toString().startsWith("setting.workspace.version-control")) {
- if (isDBAOrOwner(user.role)) {
- next();
- } else {
- next({
- name: "error.403",
- replace: false,
- });
- }
+ const currentUser = store.getters["auth/currentUser"]();
+
+ if (to.name?.toString().startsWith("setting.workspace.version-control")) {
+ if (isDBAOrOwner(currentUser.role)) {
+ next();
+ } else {
+ next({
+ name: "error.403",
+ replace: false,
+ });
+ }
+ return;
+ }
+
+ if (to.name === "workspace.instance") {
+ if (
+ !store.getters["plan/feature"]("bb.dba-workflow") ||
+ isDBAOrOwner(currentUser.role)
+ ) {
+ next();
+ } else {
+ next({
+ name: "error.403",
+ replace: false,
+ });
+ }
+ return;
+ }
+
+ if (to.name === "workspace.database.create") {
+ if (
+ !store.getters["plan/feature"]("bb.dba-workflow") ||
+ isDBAOrOwner(currentUser.role)
+ ) {
+ next();
+ } else {
+ next({
+ name: "error.403",
+ replace: false,
+ });
+ }
+ return;
+ }
+
+ if (to.name?.toString().startsWith("workspace.database.datasource")) {
+ if (
+ !store.getters["plan/feature"]("bb.data-source") ||
+ !isDBAOrOwner(currentUser.role)
+ ) {
+ next({
+ name: "error.403",
+ replace: false,
+ });
return;
}
+ }
- if (to.name === "workspace.instance") {
- if (
- !store.getters["plan/feature"]("bb.dba-workflow") ||
- isDBAOrOwner(user.role)
- ) {
+ if (
+ to.name === "error.403" ||
+ to.name === "error.404" ||
+ to.name === "error.500" ||
+ to.name === "oauth-callback" ||
+ to.name === "workspace.home" ||
+ // to.name === "workspace.inbox" ||
+ to.name === "workspace.project" ||
+ to.name === "workspace.database" ||
+ to.name === "workspace.archive" ||
+ to.name === "workspace.environment" ||
+ (to.name?.toString().startsWith("setting") &&
+ to.name?.toString() != "setting.workspace.version-control.detail")
+ ) {
+ next();
+ return;
+ }
+
+ // We may just change the anchor (e.g. in Issue Detail view), thus we don't need
+ // to fetch the data to verify its existence since we have already verified before.
+ if (to.name == from.name) {
+ next();
+ return;
+ }
+
+ const routerSlug = store.getters["router/routeSlug"](to);
+ const principalId = routerSlug.principalId;
+ const environmentSlug = routerSlug.environmentSlug;
+ const projectSlug = routerSlug.projectSlug;
+ const issueSlug = routerSlug.issueSlug;
+ const instanceSlug = routerSlug.instanceSlug;
+ const databaseSlug = routerSlug.databaseSlug;
+ const dataSourceSlug = routerSlug.dataSourceSlug;
+ const vcsSlug = routerSlug.vcsSlug;
+
+ if (principalId) {
+ store
+ .dispatch("principal/fetchPrincipalById", principalId)
+ .then(() => {
next();
- } else {
+ })
+ .catch((error) => {
next({
- name: "error.403",
+ name: "error.404",
replace: false,
});
- }
+ throw error;
+ });
+ return;
+ }
+
+ if (environmentSlug) {
+ if (
+ store.getters["environment/environmentById"](idFromSlug(environmentSlug))
+ ) {
+ next();
return;
}
+ next({
+ name: "error.404",
+ replace: false,
+ });
+ }
- if (to.name === "workspace.database.create") {
- if (
- !store.getters["plan/feature"]("bb.dba-workflow") ||
- isDBAOrOwner(user.role)
- ) {
+ if (projectSlug) {
+ store
+ .dispatch("project/fetchProjectById", idFromSlug(projectSlug))
+ .then(() => {
next();
- } else {
+ })
+ .catch((error) => {
next({
- name: "error.403",
+ name: "error.404",
replace: false,
});
+ throw error;
+ });
+ return;
+ }
+
+ if (issueSlug) {
+ if (issueSlug.toLowerCase() == "new") {
+ // For prepraing the database if user visits creating issue url directly.
+ if (to.query.databaseList) {
+ for (const databaseId of (to.query.databaseList as string).split(",")) {
+ store.dispatch("database/fetchDatabaseById", { databaseId });
+ }
}
+ next();
return;
}
-
- if (to.name?.toString().startsWith("workspace.database.datasource")) {
- if (
- !store.getters["plan/feature"]("bb.data-source") ||
- !isDBAOrOwner(user.role)
- ) {
+ store
+ .dispatch("issue/fetchIssueById", idFromSlug(issueSlug))
+ .then(() => {
+ next();
+ })
+ .catch((error) => {
next({
- name: "error.403",
+ name: "error.404",
replace: false,
});
- return;
- }
- }
+ throw error;
+ });
+ return;
+ }
+ if (databaseSlug) {
if (
- to.name === "error.403" ||
- to.name === "error.404" ||
- to.name === "error.500" ||
- to.name === "oauth-callback" ||
- to.name === "workspace.home" ||
- // to.name === "workspace.inbox" ||
- to.name === "workspace.project" ||
- to.name === "workspace.database" ||
- to.name === "workspace.archive" ||
- to.name === "workspace.environment" ||
- (to.name?.toString().startsWith("setting") &&
- to.name?.toString() != "setting.workspace.version-control.detail")
+ databaseSlug.toLowerCase() == "new" ||
+ databaseSlug.toLowerCase() == "grant"
) {
next();
return;
}
-
- // We may just change the anchor (e.g. in Issue Detail view), thus we don't need
- // to fetch the data to verify its existence since we have already verified before.
- if (to.name == from.name) {
- next();
- return;
- }
-
- const routerSlug = store.getters["router/routeSlug"](to);
- const principalId = routerSlug.principalId;
- const environmentSlug = routerSlug.environmentSlug;
- const projectSlug = routerSlug.projectSlug;
- const issueSlug = routerSlug.issueSlug;
- const instanceSlug = routerSlug.instanceSlug;
- const databaseSlug = routerSlug.databaseSlug;
- const dataSourceSlug = routerSlug.dataSourceSlug;
- const vcsSlug = routerSlug.vcsSlug;
-
- if (principalId) {
- store
- .dispatch("principal/fetchPrincipalById", principalId)
- .then(() => {
+ store
+ .dispatch("database/fetchDatabaseById", {
+ databaseId: idFromSlug(databaseSlug),
+ })
+ .then((database) => {
+ if (!dataSourceSlug) {
next();
- })
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
+ } else {
+ store
+ .dispatch("dataSource/fetchDataSourceById", {
+ dataSourceId: idFromSlug(dataSourceSlug),
+ databaseId: database.id,
+ })
+ .then(() => {
+ next();
+ })
+ .catch((error) => {
+ next({
+ name: "error.404",
+ replace: false,
+ });
+ throw error;
+ });
+ }
+ })
+ .catch((error) => {
+ next({
+ name: "error.404",
+ replace: false,
});
- return;
- }
-
- if (environmentSlug) {
- if (
- store.getters["environment/environmentById"](
- idFromSlug(environmentSlug)
- )
- ) {
- next();
- return;
- }
- next({
- name: "error.404",
- replace: false,
+ throw error;
});
- }
-
- if (projectSlug) {
- store
- .dispatch("project/fetchProjectById", idFromSlug(projectSlug))
- .then(() => {
- next();
- })
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
- });
- return;
- }
+ return;
+ }
- if (issueSlug) {
- if (issueSlug.toLowerCase() == "new") {
- // For prepraing the database if user visits creating issue url directly.
- if (to.query.databaseList) {
- for (const databaseId of (to.query.databaseList as string).split(
- ","
- )) {
- store.dispatch("database/fetchDatabaseById", { databaseId });
- }
- }
+ if (instanceSlug) {
+ store
+ .dispatch("instance/fetchInstanceById", idFromSlug(instanceSlug))
+ .then(() => {
next();
- return;
- }
- store
- .dispatch("issue/fetchIssueById", idFromSlug(issueSlug))
- .then(() => {
- next();
- })
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
+ })
+ .catch((error) => {
+ next({
+ name: "error.404",
+ replace: false,
});
- return;
- }
+ throw error;
+ });
+ return;
+ }
- if (databaseSlug) {
- if (
- databaseSlug.toLowerCase() == "new" ||
- databaseSlug.toLowerCase() == "grant"
- ) {
+ if (vcsSlug) {
+ store
+ .dispatch("vcs/fetchVCSById", idFromSlug(vcsSlug))
+ .then(() => {
next();
- return;
- }
- store
- .dispatch("database/fetchDatabaseById", {
- databaseId: idFromSlug(databaseSlug),
- })
- .then((database) => {
- if (!dataSourceSlug) {
- next();
- } else {
- store
- .dispatch("dataSource/fetchDataSourceById", {
- dataSourceId: idFromSlug(dataSourceSlug),
- databaseId: database.id,
- })
- .then(() => {
- next();
- })
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
- });
- }
- })
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
- });
- return;
- }
-
- if (instanceSlug) {
- store
- .dispatch("instance/fetchInstanceById", idFromSlug(instanceSlug))
- .then(() => {
- next();
- })
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
- });
- return;
- }
-
- if (vcsSlug) {
- store
- .dispatch("vcs/fetchVCSById", idFromSlug(vcsSlug))
- .then(() => {
- next();
- })
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
+ })
+ .catch((error) => {
+ next({
+ name: "error.404",
+ replace: false,
});
- return;
- }
-
- next({
- name: "error.404",
- replace: false,
- });
- };
-
- const loginUser: Principal = store.getters["auth/currentUser"]();
- // On displaying the first page, we need to do an async restore to fetch the lastest user from the server.
- if (loginUser.id == UNKNOWN_ID) {
- store.dispatch("auth/restoreUser").then((user: Principal) => {
- checkRouteAuth(user);
- });
- } else {
- checkRouteAuth(loginUser);
+ throw error;
+ });
+ return;
}
+
+ next({
+ name: "error.404",
+ replace: false,
+ });
});
router.afterEach((to, from) => {
diff --git a/server/acl.go b/server/acl.go
index 1e5dbe729d..1a3131ce9f 100644
--- a/server/acl.go
+++ b/server/acl.go
@@ -25,7 +25,7 @@ func GetRoleContextKey() string {
func ACLMiddleware(l *zap.Logger, s *Server, ce *casbin.Enforcer, next echo.HandlerFunc, readonly bool) echo.HandlerFunc {
return func(c echo.Context) error {
- // Skips auth, actuator and plan
+ // Skips auth, actuator, plan
if strings.HasPrefix(c.Path(), "/api/auth") || strings.HasPrefix(c.Path(), "/api/actuator") || strings.HasPrefix(c.Path(), "/api/plan") {
return next(c)
}
diff --git a/server/jwt.go b/server/jwt.go
index 30fae13dcc..e633b70b8f 100644
--- a/server/jwt.go
+++ b/server/jwt.go
@@ -144,8 +144,8 @@ func setUserCookie(c echo.Context, user *api.Principal, expiration time.Time) {
// will try to generate new access token and refresh token.
func JWTMiddleware(l *zap.Logger, p api.PrincipalService, next echo.HandlerFunc, mode string, secret string) echo.HandlerFunc {
return func(c echo.Context) error {
- // Skips auth and actuator
- if strings.HasPrefix(c.Path(), "/api/auth") || strings.HasPrefix(c.Path(), "/api/actuator") {
+ // Skips auth, actuator, plan
+ if strings.HasPrefix(c.Path(), "/api/auth") || strings.HasPrefix(c.Path(), "/api/actuator") || strings.HasPrefix(c.Path(), "/api/plan") {
return next(c)
}