mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
chore: Return copied templates to prevent reference issues (#6679)
This commit is contained in:
@ -1761,7 +1761,7 @@ func (q *fakeQuerier) GetTemplateByID(ctx context.Context, id uuid.UUID) (databa
|
||||
func (q *fakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (database.Template, error) {
|
||||
for _, template := range q.templates {
|
||||
if template.ID == id {
|
||||
return template, nil
|
||||
return template.DeepCopy(), nil
|
||||
}
|
||||
}
|
||||
return database.Template{}, sql.ErrNoRows
|
||||
@ -1785,7 +1785,7 @@ func (q *fakeQuerier) GetTemplateByOrganizationAndName(_ context.Context, arg da
|
||||
if template.Deleted != arg.Deleted {
|
||||
continue
|
||||
}
|
||||
return template, nil
|
||||
return template.DeepCopy(), nil
|
||||
}
|
||||
return database.Template{}, sql.ErrNoRows
|
||||
}
|
||||
@ -1808,7 +1808,7 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
|
||||
tpl.Description = arg.Description
|
||||
tpl.Icon = arg.Icon
|
||||
q.templates[idx] = tpl
|
||||
return tpl, nil
|
||||
return tpl.DeepCopy(), nil
|
||||
}
|
||||
|
||||
return database.Template{}, sql.ErrNoRows
|
||||
@ -1830,7 +1830,7 @@ func (q *fakeQuerier) UpdateTemplateScheduleByID(_ context.Context, arg database
|
||||
tpl.DefaultTTL = arg.DefaultTTL
|
||||
tpl.MaxTTL = arg.MaxTTL
|
||||
q.templates[idx] = tpl
|
||||
return tpl, nil
|
||||
return tpl.DeepCopy(), nil
|
||||
}
|
||||
|
||||
return database.Template{}, sql.ErrNoRows
|
||||
@ -1889,7 +1889,7 @@ func (q *fakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
|
||||
continue
|
||||
}
|
||||
}
|
||||
templates = append(templates, template)
|
||||
templates = append(templates, template.DeepCopy())
|
||||
}
|
||||
if len(templates) > 0 {
|
||||
slices.SortFunc(templates, func(i, j database.Template) bool {
|
||||
@ -2190,6 +2190,9 @@ func (q *fakeQuerier) GetTemplates(_ context.Context) ([]database.Template, erro
|
||||
defer q.mutex.RUnlock()
|
||||
|
||||
templates := slices.Clone(q.templates)
|
||||
for i := range templates {
|
||||
templates[i] = templates[i].DeepCopy()
|
||||
}
|
||||
slices.SortFunc(templates, func(i, j database.Template) bool {
|
||||
if i.Name != j.Name {
|
||||
return i.Name < j.Name
|
||||
@ -2775,7 +2778,7 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
|
||||
AllowUserCancelWorkspaceJobs: arg.AllowUserCancelWorkspaceJobs,
|
||||
}
|
||||
q.templates = append(q.templates, template)
|
||||
return template, nil
|
||||
return template.DeepCopy(), nil
|
||||
}
|
||||
|
||||
func (q *fakeQuerier) InsertTemplateVersion(_ context.Context, arg database.InsertTemplateVersionParams) (database.TemplateVersion, error) {
|
||||
@ -3403,7 +3406,7 @@ func (q *fakeQuerier) UpdateTemplateACLByID(_ context.Context, arg database.Upda
|
||||
template.UserACL = arg.UserACL
|
||||
|
||||
q.templates[i] = template
|
||||
return template, nil
|
||||
return template.DeepCopy(), nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
|
||||
"github.com/coder/coder/coderd/rbac"
|
||||
)
|
||||
|
||||
@ -86,6 +88,13 @@ func (t Template) RBACObject() rbac.Object {
|
||||
WithGroupACL(t.GroupACL)
|
||||
}
|
||||
|
||||
func (t Template) DeepCopy() Template {
|
||||
cpy := t
|
||||
cpy.UserACL = maps.Clone(t.UserACL)
|
||||
cpy.GroupACL = maps.Clone(t.GroupACL)
|
||||
return cpy
|
||||
}
|
||||
|
||||
func (TemplateVersion) RBACObject(template Template) rbac.Object {
|
||||
// Just use the parent template resource for controlling versions
|
||||
return template.RBACObject()
|
||||
|
Reference in New Issue
Block a user