2016-09-08 04:25:45 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-03-18 11:33:21 -05:00
|
|
|
"context"
|
2021-03-29 08:47:16 -05:00
|
|
|
"errors"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2017-10-12 03:12:15 -05:00
|
|
|
"strings"
|
|
|
|
|
2016-09-08 04:25:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-03-18 11:33:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2016-09-08 04:25:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations"
|
2017-12-20 17:52:21 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
2017-11-16 07:24:56 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2016-09-08 04:25:45 -05:00
|
|
|
)
|
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
func (hs *HTTPServer) GetAnnotations(c *models.ReqContext) response.Response {
|
2016-09-08 04:25:45 -05:00
|
|
|
query := &annotations.ItemQuery{
|
2022-04-11 07:18:38 -05:00
|
|
|
From: c.QueryInt64("from"),
|
|
|
|
To: c.QueryInt64("to"),
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
UserId: c.QueryInt64("userId"),
|
|
|
|
AlertId: c.QueryInt64("alertId"),
|
|
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
|
|
PanelId: c.QueryInt64("panelId"),
|
|
|
|
Limit: c.QueryInt64("limit"),
|
|
|
|
Tags: c.QueryStrings("tags"),
|
|
|
|
Type: c.Query("type"),
|
|
|
|
MatchAny: c.QueryBool("matchAny"),
|
|
|
|
SignedInUser: c.SignedInUser,
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2022-03-22 06:20:57 -05:00
|
|
|
items, err := repo.Find(c.Req.Context(), query)
|
2016-09-08 04:25:45 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get annotations", err)
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
2017-10-07 03:31:39 -05:00
|
|
|
if item.Email != "" {
|
|
|
|
item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
|
|
|
|
}
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, items)
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
2016-10-14 02:33:16 -05:00
|
|
|
|
2022-03-23 16:39:00 -05:00
|
|
|
type AnnotationError struct {
|
2017-10-18 03:13:02 -05:00
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:39:00 -05:00
|
|
|
func (e *AnnotationError) Error() string {
|
2017-10-18 03:13:02 -05:00
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
func (hs *HTTPServer) PostAnnotation(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
cmd := dtos.PostAnnotationsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-03-18 11:33:21 -05:00
|
|
|
|
2022-04-20 02:43:42 -05:00
|
|
|
if canSave, err := hs.canCreateAnnotation(c, cmd.DashboardId); err != nil || !canSave {
|
2017-12-20 17:52:21 -06:00
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-04-10 12:22:58 -05:00
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2017-10-18 03:13:02 -05:00
|
|
|
if cmd.Text == "" {
|
2022-03-23 16:39:00 -05:00
|
|
|
err := &AnnotationError{"text field should not be empty"}
|
2021-03-29 08:47:16 -05:00
|
|
|
return response.Error(400, "Failed to save annotation", err)
|
2017-10-18 03:13:02 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 12:22:58 -05:00
|
|
|
item := annotations.Item{
|
|
|
|
OrgId: c.OrgId,
|
2017-10-07 03:31:39 -05:00
|
|
|
UserId: c.UserId,
|
2017-04-10 12:22:58 -05:00
|
|
|
DashboardId: cmd.DashboardId,
|
|
|
|
PanelId: cmd.PanelId,
|
2018-03-22 10:21:47 -05:00
|
|
|
Epoch: cmd.Time,
|
2019-08-16 03:49:30 -05:00
|
|
|
EpochEnd: cmd.TimeEnd,
|
2017-04-10 12:22:58 -05:00
|
|
|
Text: cmd.Text,
|
2017-10-07 03:31:39 -05:00
|
|
|
Data: cmd.Data,
|
|
|
|
Tags: cmd.Tags,
|
2017-04-10 12:22:58 -05:00
|
|
|
}
|
|
|
|
|
2017-04-12 09:26:34 -05:00
|
|
|
if err := repo.Save(&item); err != nil {
|
2021-03-29 08:47:16 -05:00
|
|
|
if errors.Is(err, annotations.ErrTimerangeMissing) {
|
|
|
|
return response.Error(400, "Failed to save annotation", err)
|
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to save annotation", err)
|
2017-04-10 12:22:58 -05:00
|
|
|
}
|
|
|
|
|
2017-11-16 07:24:56 -06:00
|
|
|
startID := item.Id
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2017-11-16 07:24:56 -06:00
|
|
|
"message": "Annotation added",
|
|
|
|
"id": startID,
|
|
|
|
})
|
2017-04-10 12:22:58 -05:00
|
|
|
}
|
|
|
|
|
2017-10-12 03:12:15 -05:00
|
|
|
func formatGraphiteAnnotation(what string, data string) string {
|
2017-10-18 03:13:02 -05:00
|
|
|
text := what
|
|
|
|
if data != "" {
|
|
|
|
text = text + "\n" + data
|
|
|
|
}
|
|
|
|
return text
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
func (hs *HTTPServer) PostGraphiteAnnotation(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
cmd := dtos.PostGraphiteAnnotationsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2017-10-12 03:12:15 -05:00
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2017-10-18 03:13:02 -05:00
|
|
|
if cmd.What == "" {
|
2022-03-23 16:39:00 -05:00
|
|
|
err := &AnnotationError{"what field should not be empty"}
|
2021-03-29 08:47:16 -05:00
|
|
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
2017-10-18 03:13:02 -05:00
|
|
|
}
|
|
|
|
|
2017-10-12 03:12:15 -05:00
|
|
|
text := formatGraphiteAnnotation(cmd.What, cmd.Data)
|
|
|
|
|
|
|
|
// Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
|
|
|
|
var tagsArray []string
|
|
|
|
switch tags := cmd.Tags.(type) {
|
|
|
|
case string:
|
2017-10-18 03:13:02 -05:00
|
|
|
if tags != "" {
|
|
|
|
tagsArray = strings.Split(tags, " ")
|
|
|
|
} else {
|
|
|
|
tagsArray = []string{}
|
|
|
|
}
|
2017-10-12 03:12:15 -05:00
|
|
|
case []interface{}:
|
|
|
|
for _, t := range tags {
|
|
|
|
if tagStr, ok := t.(string); ok {
|
|
|
|
tagsArray = append(tagsArray, tagStr)
|
|
|
|
} else {
|
2022-03-23 16:39:00 -05:00
|
|
|
err := &AnnotationError{"tag should be a string"}
|
2021-03-29 08:47:16 -05:00
|
|
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2022-03-23 16:39:00 -05:00
|
|
|
err := &AnnotationError{"unsupported tags format"}
|
2021-03-29 08:47:16 -05:00
|
|
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
item := annotations.Item{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
UserId: c.UserId,
|
2018-04-09 06:58:09 -05:00
|
|
|
Epoch: cmd.When * 1000,
|
2017-10-12 03:12:15 -05:00
|
|
|
Text: text,
|
|
|
|
Tags: tagsArray,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.Save(&item); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to save Graphite annotation", err)
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2017-11-16 07:24:56 -06:00
|
|
|
"message": "Graphite annotation added",
|
|
|
|
"id": item.Id,
|
|
|
|
})
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
func (hs *HTTPServer) UpdateAnnotation(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
cmd := dtos.UpdateAnnotationsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
|
|
|
|
annotationID, err := strconv.ParseInt(web.Params(c.Req)[":annotationId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "annotationId is invalid", err)
|
|
|
|
}
|
2017-10-07 03:31:39 -05:00
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2022-04-11 07:18:38 -05:00
|
|
|
annotation, resp := findAnnotationByID(c.Req.Context(), repo, annotationID, c.SignedInUser)
|
2022-02-11 12:43:29 -06:00
|
|
|
if resp != nil {
|
2017-12-20 17:52:21 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-04-20 02:43:42 -05:00
|
|
|
if canSave, err := hs.canSaveAnnotation(c, annotation); err != nil || !canSave {
|
2022-02-11 12:43:29 -06:00
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
item := annotations.Item{
|
2019-08-16 03:49:30 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
UserId: c.UserId,
|
|
|
|
Id: annotationID,
|
|
|
|
Epoch: cmd.Time,
|
|
|
|
EpochEnd: cmd.TimeEnd,
|
|
|
|
Text: cmd.Text,
|
|
|
|
Tags: cmd.Tags,
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2022-03-22 06:20:57 -05:00
|
|
|
if err := repo.Update(c.Req.Context(), &item); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update annotation", err)
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Annotation updated")
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
func (hs *HTTPServer) PatchAnnotation(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
cmd := dtos.PatchAnnotationsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
annotationID, err := strconv.ParseInt(web.Params(c.Req)[":annotationId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "annotationId is invalid", err)
|
|
|
|
}
|
2019-01-27 05:49:22 -06:00
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2022-04-11 07:18:38 -05:00
|
|
|
annotation, resp := findAnnotationByID(c.Req.Context(), repo, annotationID, c.SignedInUser)
|
2022-02-11 12:43:29 -06:00
|
|
|
if resp != nil {
|
2019-01-27 05:49:22 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-04-20 02:43:42 -05:00
|
|
|
if canSave, err := hs.canSaveAnnotation(c, annotation); err != nil || !canSave {
|
2022-02-11 12:43:29 -06:00
|
|
|
return dashboardGuardianResponse(err)
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
existing := annotations.Item{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
UserId: c.UserId,
|
|
|
|
Id: annotationID,
|
2022-02-11 12:43:29 -06:00
|
|
|
Epoch: annotation.Time,
|
|
|
|
EpochEnd: annotation.TimeEnd,
|
|
|
|
Text: annotation.Text,
|
|
|
|
Tags: annotation.Tags,
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Tags != nil {
|
|
|
|
existing.Tags = cmd.Tags
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Text != "" && cmd.Text != existing.Text {
|
|
|
|
existing.Text = cmd.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Time > 0 && cmd.Time != existing.Epoch {
|
|
|
|
existing.Epoch = cmd.Time
|
|
|
|
}
|
|
|
|
|
2019-08-16 03:49:30 -05:00
|
|
|
if cmd.TimeEnd > 0 && cmd.TimeEnd != existing.EpochEnd {
|
|
|
|
existing.EpochEnd = cmd.TimeEnd
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
2022-03-22 06:20:57 -05:00
|
|
|
if err := repo.Update(c.Req.Context(), &existing); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update annotation", err)
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Annotation patched")
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
2022-03-23 16:39:00 -05:00
|
|
|
func (hs *HTTPServer) MassDeleteAnnotations(c *models.ReqContext) response.Response {
|
|
|
|
cmd := dtos.MassDeleteAnnotationsCmd{}
|
|
|
|
err := web.Bind(c.Req, &cmd)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.DashboardId != 0 && cmd.PanelId == 0) || (cmd.PanelId != 0 && cmd.DashboardId == 0) {
|
|
|
|
err := &AnnotationError{message: "DashboardId and PanelId are both required for mass delete"}
|
2021-11-29 03:18:01 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-03-23 16:39:00 -05:00
|
|
|
|
2016-10-14 02:33:16 -05:00
|
|
|
repo := annotations.GetRepository()
|
2022-03-23 16:39:00 -05:00
|
|
|
var deleteParams *annotations.DeleteParams
|
2016-10-14 02:33:16 -05:00
|
|
|
|
2022-04-21 07:31:02 -05:00
|
|
|
// validations only for RBAC. A user can mass delete all annotations in a (dashboard + panel) or a specific annotation
|
2022-03-23 16:39:00 -05:00
|
|
|
// if has access to that dashboard.
|
2022-04-25 03:42:09 -05:00
|
|
|
if !hs.AccessControl.IsDisabled() {
|
2022-03-23 16:39:00 -05:00
|
|
|
var dashboardId int64
|
|
|
|
|
|
|
|
if cmd.AnnotationId != 0 {
|
2022-04-11 07:18:38 -05:00
|
|
|
annotation, respErr := findAnnotationByID(c.Req.Context(), repo, cmd.AnnotationId, c.SignedInUser)
|
2022-03-23 16:39:00 -05:00
|
|
|
if respErr != nil {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
dashboardId = annotation.DashboardId
|
|
|
|
deleteParams = &annotations.DeleteParams{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
Id: cmd.AnnotationId,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dashboardId = cmd.DashboardId
|
|
|
|
deleteParams = &annotations.DeleteParams{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
DashboardId: cmd.DashboardId,
|
|
|
|
PanelId: cmd.PanelId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canSave, err := hs.canMassDeleteAnnotations(c, dashboardId)
|
|
|
|
if err != nil || !canSave {
|
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
} else { // legacy permissions
|
|
|
|
deleteParams = &annotations.DeleteParams{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
Id: cmd.AnnotationId,
|
|
|
|
DashboardId: cmd.DashboardId,
|
|
|
|
PanelId: cmd.PanelId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 12:23:09 -05:00
|
|
|
err = repo.Delete(c.Req.Context(), deleteParams)
|
2016-10-14 02:33:16 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete annotations", err)
|
2016-10-14 02:33:16 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Annotations deleted")
|
2016-10-14 02:33:16 -05:00
|
|
|
}
|
2017-10-07 03:31:39 -05:00
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
func (hs *HTTPServer) DeleteAnnotationByID(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
annotationID, err := strconv.ParseInt(web.Params(c.Req)[":annotationId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "annotationId is invalid", err)
|
|
|
|
}
|
2017-10-07 03:31:39 -05:00
|
|
|
|
2022-02-11 12:43:29 -06:00
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2022-04-11 07:18:38 -05:00
|
|
|
annotation, resp := findAnnotationByID(c.Req.Context(), repo, annotationID, c.SignedInUser)
|
2022-02-11 12:43:29 -06:00
|
|
|
if resp != nil {
|
2017-12-20 17:52:21 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-04-20 02:43:42 -05:00
|
|
|
if canSave, err := hs.canSaveAnnotation(c, annotation); err != nil || !canSave {
|
2022-02-11 12:43:29 -06:00
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
2022-03-25 12:23:09 -05:00
|
|
|
err = repo.Delete(c.Req.Context(), &annotations.DeleteParams{
|
2018-06-25 09:02:34 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
Id: annotationID,
|
2017-10-07 03:31:39 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete annotation", err)
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Annotation deleted")
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2022-04-20 02:43:42 -05:00
|
|
|
func (hs *HTTPServer) canSaveAnnotation(c *models.ReqContext, annotation *annotations.ItemDTO) (bool, error) {
|
|
|
|
if annotation.GetType() == annotations.Dashboard {
|
|
|
|
return canEditDashboard(c, annotation.DashboardId)
|
|
|
|
} else {
|
2022-04-25 03:42:09 -05:00
|
|
|
if hs.AccessControl.IsDisabled() {
|
2022-04-20 02:43:42 -05:00
|
|
|
return c.SignedInUser.HasRole(models.ROLE_EDITOR), nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func canEditDashboard(c *models.ReqContext, dashboardID int64) (bool, error) {
|
2022-03-18 11:33:21 -05:00
|
|
|
guard := guardian.New(c.Req.Context(), dashboardID, c.OrgId, c.SignedInUser)
|
|
|
|
if canEdit, err := guard.CanEdit(); err != nil || !canEdit {
|
|
|
|
return false, err
|
2017-12-20 17:52:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-04-11 07:18:38 -05:00
|
|
|
func findAnnotationByID(ctx context.Context, repo annotations.Repository, annotationID int64, user *models.SignedInUser) (*annotations.ItemDTO, response.Response) {
|
|
|
|
query := &annotations.ItemQuery{
|
|
|
|
AnnotationId: annotationID,
|
|
|
|
OrgId: user.OrgId,
|
|
|
|
SignedInUser: user,
|
|
|
|
}
|
|
|
|
items, err := repo.Find(ctx, query)
|
2017-12-20 17:52:21 -06:00
|
|
|
|
2022-02-11 12:43:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, response.Error(500, "Failed to find annotation", err)
|
|
|
|
}
|
2017-12-20 17:52:21 -06:00
|
|
|
|
2022-02-11 12:43:29 -06:00
|
|
|
if len(items) == 0 {
|
|
|
|
return nil, response.Error(404, "Annotation not found", nil)
|
2017-12-20 17:52:21 -06:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:43:29 -06:00
|
|
|
return items[0], nil
|
2017-12-20 17:52:21 -06:00
|
|
|
}
|
2021-06-30 06:42:54 -05:00
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
func (hs *HTTPServer) GetAnnotationTags(c *models.ReqContext) response.Response {
|
2021-06-30 06:42:54 -05:00
|
|
|
query := &annotations.TagsQuery{
|
|
|
|
OrgID: c.OrgId,
|
|
|
|
Tag: c.Query("tag"),
|
|
|
|
Limit: c.QueryInt64("limit"),
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
2022-03-25 12:23:09 -05:00
|
|
|
result, err := repo.FindTags(c.Req.Context(), query)
|
2021-06-30 06:42:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Failed to find annotation tags", err)
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, annotations.GetAnnotationTagsResponse{Result: result})
|
2021-06-30 06:42:54 -05:00
|
|
|
}
|
2022-03-18 11:33:21 -05:00
|
|
|
|
|
|
|
// AnnotationTypeScopeResolver provides an AttributeScopeResolver able to
|
|
|
|
// resolve annotation types. Scope "annotations:id:<id>" will be translated to "annotations:type:<type>,
|
|
|
|
// where <type> is the type of annotation with id <id>.
|
|
|
|
func AnnotationTypeScopeResolver() (string, accesscontrol.AttributeScopeResolveFunc) {
|
|
|
|
annotationTypeResolver := func(ctx context.Context, orgID int64, initialScope string) (string, error) {
|
|
|
|
scopeParts := strings.Split(initialScope, ":")
|
|
|
|
if scopeParts[0] != accesscontrol.ScopeAnnotationsRoot || len(scopeParts) != 3 {
|
|
|
|
return "", accesscontrol.ErrInvalidScope
|
|
|
|
}
|
|
|
|
|
|
|
|
annotationIdStr := scopeParts[2]
|
|
|
|
annotationId, err := strconv.Atoi(annotationIdStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", accesscontrol.ErrInvalidScope
|
|
|
|
}
|
|
|
|
|
2022-04-11 07:18:38 -05:00
|
|
|
// tempUser is used to resolve annotation type.
|
|
|
|
// The annotation doesn't get returned to the real user, so real user's permissions don't matter here.
|
|
|
|
tempUser := &models.SignedInUser{
|
|
|
|
OrgId: orgID,
|
|
|
|
Permissions: map[int64]map[string][]string{
|
|
|
|
orgID: {
|
|
|
|
accesscontrol.ActionDashboardsRead: {accesscontrol.ScopeDashboardsAll},
|
|
|
|
accesscontrol.ActionAnnotationsRead: {accesscontrol.ScopeAnnotationsAll},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
annotation, resp := findAnnotationByID(ctx, annotations.GetRepository(), int64(annotationId), tempUser)
|
2022-03-18 11:33:21 -05:00
|
|
|
if resp != nil {
|
2022-04-11 07:18:38 -05:00
|
|
|
return "", errors.New("could not resolve annotation type")
|
2022-03-18 11:33:21 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 12:28:39 -05:00
|
|
|
if annotation.GetType() == annotations.Organization {
|
|
|
|
return accesscontrol.ScopeAnnotationsTypeOrganization, nil
|
2022-03-18 11:33:21 -05:00
|
|
|
} else {
|
2022-03-21 12:28:39 -05:00
|
|
|
return accesscontrol.ScopeAnnotationsTypeDashboard, nil
|
2022-03-18 11:33:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return accesscontrol.ScopeAnnotationsProvider.GetResourceScope(""), annotationTypeResolver
|
|
|
|
}
|
2022-03-21 12:28:39 -05:00
|
|
|
|
2022-04-20 02:43:42 -05:00
|
|
|
func (hs *HTTPServer) canCreateAnnotation(c *models.ReqContext, dashboardId int64) (bool, error) {
|
|
|
|
if dashboardId != 0 {
|
2022-04-25 03:42:09 -05:00
|
|
|
if !hs.AccessControl.IsDisabled() {
|
2022-04-20 02:43:42 -05:00
|
|
|
evaluator := accesscontrol.EvalPermission(accesscontrol.ActionAnnotationsCreate, accesscontrol.ScopeAnnotationsTypeDashboard)
|
|
|
|
if canSave, err := hs.AccessControl.Evaluate(c.Req.Context(), c.SignedInUser, evaluator); err != nil || !canSave {
|
|
|
|
return canSave, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return canEditDashboard(c, dashboardId)
|
|
|
|
} else { // organization annotations
|
2022-04-25 03:42:09 -05:00
|
|
|
if !hs.AccessControl.IsDisabled() {
|
2022-04-20 02:43:42 -05:00
|
|
|
evaluator := accesscontrol.EvalPermission(accesscontrol.ActionAnnotationsCreate, accesscontrol.ScopeAnnotationsTypeOrganization)
|
|
|
|
return hs.AccessControl.Evaluate(c.Req.Context(), c.SignedInUser, evaluator)
|
|
|
|
} else {
|
|
|
|
return c.SignedInUser.HasRole(models.ROLE_EDITOR), nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 12:28:39 -05:00
|
|
|
}
|
2022-03-23 16:39:00 -05:00
|
|
|
|
|
|
|
func (hs *HTTPServer) canMassDeleteAnnotations(c *models.ReqContext, dashboardID int64) (bool, error) {
|
|
|
|
if dashboardID == 0 {
|
|
|
|
evaluator := accesscontrol.EvalPermission(accesscontrol.ActionAnnotationsDelete, accesscontrol.ScopeAnnotationsTypeOrganization)
|
|
|
|
return hs.AccessControl.Evaluate(c.Req.Context(), c.SignedInUser, evaluator)
|
|
|
|
} else {
|
|
|
|
evaluator := accesscontrol.EvalPermission(accesscontrol.ActionAnnotationsDelete, accesscontrol.ScopeAnnotationsTypeDashboard)
|
|
|
|
canSave, err := hs.AccessControl.Evaluate(c.Req.Context(), c.SignedInUser, evaluator)
|
|
|
|
if err != nil || !canSave {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-04-20 02:43:42 -05:00
|
|
|
canSave, err = canEditDashboard(c, dashboardID)
|
2022-03-23 16:39:00 -05:00
|
|
|
if err != nil || !canSave {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|