Skip to content

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

feat: find sheet with `PrincipalID` (#1121)

STEVEN2cf4d572d211
diff --git a/api/sheet.go b/api/sheet.go
index a3f8ee63f5..d25c6d0108 100644
--- a/api/sheet.go
+++ b/api/sheet.go
@@ -215,12 +215,10 @@ type SheetFind struct {
// Standard fields
RowStatus *RowStatus
- // Value is assigned from the jwt subject field passed by the client.
CreatorID *int
// Related fields
- ProjectID *int
- // Query all related sheets with databaseId can be used for database transfer checking.
+ ProjectID *int
DatabaseID *int
// Domain fields
@@ -229,6 +227,8 @@ type SheetFind struct {
Source *SheetSource
Type *SheetType
Payload *string
+ // If present, will only find sheet which related project containing PrincipalID as an active member.
+ PrincipalID *int
}
func (find *SheetFind) String() string {
diff --git a/server/sheet.go b/server/sheet.go
index b082573ee2..0b01571829 100644
--- a/server/sheet.go
+++ b/server/sheet.go
@@ -281,14 +281,11 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {
g.GET("/sheet", func(c echo.Context) error {
ctx := context.Background()
sheetFind := &api.SheetFind{}
- creatorID := c.Get(getPrincipalIDContextKey()).(int)
- sheetFind.CreatorID = &creatorID
- if rowStatusStr := c.QueryParam("rowstatus"); rowStatusStr != "" {
+ if rowStatusStr := c.QueryParam("rowStatus"); rowStatusStr != "" {
rowStatus := api.RowStatus(rowStatusStr)
sheetFind.RowStatus = &rowStatus
}
-
if projectIDStr := c.QueryParam("projectId"); projectIDStr != "" {
projectID, err := strconv.Atoi(projectIDStr)
if err != nil {
@@ -296,7 +293,6 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {
}
sheetFind.ProjectID = &projectID
}
-
if databaseIDStr := c.QueryParam("databaseId"); databaseIDStr != "" {
databaseID, err := strconv.Atoi(databaseIDStr)
if err != nil {
@@ -304,10 +300,22 @@ func (s *Server) registerSheetRoutes(g *echo.Group) {
}
sheetFind.DatabaseID = &databaseID
}
-
if visibility := api.SheetVisibility(c.QueryParam("visibility")); visibility != "" {
sheetFind.Visibility = &visibility
}
+ if creatorIDStr := c.QueryParam("creatorId"); creatorIDStr != "" {
+ creatorID, err := strconv.Atoi(creatorIDStr)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("creatorID ID is not a number: %s", c.QueryParam("creatorId"))).SetInternal(err)
+ }
+ sheetFind.CreatorID = &creatorID
+ }
+ // When getting private/project sheet list, we should set the PrincipalID to ensure only
+ // the sheet which related project containing PrincipalID as an active member could be found.
+ if sheetFind.Visibility != nil && (*sheetFind.Visibility == api.PrivateSheet || *sheetFind.Visibility == api.ProjectSheet) {
+ principalID := c.Get(getPrincipalIDContextKey()).(int)
+ sheetFind.PrincipalID = &principalID
+ }
sheetRawList, err := s.SheetService.FindSheetList(ctx, sheetFind)
if err != nil {
diff --git a/store/demo/dev/10220__sheet.sql b/store/demo/dev/10220__sheet.sql
index e09eab25d5..cd3cbf7ee7 100644
--- a/store/demo/dev/10220__sheet.sql
+++ b/store/demo/dev/10220__sheet.sql
@@ -1,4 +1,3 @@
--- Sheet for principal 101 "Demo Owner"
INSERT INTO
sheet (
id,
@@ -8,8 +7,7 @@ INSERT INTO
database_id,
name,
statement,
- source,
- type
+ visibility
)
VALUES
(
@@ -18,10 +16,74 @@ VALUES
101,
3001,
7019,
- 'my-sheet',
- 'SELECT * FROM demo',
- 'BYTEBASE',
- 'SQL'
+ 'My test cases',
+ 'SELECT * FROM test_case',
+ 'PRIVATE'
);
-ALTER SEQUENCE sheet_id_seq RESTART WITH 22002;
+INSERT INTO
+ sheet (
+ id,
+ row_status,
+ creator_id,
+ updater_id,
+ project_id,
+ name,
+ statement,
+ visibility
+ )
+VALUES
+ (
+ 22002,
+ 'ARCHIVED',
+ 101,
+ 101,
+ 3002,
+ 'My starred items',
+ 'SELECT * FROM item WHERE starred=true',
+ 'PRIVATE'
+ );
+
+INSERT INTO
+ sheet (
+ id,
+ creator_id,
+ updater_id,
+ project_id,
+ name,
+ statement,
+ visibility
+ )
+VALUES
+ (
+ 22003,
+ 102,
+ 102,
+ 3002,
+ 'All coupon of our shop',
+ 'SELECT * FROM coupon WHERE shop_id=101',
+ 'PROJECT'
+ );
+
+INSERT INTO
+ sheet (
+ id,
+ creator_id,
+ updater_id,
+ project_id,
+ name,
+ statement,
+ visibility
+ )
+VALUES
+ (
+ 22004,
+ 103,
+ 103,
+ 3003,
+ 'All blogs from Alex',
+ 'SELECT * FROM blog WHERE author="Alex"',
+ 'PUBLIC'
+ );
+
+ALTER SEQUENCE sheet_id_seq RESTART WITH 22005;
diff --git a/store/sheet.go b/store/sheet.go
index bfe230a5ae..9dae04d88b 100644
--- a/store/sheet.go
+++ b/store/sheet.go
@@ -388,6 +388,9 @@ func findSheetList(ctx context.Context, tx *sql.Tx, find *api.SheetFind, mode co
if v := find.Visibility; v != nil {
where, args = append(where, fmt.Sprintf("visibility = $%d", len(args)+1)), append(args, *v)
}
+ if v := find.PrincipalID; v != nil {
+ where, args = append(where, fmt.Sprintf("project_id IN (SELECT project_id FROM project_member WHERE principal_id = $%d)", len(args)+1)), append(args, *v)
+ }
if mode == common.ReleaseModeDev {
if v := find.Source; v != nil {
where, args = append(where, fmt.Sprintf("source = $%d", len(args)+1)), append(args, *v)