2017-05-08 08:35:34 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-06-21 18:02:03 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2017-05-08 08:35:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
|
|
)
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func GetDashboardPermissionList(c *m.ReqContext) Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
dashID := c.ParamsInt64(":dashboardId")
|
2017-05-08 08:35:34 -05:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
_, rsp := getDashboardHelper(c.OrgId, "", dashID, "")
|
2018-02-12 02:26:09 -06:00
|
|
|
if rsp != nil {
|
|
|
|
return rsp
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
g := guardian.New(dashID, c.OrgId, c.SignedInUser)
|
2017-06-19 12:47:44 -05:00
|
|
|
|
2018-02-26 13:15:57 -06:00
|
|
|
if canAdmin, err := g.CanAdmin(); err != nil || !canAdmin {
|
2017-06-19 12:47:44 -05:00
|
|
|
return dashboardGuardianResponse(err)
|
2017-05-08 08:35:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-26 13:15:57 -06:00
|
|
|
acl, err := g.GetAcl()
|
2017-06-22 16:10:43 -05:00
|
|
|
if err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to get dashboard permissions", err)
|
2017-05-08 08:35:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-05 07:28:24 -06:00
|
|
|
for _, perm := range acl {
|
2018-03-29 04:32:24 -05:00
|
|
|
perm.UserAvatarUrl = dtos.GetGravatarUrl(perm.UserEmail)
|
2018-04-04 08:50:45 -05:00
|
|
|
|
|
|
|
if perm.TeamId > 0 {
|
|
|
|
perm.TeamAvatarUrl = dtos.GetGravatarUrlWithDefault(perm.TeamEmail, perm.Team)
|
|
|
|
}
|
2018-02-05 07:28:24 -06:00
|
|
|
if perm.Slug != "" {
|
|
|
|
perm.Url = m.GetDashboardFolderUrl(perm.IsFolder, perm.Uid, perm.Slug)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, acl)
|
2017-05-08 08:35:34 -05:00
|
|
|
}
|
2017-05-22 03:36:47 -05:00
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func UpdateDashboardPermissions(c *m.ReqContext, apiCmd dtos.UpdateDashboardAclCommand) Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
dashID := c.ParamsInt64(":dashboardId")
|
2017-06-19 12:47:44 -05:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
_, rsp := getDashboardHelper(c.OrgId, "", dashID, "")
|
2018-02-12 02:26:09 -06:00
|
|
|
if rsp != nil {
|
|
|
|
return rsp
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
g := guardian.New(dashID, c.OrgId, c.SignedInUser)
|
2018-02-26 13:15:57 -06:00
|
|
|
if canAdmin, err := g.CanAdmin(); err != nil || !canAdmin {
|
2017-06-19 12:47:44 -05:00
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-06-21 18:02:03 -05:00
|
|
|
cmd := m.UpdateDashboardAclCommand{}
|
2018-03-22 06:37:35 -05:00
|
|
|
cmd.DashboardId = dashID
|
2017-06-09 14:56:13 -05:00
|
|
|
|
2017-06-21 18:02:03 -05:00
|
|
|
for _, item := range apiCmd.Items {
|
|
|
|
cmd.Items = append(cmd.Items, &m.DashboardAcl{
|
|
|
|
OrgId: c.OrgId,
|
2018-03-22 06:37:35 -05:00
|
|
|
DashboardId: dashID,
|
2017-06-21 18:02:03 -05:00
|
|
|
UserId: item.UserId,
|
2017-12-11 10:46:05 -06:00
|
|
|
TeamId: item.TeamId,
|
2017-06-21 18:02:03 -05:00
|
|
|
Role: item.Role,
|
|
|
|
Permission: item.Permission,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:15:57 -06:00
|
|
|
if okToUpdate, err := g.CheckPermissionBeforeUpdate(m.PERMISSION_ADMIN, cmd.Items); err != nil || !okToUpdate {
|
2018-01-18 07:30:04 -06:00
|
|
|
if err != nil {
|
2018-02-27 09:03:11 -06:00
|
|
|
if err == guardian.ErrGuardianPermissionExists ||
|
|
|
|
err == guardian.ErrGuardianOverride {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, err.Error(), err)
|
2018-02-26 13:15:57 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Error while checking dashboard permissions", err)
|
2018-01-18 07:30:04 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(403, "Cannot remove own admin permission for a folder", nil)
|
2018-01-18 07:30:04 -06:00
|
|
|
}
|
|
|
|
|
2017-06-09 14:56:13 -05:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2017-06-19 17:55:34 -05:00
|
|
|
if err == m.ErrDashboardAclInfoMissing || err == m.ErrDashboardPermissionDashboardEmpty {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(409, err.Error(), err)
|
2017-06-09 14:56:13 -05:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to create permission", err)
|
2017-06-09 14:56:13 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("Dashboard permissions updated")
|
2017-06-09 14:56:13 -05:00
|
|
|
}
|