Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: update instance user related apis (#2306)
boojack7adcf013acb8
diff --git a/api/instance_user.go b/api/instance_user.goindex 2a1bf25810..185bb7c234 100644--- a/api/instance_user.go+++ b/api/instance_user.go@@ -31,7 +31,10 @@ type InstanceUserUpsert struct {// InstanceUserFind is the API message for finding instance users.type InstanceUserFind struct {- InstanceID int+ ID *int++ // Related fields+ InstanceID *int}func (find *InstanceUserFind) String() string {diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csvindex 24b8298276..4ec143dc60 100644--- a/server/acl_casbin_policy_dba.csv+++ b/server/acl_casbin_policy_dba.csv@@ -34,6 +34,7 @@ p, DBA, /instance, GETp, DBA, /instance/{id}, GETp, DBA, /instance/{id}, PATCHp, DBA, /instance/{id}/user, GET+p, DBA, /instance/{id}/user/{userID}, GETp, DBA, /instance/{id}/migration, POSTp, DBA, /instance/{id}/migration/status, GETp, DBA, /instance/{id}/migration/history, GETdiff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csvindex 320c35f0d1..d2440118cd 100644--- a/server/acl_casbin_policy_developer.csv+++ b/server/acl_casbin_policy_developer.csv@@ -28,6 +28,7 @@ p, DEVELOPER, /policy/environment/{environmentID}, GETp, DEVELOPER, /instance, GETp, DEVELOPER, /instance/{id}, GETp, DEVELOPER, /instance/{id}/user, GET+p, DEVELOPER, /instance/{id}/user/{userID}, GETp, DEVELOPER, /instance/{id}/migration/status, GETp, DEVELOPER, /instance/{id}/migration/history, GETp, DEVELOPER, /instance/{id}/migration/history/{historyID}, GETdiff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csvindex a04a77d381..73b1d4fe04 100644--- a/server/acl_casbin_policy_owner.csv+++ b/server/acl_casbin_policy_owner.csv@@ -38,6 +38,7 @@ p, OWNER, /instance, GETp, OWNER, /instance/{id}, GETp, OWNER, /instance/{id}, PATCHp, OWNER, /instance/{id}/user, GET+p, OWNER, /instance/{id}/user/{userID}, GETp, OWNER, /instance/{id}/migration, POSTp, OWNER, /instance/{id}/migration/status, GETp, OWNER, /instance/{id}/migration/history, GETdiff --git a/server/instance.go b/server/instance.goindex 19f498bd8c..b566b0997d 100644--- a/server/instance.go+++ b/server/instance.go@@ -229,6 +229,36 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {return nil})+ g.GET("/instance/:instanceID/user/:userID", func(c echo.Context) error {+ ctx := c.Request().Context()+ instanceID, err := strconv.Atoi(c.Param("instanceID"))+ if err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Instance ID is not a number: %s", c.Param("instanceID"))).SetInternal(err)+ }+ userID, err := strconv.Atoi(c.Param("userID"))+ if err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("User ID is not a number: %s", c.Param("userID"))).SetInternal(err)+ }++ instanceUserFind := &api.InstanceUserFind{+ ID: &userID,+ InstanceID: &instanceID,+ }+ instanceUser, err := s.store.GetInstanceUser(ctx, instanceUserFind)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance user with instanceID: %v and userID: %v", instanceID, userID)).SetInternal(err)+ }+ if instanceUser == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("instanceUser not found with instanceID: %v and userID: %v", instanceID, userID))+ }++ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)+ if err := jsonapi.MarshalPayload(c.Response().Writer, instanceUser); err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance user with instanceID: %v and userID: %v", instanceID, userID)).SetInternal(err)+ }+ return nil+ })+g.POST("/instance/:instanceID/migration", func(c echo.Context) error {ctx := c.Request().Context()id, err := strconv.Atoi(c.Param("instanceID"))diff --git a/store/instance_user.go b/store/instance_user.goindex 8d0bcfd4d5..dd70fba0cc 100644--- a/store/instance_user.go+++ b/store/instance_user.go@@ -3,10 +3,12 @@ package storeimport ("context""database/sql"+ "fmt""strings""github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common"+ "github.com/pkg/errors")// UpsertInstanceUser would update the existing user if name matches.@@ -29,10 +31,31 @@ func (s *Store) UpsertInstanceUser(ctx context.Context, upsert *api.InstanceUserreturn instanceUser, nil}+// GetInstanceUser gets an instance of IntanceUser.+func (s *Store) GetInstanceUser(ctx context.Context, find *api.InstanceUserFind) (*api.InstanceUser, error) {+ tx, err := s.db.BeginTx(ctx, nil)+ if err != nil {+ return nil, FormatError(err)+ }+ defer tx.PTx.Rollback()++ list, err := findInstanceUserImpl(ctx, tx.PTx, find)+ if err != nil {+ return nil, err+ }++ if len(list) == 0 {+ return nil, nil+ } else if len(list) > 1 {+ return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d instance users with filter %+v, expect 1", len(list), find)}+ }+ return list[0], nil+}+// FindInstanceUserByInstanceID retrieves a list of instanceUsers based on find.func (s *Store) FindInstanceUserByInstanceID(ctx context.Context, id int) ([]*api.InstanceUser, error) {find := &api.InstanceUserFind{- InstanceID: id,+ InstanceID: &id,}tx, err := s.db.BeginTx(ctx, nil)@@ -109,7 +132,13 @@ func upsertInstanceUserImpl(ctx context.Context, tx *sql.Tx, upsert *api.Instancfunc findInstanceUserImpl(ctx context.Context, tx *sql.Tx, find *api.InstanceUserFind) ([]*api.InstanceUser, error) {// Build WHERE clause.where, args := []string{"1 = 1"}, []interface{}{}- where, args = append(where, "instance_id = $1"), append(args, find.InstanceID)++ if v := find.ID; v != nil {+ where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)+ }+ if v := find.InstanceID; v != nil {+ where, args = append(where, fmt.Sprintf("instance_id = $%d", len(args)+1)), append(args, *v)+ }rows, err := tx.QueryContext(ctx, `SELECT