Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
dbauthz and dbmem impls
  • Loading branch information
aslilac committed May 15, 2024
commit 06378e4851828d217d15e85359644c03d244c8eb
9 changes: 8 additions & 1 deletion coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ func (q *querier) DeleteOldWorkspaceAgentStats(ctx context.Context) error {
return q.db.DeleteOldWorkspaceAgentStats(ctx)
}

func (q *querier) DeleteOrganization(ctx context.Context, id uuid.UUID) error {
return deleteQ[database.Organization](q.log, q.auth, q.db.GetOrganizationByID, q.db.DeleteOrganization)(ctx, id)
}

func (q *querier) DeleteReplicasUpdatedBefore(ctx context.Context, updatedAt time.Time) error {
if err := q.authorizeContext(ctx, policy.ActionDelete, rbac.ResourceSystem); err != nil {
return err
Expand Down Expand Up @@ -2754,7 +2758,10 @@ func (q *querier) UpdateOAuth2ProviderAppSecretByID(ctx context.Context, arg dat
}

func (q *querier) UpdateOrganization(ctx context.Context, arg database.UpdateOrganizationParams) (database.Organization, error) {
panic("not implemented")
fetch := func(ctx context.Context, arg database.UpdateOrganizationParams) (database.Organization, error) {
return q.db.GetOrganizationByID(ctx, arg.ID)
}
return updateWithReturn(q.log, q.auth, fetch, q.db.UpdateOrganization)(ctx, arg)
}

func (q *querier) UpdateProvisionerDaemonLastSeenAt(ctx context.Context, arg database.UpdateProvisionerDaemonLastSeenAtParams) error {
Expand Down
19 changes: 18 additions & 1 deletion coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,16 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
return nil
}

func (q *FakeQuerier) DeleteOrganization(ctx context.Context, id uuid.UUID) error {
for i, org := range q.organizations {
if org.ID == id {
q.organizations = append(q.organizations[:i], q.organizations[i+1:]...)
return nil
}
}
return sql.ErrNoRows
}

func (q *FakeQuerier) DeleteReplicasUpdatedBefore(_ context.Context, before time.Time) error {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down Expand Up @@ -7149,7 +7159,14 @@ func (q *FakeQuerier) UpdateOrganization(ctx context.Context, arg database.Updat
return database.Organization{}, err
}

panic("not implemented")
for i, org := range q.organizations {
if org.ID == arg.ID {
org.Name = arg.Name
q.organizations[i] = org
return org, nil
}
}
return database.Organization{}, sql.ErrNoRows
}

func (q *FakeQuerier) UpdateProvisionerDaemonLastSeenAt(_ context.Context, arg database.UpdateProvisionerDaemonLastSeenAtParams) error {
Expand Down
7 changes: 7 additions & 0 deletions coderd/database/dbmetrics/dbmetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions coderd/database/dbmock/dbmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion coderd/database/queries/organizations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ VALUES
-- If no organizations exist, and this is the first, make it the default.
($1, $2, $3, $4, $5, (SELECT TRUE FROM organizations LIMIT 1) IS NULL) RETURNING *;


-- name: UpdateOrganization :one
UPDATE
organizations
Expand All @@ -64,3 +63,10 @@ SET
WHERE
id = $1
RETURNING *;

-- name: DeleteOrganization :exec
DELETE FROM
organizations
WHERE
id = $1 AND
is_default = false;
1 change: 0 additions & 1 deletion coderd/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
// @Router /organizations/{organization} [patch]
func (api *API) patchOrganization(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
apiKey := httpmw.APIKey(r)

var req codersdk.PatchOrganizationRequest
if !httpapi.Read(ctx, rw, r, &req) {
Expand Down
4 changes: 2 additions & 2 deletions coderd/rbac/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type Object struct {
func (z Object) ValidAction(action policy.Action) error {
perms, ok := policy.RBACPermissions[z.Type]
if !ok {
return fmt.Errorf("invalid type %q", z.Type)
return xerrors.Errorf("invalid type %q", z.Type)
}
if _, ok := perms.Actions[action]; !ok {
return fmt.Errorf("invalid action %q for type %q", action, z.Type)
return xerrors.Errorf("invalid action %q for type %q", action, z.Type)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion coderd/rbac/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestRolePermissions(t *testing.T) {
},
// Some admin style resources
{
Name: "Licences",
Name: "Licenses",
Actions: []policy.Action{policy.ActionCreate, policy.ActionRead, policy.ActionDelete},
Resource: rbac.ResourceLicense,
AuthorizeMap: map[bool][]authSubject{
Expand Down