2018-01-29 06:51:01 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-02-21 09:38:09 -06:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-29 06:51:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-01-29 06:51:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func GetFolders(c *models.ReqContext) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
2019-04-17 06:07:50 -05:00
|
|
|
folders, err := s.GetFolders(c.QueryInt64("limit"))
|
2018-01-29 06:51:01 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2018-02-20 06:57:32 -06:00
|
|
|
return toFolderError(err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
result := make([]dtos.FolderSearchHit, 0)
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
for _, f := range folders {
|
|
|
|
result = append(result, dtos.FolderSearchHit{
|
|
|
|
Id: f.Id,
|
|
|
|
Uid: f.Uid,
|
|
|
|
Title: f.Title,
|
|
|
|
})
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, result)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func GetFolderByUID(c *models.ReqContext) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
2018-03-22 16:13:46 -05:00
|
|
|
folder, err := s.GetFolderByUID(c.Params(":uid"))
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
if err != nil {
|
|
|
|
return toFolderError(err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
g := guardian.New(folder.Id, c.OrgId, c.SignedInUser)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, toFolderDto(g, folder))
|
2018-02-20 06:57:32 -06:00
|
|
|
}
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func GetFolderByID(c *models.ReqContext) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
2018-03-22 16:13:46 -05:00
|
|
|
folder, err := s.GetFolderByID(c.ParamsInt64(":id"))
|
2018-01-29 06:51:01 -06:00
|
|
|
if err != nil {
|
2018-02-20 06:57:32 -06:00
|
|
|
return toFolderError(err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
g := guardian.New(folder.Id, c.OrgId, c.SignedInUser)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, toFolderDto(g, folder))
|
2018-02-20 06:57:32 -06:00
|
|
|
}
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func (hs *HTTPServer) CreateFolder(c *models.ReqContext, cmd models.CreateFolderCommand) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
err := s.CreateFolder(&cmd)
|
2018-01-29 06:51:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return toFolderError(err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 01:32:47 -05:00
|
|
|
if hs.Cfg.EditorsCanAdmin {
|
2019-03-14 06:18:07 -05:00
|
|
|
if err := dashboards.MakeUserAdmin(hs.Bus, c.OrgId, c.SignedInUser.UserId, cmd.Result.Id, true); err != nil {
|
2019-03-11 09:05:28 -05:00
|
|
|
hs.log.Error("Could not make user admin", "folder", cmd.Result.Title, "user", c.SignedInUser.UserId, "error", err)
|
|
|
|
}
|
2019-03-05 09:53:16 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
g := guardian.New(cmd.Result.Id, c.OrgId, c.SignedInUser)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, toFolderDto(g, cmd.Result))
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func UpdateFolder(c *models.ReqContext, cmd models.UpdateFolderCommand) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
err := s.UpdateFolder(c.Params(":uid"), &cmd)
|
2018-01-29 06:51:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return toFolderError(err)
|
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
g := guardian.New(cmd.Result.Id, c.OrgId, c.SignedInUser)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, toFolderDto(g, cmd.Result))
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func DeleteFolder(c *models.ReqContext) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
f, err := s.DeleteFolder(c.Params(":uid"))
|
|
|
|
if err != nil {
|
|
|
|
return toFolderError(err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, util.DynMap{
|
2018-02-21 09:38:09 -06:00
|
|
|
"title": f.Title,
|
|
|
|
"message": fmt.Sprintf("Folder %s deleted", f.Title),
|
|
|
|
})
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func toFolderDto(g guardian.DashboardGuardian, folder *models.Folder) dtos.Folder {
|
2018-02-19 06:32:45 -06:00
|
|
|
canEdit, _ := g.CanEdit()
|
|
|
|
canSave, _ := g.CanSave()
|
|
|
|
canAdmin, _ := g.CanAdmin()
|
2018-01-29 06:51:01 -06:00
|
|
|
|
|
|
|
// Finding creator and last updater of the folder
|
2018-09-22 03:50:00 -05:00
|
|
|
updater, creator := anonString, anonString
|
2018-01-29 06:51:01 -06:00
|
|
|
if folder.CreatedBy > 0 {
|
|
|
|
creator = getUserLogin(folder.CreatedBy)
|
|
|
|
}
|
2018-02-01 14:00:37 -06:00
|
|
|
if folder.UpdatedBy > 0 {
|
|
|
|
updater = getUserLogin(folder.UpdatedBy)
|
|
|
|
}
|
2018-01-29 06:51:01 -06:00
|
|
|
|
|
|
|
return dtos.Folder{
|
|
|
|
Id: folder.Id,
|
2018-02-01 14:00:37 -06:00
|
|
|
Uid: folder.Uid,
|
2018-01-29 06:51:01 -06:00
|
|
|
Title: folder.Title,
|
2018-02-20 06:57:32 -06:00
|
|
|
Url: folder.Url,
|
2018-01-29 06:51:01 -06:00
|
|
|
HasAcl: folder.HasAcl,
|
|
|
|
CanSave: canSave,
|
|
|
|
CanEdit: canEdit,
|
|
|
|
CanAdmin: canAdmin,
|
|
|
|
CreatedBy: creator,
|
|
|
|
Created: folder.Created,
|
|
|
|
UpdatedBy: updater,
|
|
|
|
Updated: folder.Updated,
|
|
|
|
Version: folder.Version,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toFolderError(err error) Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
if err == models.ErrFolderTitleEmpty ||
|
|
|
|
err == models.ErrFolderSameNameExists ||
|
|
|
|
err == models.ErrFolderWithSameUIDExists ||
|
|
|
|
err == models.ErrDashboardTypeMismatch ||
|
|
|
|
err == models.ErrDashboardInvalidUid ||
|
|
|
|
err == models.ErrDashboardUidToLong {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, err.Error(), nil)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if err == models.ErrFolderAccessDenied {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(403, "Access denied", err)
|
2018-02-20 06:57:32 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if err == models.ErrFolderNotFound {
|
|
|
|
return JSON(404, util.DynMap{"status": "not-found", "message": models.ErrFolderNotFound.Error()})
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if err == models.ErrFolderVersionMismatch {
|
|
|
|
return JSON(412, util.DynMap{"status": "version-mismatch", "message": models.ErrFolderVersionMismatch.Error()})
|
2018-02-01 11:29:00 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Folder API error", err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|