Skip to content

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

Add plan endpoint to get and change plan. Also treat incoming user role as owner if admin feature is not enabled.

天舟7acb1beb5421
diff --git a/api/plan.go b/api/plan.go
new file mode 100644
index 0000000000..ed4a448ddf
--- /dev/null
+++ b/api/plan.go
@@ -0,0 +1,44 @@
+package api
+
+type FeatureType string
+
+const (
+ FEATURE_ADMIN FeatureType = "bb.admin"
+ FEATURE_DBA_WORKFLOW FeatureType = "bb.dba-workflow"
+ FEATURE_DATA_SOURCE FeatureType = "bb.data-source"
+)
+
+func (e FeatureType) String() string {
+ switch e {
+ case FEATURE_ADMIN:
+ return "bb.admin"
+ case FEATURE_DBA_WORKFLOW:
+ return "bb.dba-workflow"
+ case FEATURE_DATA_SOURCE:
+ return "bb.data-source"
+ }
+ return ""
+}
+
+type PlanType int
+
+const (
+ FREE PlanType = iota
+ TEAM
+ ENTERPRISE
+)
+
+// A map from the a particular feature to the respective enablement of a particular plan
+var FEATURE_MATRIX = map[FeatureType][3]bool{
+ "bb.admin": {false, true, true},
+ "bb.dba-workflow": {false, false, true},
+ "bb.data-source": {false, false, false},
+}
+
+type Plan struct {
+ Type PlanType `jsonapi:"attr,type"`
+}
+
+type PlanPatch struct {
+ Type PlanType `jsonapi:"attr,type"`
+}
diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vue
index 987174d170..48004bb2e1 100644
--- a/frontend/src/components/ProvideDashboardContext.vue
+++ b/frontend/src/components/ProvideDashboardContext.vue
@@ -17,6 +17,8 @@ export default {
store.getters["auth/currentUser"]()
);
+ await store.dispatch("plan/fetchCurrentPlan");
+
await store.dispatch("actuator/info");
// Fetch member and principal list so PrincipalSelect can have the data.
diff --git a/frontend/src/store/modules/plan.ts b/frontend/src/store/modules/plan.ts
index 82845e121f..266c0adcb1 100644
--- a/frontend/src/store/modules/plan.ts
+++ b/frontend/src/store/modules/plan.ts
@@ -1,4 +1,5 @@
-import { FeatureType, PlanState, PlanType } from "../../types";
+import axios from "axios";
+import { FeatureType, PlanPatch, PlanState, PlanType } from "../../types";
// A map from the a particular feature to the respective enablement of a particular plan
const FEATURE_MATRIX: Map<FeatureType, boolean[]> = new Map([
@@ -24,9 +25,29 @@ const getters = {
};
const actions = {
+ async fetchCurrentPlan({ commit }: any): Promise<PlanType> {
+ const data = (await axios.get(`/api/plan`)).data.data;
+ const plan = data.attributes.type;
+ commit("setCurrentPlan", plan);
+ return plan;
+ },
+
async changePlan({ commit }: any, newPlan: PlanType) {
- commit("setCurrentPlan", newPlan);
- return newPlan;
+ const planPatch: PlanPatch = {
+ type: newPlan,
+ };
+ const data = (
+ await axios.patch(`/api/plan`, {
+ data: {
+ type: "planPatch",
+ attributes: planPatch,
+ },
+ })
+ ).data.data;
+
+ const updatedPlan = data.attributes.type;
+ commit("setCurrentPlan", updatedPlan);
+ return updatedPlan;
},
};
diff --git a/frontend/src/types/plan.ts b/frontend/src/types/plan.ts
index c9fdd6987a..7c72dead41 100644
--- a/frontend/src/types/plan.ts
+++ b/frontend/src/types/plan.ts
@@ -13,3 +13,7 @@ export enum PlanType {
TEAM = 1,
ENTERPRISE = 2,
}
+
+export type PlanPatch = {
+ type: PlanType;
+};
diff --git a/frontend/src/views/DashboardHeader.vue b/frontend/src/views/DashboardHeader.vue
index 631d2dc328..f97ce6f6af 100644
--- a/frontend/src/views/DashboardHeader.vue
+++ b/frontend/src/views/DashboardHeader.vue
@@ -37,7 +37,7 @@
<div>
<div class="flex items-center space-x-3">
<div
- v-if="isDev"
+ v-if="showSwitchPlan"
class="hidden md:flex sm:flex-row items-center space-x-2 text-sm"
>
<span class="hidden lg:block"
@@ -193,7 +193,7 @@ import { computed, reactive } from "vue";
import { useStore } from "vuex";
import ProfileDropdown from "../components/ProfileDropdown.vue";
import { PlanType } from "../types";
-import { isDBAOrOwner } from "../utils";
+import { isDBAOrOwner, isDev, isOwner } from "../utils";
interface LocalState {
showMobileMenu: boolean;
@@ -217,6 +217,10 @@ export default {
return isDBAOrOwner(currentUser.value.role);
});
+ const showSwitchPlan = computed((): boolean => {
+ return isDev() && isOwner(currentUser.value.role);
+ });
+
const switchToOwner = () => {
store.dispatch("auth/login", {
@@ -255,6 +259,7 @@ export default {
currentUser,
currentPlan,
showDBAItem,
+ showSwitchPlan,
switchToOwner,
switchToDBA,
switchToDeveloper,
diff --git a/server/acl.go b/server/acl.go
index b24740c81f..f31ac5a5af 100644
--- a/server/acl.go
+++ b/server/acl.go
@@ -102,8 +102,13 @@ func ACLMiddleware(l *zap.Logger, s *Server, ce *casbin.Enforcer, next echo.Hand
path := strings.TrimPrefix(c.Request().URL.Path, "/api")
+ role := member.Role
+ // If admin feature is not enabled, then we treat all user as OWNER.
+ if !s.feature("bb.admin") {
+ role = api.Owner
+ }
// Performs the ACL check.
- pass, err := ce.Enforce(member.Role.String(), path, method)
+ pass, err := ce.Enforce(role.String(), path, method)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
@@ -111,11 +116,11 @@ func ACLMiddleware(l *zap.Logger, s *Server, ce *casbin.Enforcer, next echo.Hand
if !pass {
return echo.NewHTTPError(http.StatusUnauthorized).SetInternal(
- fmt.Errorf("rejected by the ACL policy; %s %s u%d/%s", method, path, principalId, member.Role))
+ fmt.Errorf("rejected by the ACL policy; %s %s u%d/%s", method, path, principalId, role))
}
// Stores role into context.
- c.Set(GetRoleContextKey(), member.Role)
+ c.Set(GetRoleContextKey(), role)
return next(c)
}
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index cb90081105..369ed44755 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -48,4 +48,5 @@ p, DBA, /vcs, GET
p, DBA, /vcs/{id}, GET
p, DBA, /vcs/{id}, PATCH
p, DBA, /vcs/{id}, DELETE
-p, DBA, /vcs/{id}/repository, GET
\ No newline at end of file
+p, DBA, /vcs/{id}/repository, GET
+p, DBA, /plan, GET
\ No newline at end of file
diff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csv
index 7f166ba5b0..0bf7124028 100644
--- a/server/acl_casbin_policy_developer.csv
+++ b/server/acl_casbin_policy_developer.csv
@@ -38,3 +38,4 @@ p, DEVELOPER, /pipeline/{pipelineId}/task/{taskId}/status, PATCH
p, DEVELOPER, /sql/ping, POST
p, DEVELOPER, /vcs, GET
p, DEVELOPER, /vcs/{id}, GET
+p, DEVELOPER, /plan, GET
\ No newline at end of file
diff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csv
index c48ef65f50..bf2881d16b 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -53,4 +53,6 @@ p, OWNER, /vcs, GET
p, OWNER, /vcs/{id}, GET
p, OWNER, /vcs/{id}, PATCH
p, OWNER, /vcs/{id}, DELETE
-p, OWNER, /vcs/{id}/repository, GET
\ No newline at end of file
+p, OWNER, /vcs/{id}/repository, GET
+p, OWNER, /plan, GET
+p, OWNER, /plan, PATCH
\ No newline at end of file
diff --git a/server/plan.go b/server/plan.go
new file mode 100644
index 0000000000..bf14663c66
--- /dev/null
+++ b/server/plan.go
@@ -0,0 +1,44 @@
+package server
+
+import (
+ "net/http"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/google/jsonapi"
+ "github.com/labstack/echo/v4"
+)
+
+func (s *Server) registerPlanRoutes(g *echo.Group) {
+ g.GET("/plan", func(c echo.Context) error {
+ plan := &api.Plan{
+ Type: s.plan,
+ }
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, plan); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal plan response").SetInternal(err)
+ }
+ return nil
+ })
+
+ g.PATCH("/plan", func(c echo.Context) error {
+ planPatch := &api.PlanPatch{}
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, planPatch); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted update plan request").SetInternal(err)
+ }
+
+ s.plan = planPatch.Type
+
+ plan := &api.Plan{
+ Type: s.plan,
+ }
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, plan); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal plan response").SetInternal(err)
+ }
+ return nil
+ })
+}
+
+func (s *Server) feature(feature api.FeatureType) bool {
+ return api.FEATURE_MATRIX[feature][s.plan]
+}
diff --git a/server/server.go b/server/server.go
index 4a4a1f22a9..f085f6bd59 100644
--- a/server/server.go
+++ b/server/server.go
@@ -49,6 +49,7 @@ type Server struct {
startedTs int64
secret string
demo bool
+ plan api.PlanType
}
//go:embed dist
@@ -100,6 +101,7 @@ func NewServer(logger *zap.Logger, host string, port int, secret string, demo bo
startedTs: time.Now().Unix(),
secret: secret,
demo: demo,
+ plan: api.TEAM,
}
scheduler := NewTaskScheduler(logger, s)
@@ -167,6 +169,7 @@ func NewServer(logger *zap.Logger, host string, port int, secret string, demo bo
s.registerBookmarkRoutes(apiGroup)
s.registerSqlRoutes(apiGroup)
s.registerVCSRoutes(apiGroup)
+ s.registerPlanRoutes(apiGroup)
allRoutes, err := json.MarshalIndent(e.Routes(), "", " ")
if err != nil {