Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
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.gonew file mode 100644index 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.vueindex 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.tsindex 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 planconst 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.tsindex 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.vueindex 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", {email: "[email protected]",@@ -255,6 +259,7 @@ export default {currentUser,currentPlan,showDBAItem,+ showSwitchPlan,switchToOwner,switchToDBA,switchToDeveloper,diff --git a/server/acl.go b/server/acl.goindex 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.Handpath := 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.Handif !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.csvindex cb90081105..369ed44755 100644--- a/server/acl_casbin_policy_dba.csv+++ b/server/acl_casbin_policy_dba.csv@@ -48,4 +48,5 @@ p, DBA, /vcs, GETp, DBA, /vcs/{id}, GETp, DBA, /vcs/{id}, PATCHp, 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 filediff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csvindex 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, PATCHp, DEVELOPER, /sql/ping, POSTp, DEVELOPER, /vcs, GETp, DEVELOPER, /vcs/{id}, GET+p, DEVELOPER, /plan, GET\ No newline at end of filediff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csvindex c48ef65f50..bf2881d16b 100644--- a/server/acl_casbin_policy_owner.csv+++ b/server/acl_casbin_policy_owner.csv@@ -53,4 +53,6 @@ p, OWNER, /vcs, GETp, OWNER, /vcs/{id}, GETp, OWNER, /vcs/{id}, PATCHp, 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 filediff --git a/server/plan.go b/server/plan.gonew file mode 100644index 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.goindex 4a4a1f22a9..f085f6bd59 100644--- a/server/server.go+++ b/server/server.go@@ -49,6 +49,7 @@ type Server struct {startedTs int64secret stringdemo bool+ plan api.PlanType}//go:embed dist@@ -100,6 +101,7 @@ func NewServer(logger *zap.Logger, host string, port int, secret string, demo bostartedTs: 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 bos.registerBookmarkRoutes(apiGroup)s.registerSqlRoutes(apiGroup)s.registerVCSRoutes(apiGroup)+ s.registerPlanRoutes(apiGroup)allRoutes, err := json.MarshalIndent(e.Routes(), "", " ")if err != nil {