Skip to content

Commit 8850b12

Browse files
committed
inject authorize filter properly
1 parent 3aa72a9 commit 8850b12

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

coderd/database/modelqueries.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strings"
78

89
"github.com/lib/pq"
910

@@ -164,8 +165,11 @@ type workspaceQuerier interface {
164165
// This code is copied from `GetWorkspaces` and adds the authorized filter WHERE
165166
// clause.
166167
func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, authorizedFilter rbac.AuthorizeFilter) ([]Workspace, error) {
168+
// In order to properly use ORDER BY, OFFSET, and LIMIT, we need to inject the
169+
// authorizedFilter between the end of the where claus and those statements.
170+
filter := strings.Replace(getWorkspaces, "-- @authorize_filter", fmt.Sprintf(" AND %s", authorizedFilter.SQLString(rbac.NoACLConfig())), 1)
167171
// The name comment is for metric tracking
168-
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s AND %s", getWorkspaces, authorizedFilter.SQLString(rbac.NoACLConfig()))
172+
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s", filter)
169173
rows, err := q.db.QueryContext(ctx, query,
170174
arg.Limit,
171175
arg.Offset,

coderd/database/queries.sql.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ WHERE
132132
name ILIKE '%' || @name || '%'
133133
ELSE true
134134
END
135+
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
136+
-- @authorize_filter
135137
ORDER BY
136138
last_used_at DESC
137139
LIMIT

0 commit comments

Comments
 (0)