-
Notifications
You must be signed in to change notification settings - Fork 982
feat: Convert rego queries into SQL clauses #4225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
ff931dc
e535e5a
8f92953
cb5d519
2bd0165
364498c
b2da580
d516be7
125931a
fc58da5
04c1d6a
98b405e
6ad0b51
7cfad87
33c9d34
f10e9b7
d89c4d2
3828c29
913fb27
3e2fbb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/coderd/rbac" | ||
) | ||
|
||
type customQuerier interface { | ||
AuthorizedGetWorkspaces(ctx context.Context, arg GetWorkspacesParams, authorizedFilter rbac.AuthorizeFilter) ([]Workspace, error) | ||
} | ||
|
||
// AuthorizedGetWorkspaces returns all workspaces that the user is authorized to access. | ||
// This code is copied from `GetWorkspaces` and adds the authorized filter WHERE | ||
// clause. | ||
func (q *sqlQuerier) AuthorizedGetWorkspaces(ctx context.Context, arg GetWorkspacesParams, authorizedFilter rbac.AuthorizeFilter) ([]Workspace, error) { | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might change the wording of this. Prefixing with Thoughts on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for us to get rid of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kylecarbs that is a good question. For system level things there might be a need? But we can always add back "GetWorkspaces" later. I was using SQLc's generated code to make this function. To get rid of |
||
query := fmt.Sprintf("%s AND %s", getWorkspaces, authorizedFilter.SQLString(rbac.DefaultConfig())) | ||
rows, err := q.db.QueryContext(ctx, query, | ||
arg.Deleted, | ||
arg.OwnerID, | ||
arg.OwnerUsername, | ||
arg.TemplateName, | ||
pq.Array(arg.TemplateIds), | ||
arg.Name, | ||
) | ||
if err != nil { | ||
return nil, xerrors.Errorf("get authorized workspaces: %w", err) | ||
} | ||
defer rows.Close() | ||
var items []Workspace | ||
for rows.Next() { | ||
var i Workspace | ||
if err := rows.Scan( | ||
&i.ID, | ||
&i.CreatedAt, | ||
&i.UpdatedAt, | ||
&i.OwnerID, | ||
&i.OrganizationID, | ||
&i.TemplateID, | ||
&i.Deleted, | ||
&i.Name, | ||
&i.AutostartSchedule, | ||
&i.Ttl, | ||
&i.LastUsedAt, | ||
); err != nil { | ||
return nil, err | ||
} | ||
items = append(items, i) | ||
} | ||
if err := rows.Close(); err != nil { | ||
return nil, err | ||
} | ||
if err := rows.Err(); err != nil { | ||
return nil, err | ||
} | ||
return items, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish we could add hooks into SQLc to do this. Kinda sucks to have to copy the original function and call it like this.