Commit 38d851eb by Torkel Ödegaard

Another HTTP API fix

parent 73ee8a59
...@@ -23,21 +23,17 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error { ...@@ -23,21 +23,17 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
dash := cmd.GetDashboardModel() dash := cmd.GetDashboardModel()
// try get existing dashboard // try get existing dashboard
existing := m.Dashboard{Slug: dash.Slug, OrgId: dash.OrgId} var existing, sameTitle m.Dashboard
hasExisting, err := sess.Get(&existing)
if err != nil {
return err
}
if hasExisting { if dash.Id > 0 {
// another dashboard with same name dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing)
if dash.Id != existing.Id { if err != nil {
if cmd.Overwrite { return err
dash.Id = existing.Id }
} else { if !dashWithIdExists {
return m.ErrDashboardWithSameNameExists return m.ErrDashboardNotFound
}
} }
// check for is someone else has written in between // check for is someone else has written in between
if dash.Version != existing.Version { if dash.Version != existing.Version {
if cmd.Overwrite { if cmd.Overwrite {
...@@ -48,6 +44,22 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error { ...@@ -48,6 +44,22 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
} }
} }
sameTitleExists, err := sess.Where("org_id=? AND slug=?", dash.OrgId, dash.Slug).Get(&sameTitle)
if err != nil {
return err
}
if sameTitleExists {
// another dashboard with same name
if dash.Id != sameTitle.Id {
if cmd.Overwrite {
dash.Id = sameTitle.Id
} else {
return m.ErrDashboardWithSameNameExists
}
}
}
affectedRows := int64(0) affectedRows := int64(0)
if dash.Id == 0 { if dash.Id == 0 {
......
...@@ -66,6 +66,24 @@ func TestDashboardDataAccess(t *testing.T) { ...@@ -66,6 +66,24 @@ func TestDashboardDataAccess(t *testing.T) {
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
}) })
Convey("Should not be able to overwrite dashboard in another org", func() {
query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1}
GetDashboard(&query)
cmd := m.SaveDashboardCommand{
OrgId: 2,
Overwrite: true,
Dashboard: map[string]interface{}{
"id": float64(query.Result.Id),
"title": "Expect error",
"tags": []interface{}{},
},
}
err := SaveDashboard(&cmd)
So(err, ShouldNotBeNil)
})
Convey("Should be able to search for dashboard", func() { Convey("Should be able to search for dashboard", func() {
query := m.SearchDashboardsQuery{ query := m.SearchDashboardsQuery{
Title: "test", Title: "test",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment