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"
|
2022-03-18 11:33:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
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{
|
2018-09-13 08:15:42 -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"),
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
|
|
|
items, err := repo.Find(query)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, items)
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
2016-10-14 02:33:16 -05:00
|
|
|
|
2017-10-18 03:13:02 -05:00
|
|
|
type CreateAnnotationError struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CreateAnnotationError) Error() string {
|
|
|
|
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
|
|
|
|
|
|
|
var canSave bool
|
|
|
|
var err error
|
|
|
|
if cmd.DashboardId != 0 {
|
|
|
|
canSave, err = canSaveLocalAnnotation(c, cmd.DashboardId)
|
|
|
|
} else {
|
|
|
|
canSave = canSaveGlobalAnnotation(c)
|
|
|
|
}
|
|
|
|
if 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 == "" {
|
|
|
|
err := &CreateAnnotationError{"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
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, 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 == "" {
|
|
|
|
err := &CreateAnnotationError{"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 {
|
2017-10-18 03:13:02 -05:00
|
|
|
err := &CreateAnnotationError{"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:
|
2017-10-18 03:13:02 -05:00
|
|
|
err := &CreateAnnotationError{"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
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, 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-02-11 12:43:29 -06:00
|
|
|
annotation, resp := findAnnotationByID(repo, annotationID, c.OrgId)
|
|
|
|
if resp != nil {
|
2017-12-20 17:52:21 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
canSave := true
|
|
|
|
if annotation.GetType() == annotations.Local {
|
|
|
|
canSave, err = canSaveLocalAnnotation(c, annotation.DashboardId)
|
|
|
|
} else {
|
|
|
|
if !hs.Features.IsEnabled(featuremgmt.FlagAccesscontrol) {
|
|
|
|
canSave = canSaveGlobalAnnotation(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if 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
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.Update(&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-02-11 12:43:29 -06:00
|
|
|
annotation, resp := findAnnotationByID(repo, annotationID, c.OrgId)
|
|
|
|
if resp != nil {
|
2019-01-27 05:49:22 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
canSave := true
|
|
|
|
if annotation.GetType() == annotations.Local {
|
|
|
|
canSave, err = canSaveLocalAnnotation(c, annotation.DashboardId)
|
|
|
|
} else {
|
|
|
|
if !hs.Features.IsEnabled(featuremgmt.FlagAccesscontrol) {
|
|
|
|
canSave = canSaveGlobalAnnotation(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if 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
|
|
|
}
|
|
|
|
|
2019-08-16 03:49:30 -05:00
|
|
|
if err := repo.Update(&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-18 11:33:21 -05:00
|
|
|
func (hs *HTTPServer) DeleteAnnotations(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
cmd := dtos.DeleteAnnotationsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2016-10-14 02:33:16 -05:00
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
|
|
|
err := repo.Delete(&annotations.DeleteParams{
|
2018-06-25 09:02:34 -05:00
|
|
|
OrgId: c.OrgId,
|
2018-06-25 06:58:49 -05:00
|
|
|
Id: cmd.AnnotationId,
|
2016-10-14 02:33:16 -05:00
|
|
|
DashboardId: cmd.DashboardId,
|
|
|
|
PanelId: cmd.PanelId,
|
|
|
|
})
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
annotation, resp := findAnnotationByID(repo, annotationID, c.OrgId)
|
|
|
|
if resp != nil {
|
2017-12-20 17:52:21 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-03-18 11:33:21 -05:00
|
|
|
var canSave bool
|
|
|
|
if annotation.GetType() == annotations.Local {
|
|
|
|
canSave, err = canSaveLocalAnnotation(c, annotation.DashboardId)
|
|
|
|
} else {
|
|
|
|
canSave = canSaveGlobalAnnotation(c)
|
|
|
|
}
|
|
|
|
if err != nil || !canSave {
|
2022-02-11 12:43:29 -06:00
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
2022-01-14 10:55:57 -06:00
|
|
|
err = repo.Delete(&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-03-18 11:33:21 -05:00
|
|
|
func canSaveLocalAnnotation(c *models.ReqContext, dashboardID int64) (bool, error) {
|
|
|
|
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-03-18 11:33:21 -05:00
|
|
|
func canSaveGlobalAnnotation(c *models.ReqContext) bool {
|
|
|
|
return c.SignedInUser.HasRole(models.ROLE_EDITOR)
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:43:29 -06:00
|
|
|
func findAnnotationByID(repo annotations.Repository, annotationID int64, orgID int64) (*annotations.ItemDTO, response.Response) {
|
|
|
|
items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: orgID})
|
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()
|
|
|
|
result, err := repo.FindTags(query)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Failed to find annotation tags", err)
|
|
|
|
}
|
|
|
|
|
2021-07-01 03:23:33 -05:00
|
|
|
return response.JSON(200, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
annotation, resp := findAnnotationByID(annotations.GetRepository(), int64(annotationId), orgID)
|
|
|
|
if resp != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if annotation.GetType() == annotations.Global {
|
|
|
|
return accesscontrol.ScopeAnnotationsTypeGlobal, nil
|
|
|
|
} else {
|
|
|
|
return accesscontrol.ScopeAnnotationsTypeLocal, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accesscontrol.ScopeAnnotationsProvider.GetResourceScope(""), annotationTypeResolver
|
|
|
|
}
|