Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: add GET_SELF handler in `acl.go` (#548)
STEVEN92c29ec5834e
diff --git a/frontend/src/store/modules/inbox.ts b/frontend/src/store/modules/inbox.tsindex b91d95fe74..d514e7b1d4 100644--- a/frontend/src/store/modules/inbox.ts+++ b/frontend/src/store/modules/inbox.ts@@ -67,7 +67,7 @@ const actions = {readCreatedAfterTs,}: { userId: PrincipalId; readCreatedAfterTs?: number }) {- let url = `/api/inbox?user=${userId}`;+ let url = `/api/inbox/user/${userId}`;if (readCreatedAfterTs) {url += `&created=${readCreatedAfterTs}`;}@@ -81,7 +81,7 @@ const actions = {},async fetchInboxSummaryByUser({ commit }: any, userId: PrincipalId) {- const inboxSummary = (await axios.get(`/api/inbox/summary?user=${userId}`))+ const inboxSummary = (await axios.get(`/api/inbox/user/${userId}/summary`)).data;commit("setInboxSummaryByUser", { userId, inboxSummary });diff --git a/server/acl.go b/server/acl.goindex fcc7f631a2..0f64500e89 100644--- a/server/acl.go+++ b/server/acl.go@@ -58,10 +58,10 @@ func aclMiddleware(l *zap.Logger, s *Server, ce *casbin.Enforcer, next echo.Handreturn echo.NewHTTPError(http.StatusUnauthorized, "This user has been deactivated by the admin")}- // If the requests is trying to PATCH/DELETE herself, we will change the method signature to+ // If the request is trying to GET/PATCH/DELETE itself, we will change the method signature to// XXX_SELF so that the policy can differentiate between XXX and XXX_SELF- if method == "PATCH" || method == "DELETE" {- if isSelf, err := isUpdatingSelf(ctx, c, s, principalID); err != nil {+ if method == "GET" || method == "PATCH" || method == "DELETE" {+ if isSelf, err := isOperatingSelf(ctx, c, s, principalID, method); err != nil {return err} else if isSelf {method = method + "_SELF"@@ -94,6 +94,29 @@ func aclMiddleware(l *zap.Logger, s *Server, ce *casbin.Enforcer, next echo.Hand}}+func isOperatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalID int, method string) (bool, error) {+ if method == "GET" {+ return isGettingSelf(ctx, c, s, curPrincipalID)+ } else if method == "PATCH" || method == "DELETE" {+ return isUpdatingSelf(ctx, c, s, curPrincipalID)+ }++ return false, nil+}++func isGettingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalID int) (bool, error) {+ if strings.HasPrefix(c.Path(), "/api/inbox/user") {+ userID, err := strconv.Atoi(c.Param("userId"))+ if err != nil {+ return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("User ID is not a number: %s", c.Param("userId"))).SetInternal(err)+ }++ return userID == curPrincipalID, nil+ }++ return false, nil+}+func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalID int) (bool, error) {const defaultErrMsg = "Failed to process authorize request."if strings.HasPrefix(c.Path(), "/api/principal") {@@ -105,10 +128,9 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalif activityIDStr := c.Param("activityID"); activityIDStr != "" {activityID, err := strconv.Atoi(activityIDStr)if err != nil {- msg := "Activity ID is not a number: " + activityIDStr- httpErr := echo.NewHTTPError(http.StatusBadRequest, msg)- return false, httpErr+ return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Activity ID is not a number: %s"+activityIDStr)).SetInternal(err)}+activityFind := &api.ActivityFind{ID: &activityID,}@@ -119,16 +141,16 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalif activity == nil {return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Activity ID not found: %d", activityID))}+return activity.CreatorID == curPrincipalID, nil}} else if strings.HasPrefix(c.Path(), "/api/bookmark") {if bookmarkIDStr := c.Param("bookmarkID"); bookmarkIDStr != "" {bookmarkID, err := strconv.Atoi(bookmarkIDStr)if err != nil {- msg := "Bookmark ID is not a number: " + bookmarkIDStr- httpErr := echo.NewHTTPError(http.StatusBadRequest, msg)- return false, httpErr+ return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Bookmark ID is not a number: %s"+bookmarkIDStr)).SetInternal(err)}+bookmarkFind := &api.BookmarkFind{ID: &bookmarkID,}@@ -137,18 +159,18 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalreturn false, echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)}if bookmark == nil {- return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Bookmark ID not found: %d", bookmarkID))+ return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Bookmark ID not found: %d", bookmarkID)).SetInternal(err)}+return bookmark.CreatorID == curPrincipalID, nil}} else if strings.HasPrefix(c.Path(), "/api/inbox") {if inboxIDStr := c.Param("inboxID"); inboxIDStr != "" {inboxID, err := strconv.Atoi(inboxIDStr)if err != nil {- msg := "Inbox ID is not a number: " + inboxIDStr- httpErr := echo.NewHTTPError(http.StatusBadRequest, msg)- return false, httpErr+ return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Inbox ID is not a number: %s", inboxIDStr)).SetInternal(err)}+inboxFind := &api.InboxFind{ID: &inboxID,}@@ -157,16 +179,18 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalreturn false, echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)}if inbox == nil {- return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Inbox ID not found: %d", inboxID))+ return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Inbox ID not found: %d", inboxID)).SetInternal(err)}+return inbox.ReceiverID == curPrincipalID, nil}} else if strings.HasPrefix(c.Path(), "/api/savedquery") {if idStr := c.Param("id"); idStr != "" {id, err := strconv.Atoi(idStr)if err != nil {- return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Saved query ID is not a number: %s", idStr))+ return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Saved query ID is not a number: %s", idStr)).SetInternal(err)}+savedQueryFind := &api.SavedQueryFind{ID: &id,}@@ -175,16 +199,18 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalreturn false, echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)}if savedQuery == nil {- return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Saved query ID not found: %d", id))+ return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Saved query ID not found: %d", id)).SetInternal(err)}+return savedQuery.CreatorID == curPrincipalID, nil}} else if strings.HasPrefix(c.Path(), "/api/sheet") {if idStr := c.Param("id"); idStr != "" {id, err := strconv.Atoi(idStr)if err != nil {- return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Sheet ID is not a number: %s", idStr))+ return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Sheet ID is not a number: %s", idStr)).SetInternal(err)}+sheetFind := &api.SheetFind{ID: &id,}@@ -193,10 +219,12 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalreturn false, echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)}if sheet == nil {- return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Sheet ID not found: %d", id))+ return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Sheet ID not found: %d", id)).SetInternal(err)}+return sheet.CreatorID == curPrincipalID, nil}}+return false, nil}diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csvindex 9f13215b87..0a9d96a57a 100644--- a/server/acl_casbin_policy_dba.csv+++ b/server/acl_casbin_policy_dba.csv@@ -57,8 +57,8 @@ p, DBA, /activity, POSTp, DBA, /activity, GETp, DBA, /activity/{id}, PATCH_SELFp, DBA, /activity/{id}, DELETE_SELF-p, DBA, /inbox, GET-p, DBA, /inbox/summary, GET+p, DBA, /inbox/user/{userID}, GET_SELF+p, DBA, /inbox/user/{userID}/summary, GET_SELFp, DBA, /inbox/{id}, PATCH_SELFp, DBA, /bookmark, POSTp, DBA, /bookmark, GET@@ -91,4 +91,4 @@ p, DBA, /sheet, POSTp, DBA, /sheet, GETp, DBA, /sheet/{id}, GETp, DBA, /sheet/{id}, PATCH-p, DBA, /sheet/{id}, DELETE_SELF\ No newline at end of file+p, DBA, /sheet/{id}, DELETE_SELFdiff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csvindex 36ce000c21..315b5fdee8 100644--- a/server/acl_casbin_policy_developer.csv+++ b/server/acl_casbin_policy_developer.csv@@ -53,8 +53,8 @@ p, DEVELOPER, /activity, POSTp, DEVELOPER, /activity, GETp, DEVELOPER, /activity/{id}, PATCH_SELFp, DEVELOPER, /activity/{id}, DELETE_SELF-p, DEVELOPER, /inbox, GET-p, DEVELOPER, /inbox/summary, GET+p, DEVELOPER, /inbox/user/{userID}, GET_SELF+p, DEVELOPER, /inbox/user/{userID}/summary, GET_SELFp, DEVELOPER, /inbox/{id}, PATCH_SELFp, DEVELOPER, /bookmark, POSTp, DEVELOPER, /bookmark, GET@@ -80,4 +80,4 @@ p, DEVELOPER, /sheet, POSTp, DEVELOPER, /sheet, GETp, DEVELOPER, /sheet/{id}, GETp, DEVELOPER, /sheet/{id}, PATCH-p, DEVELOPER, /sheet/{id}, DELETE_SELF\ No newline at end of file+p, DEVELOPER, /sheet/{id}, DELETE_SELFdiff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csvindex fa96c6b92b..80521200ec 100644--- a/server/acl_casbin_policy_owner.csv+++ b/server/acl_casbin_policy_owner.csv@@ -62,8 +62,8 @@ p, OWNER, /activity, POSTp, OWNER, /activity, GETp, OWNER, /activity/{id}, PATCH_SELFp, OWNER, /activity/{id}, DELETE_SELF-p, OWNER, /inbox, GET-p, OWNER, /inbox/summary, GET+p, OWNER, /inbox/user/{userID}, GET_SELF+p, OWNER, /inbox/user/{userID}/summary, GET_SELFp, OWNER, /inbox/{id}, PATCH_SELFp, OWNER, /bookmark, POSTp, OWNER, /bookmark, GET@@ -97,4 +97,4 @@ p, OWNER, /sheet, POSTp, OWNER, /sheet, GETp, OWNER, /sheet/{id}, GETp, OWNER, /sheet/{id}, PATCH-p, OWNER, /sheet/{id}, DELETE_SELF\ No newline at end of file+p, OWNER, /sheet/{id}, DELETE_SELFdiff --git a/server/inbox.go b/server/inbox.goindex e9cfd983b2..b456885f0b 100644--- a/server/inbox.go+++ b/server/inbox.go@@ -13,17 +13,17 @@ import ()func (s *Server) registerInboxRoutes(g *echo.Group) {- g.GET("/inbox", func(c echo.Context) error {+ g.GET("/inbox/user/:userId", func(c echo.Context) error {ctx := context.Background()- inboxFind := &api.InboxFind{}- userIDStr := c.QueryParams().Get("user")- if userIDStr != "" {- userID, err := strconv.Atoi(userIDStr)- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query parameter user is not a number: %s", userIDStr)).SetInternal(err)- }- inboxFind.ReceiverID = &userID+ 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)}++ inboxFind := &api.InboxFind{+ ReceiverID: &userID,+ }+createdAfterStr := c.QueryParams().Get("created")if createdAfterStr != "" {createdTs, err := strconv.ParseInt(createdAfterStr, 10, 64)@@ -50,15 +50,11 @@ func (s *Server) registerInboxRoutes(g *echo.Group) {return nil})- g.GET("/inbox/summary", func(c echo.Context) error {+ g.GET("/inbox/user/:userId/summary", func(c echo.Context) error {ctx := context.Background()- userIDStr := c.QueryParams().Get("user")- if userIDStr == "" {- return echo.NewHTTPError(http.StatusBadRequest, "Missing query parameter user")- }- userID, err := strconv.Atoi(userIDStr)+ userID, err := strconv.Atoi(c.Param("userId"))if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query parameter user is not a number: %s", userIDStr)).SetInternal(err)+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("User ID is not a number: %s", c.Param("userId"))).SetInternal(err)}summary, err := s.InboxService.FindInboxSummary(ctx, userID)