Skip to content

Commit 5a081eb

Browse files
committed
fix authorize bug
1 parent ea25c08 commit 5a081eb

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

coderd/coderdtest/coderdtest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ func createAnotherUserRetry(t *testing.T, client *codersdk.Client, organizationI
399399
// with the responses provided. It uses the "echo" provisioner for compatibility
400400
// with testing.
401401
func CreateTemplateVersion(t *testing.T, client *codersdk.Client, organizationID uuid.UUID, res *echo.Responses) codersdk.TemplateVersion {
402+
t.Helper()
402403
data, err := echo.Tar(res)
403404
require.NoError(t, err)
404405
file, err := client.Upload(context.Background(), codersdk.ContentTypeTar, data)

coderd/database/modelmethods.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func (u UserACL) Actions() map[string][]rbac.Action {
2121

2222
func (t Template) UserACL() UserACL {
2323
var acl UserACL
24+
if len(t.userACL) == 0 {
25+
return acl
26+
}
27+
2428
err := json.Unmarshal(t.userACL, &acl)
2529
if err != nil {
2630
panic(fmt.Sprintf("failed to unmarshal template.userACL: %v", err.Error()))

coderd/httpmw/templateversionparam.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88

99
"github.com/go-chi/chi/v5"
10+
"golang.org/x/xerrors"
1011

1112
"github.com/coder/coder/coderd/database"
1213
"github.com/coder/coder/coderd/httpapi"
@@ -32,6 +33,7 @@ func ExtractTemplateVersionParam(db database.Store) func(http.Handler) http.Hand
3233
if !parsed {
3334
return
3435
}
36+
3537
templateVersion, err := db.GetTemplateVersionByID(r.Context(), templateVersionID)
3638
if errors.Is(err, sql.ErrNoRows) {
3739
httpapi.ResourceNotFound(rw)
@@ -46,11 +48,7 @@ func ExtractTemplateVersionParam(db database.Store) func(http.Handler) http.Hand
4648
}
4749

4850
template, err := db.GetTemplateByID(r.Context(), templateVersion.TemplateID.UUID)
49-
if errors.Is(err, sql.ErrNoRows) {
50-
httpapi.ResourceNotFound(rw)
51-
return
52-
}
53-
if err != nil {
51+
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
5452
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
5553
Message: "Internal error fetching template.",
5654
Detail: err.Error(),
@@ -61,8 +59,7 @@ func ExtractTemplateVersionParam(db database.Store) func(http.Handler) http.Hand
6159
ctx := context.WithValue(r.Context(), templateVersionParamContextKey{}, templateVersion)
6260
chi.RouteContext(ctx).URLParams.Add("organization", templateVersion.OrganizationID.String())
6361

64-
ctx = context.WithValue(r.Context(), templateParamContextKey{}, template)
65-
chi.RouteContext(ctx).URLParams.Add("organization", template.OrganizationID.String())
62+
ctx = context.WithValue(ctx, templateParamContextKey{}, template)
6663

6764
next.ServeHTTP(rw, r.WithContext(ctx))
6865
})

coderd/templateversions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
2727
templateVersion = httpmw.TemplateVersionParam(r)
2828
template = httpmw.TemplateParam(r)
2929
)
30+
3031
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
3132
httpapi.ResourceNotFound(rw)
3233
return

0 commit comments

Comments
 (0)