grafana/pkg/services/sqlstore/dashboard_acl.go

142 lines
3.5 KiB
Go
Raw Normal View History

package sqlstore
import (
"time"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddHandler("sql", SetDashboardAcl)
bus.AddHandler("sql", RemoveDashboardAcl)
2017-06-19 16:30:54 -05:00
bus.AddHandler("sql", GetDashboardAclInfoList)
2017-06-19 10:54:37 -05:00
bus.AddHandler("sql", GetInheritedDashboardAcl)
}
func SetDashboardAcl(cmd *m.SetDashboardAclCommand) error {
return inTransaction(func(sess *DBSession) error {
if cmd.UserId == 0 && cmd.UserGroupId == 0 {
2017-06-19 17:19:58 -05:00
return m.ErrDashboardAclInfoMissing
}
2017-06-19 17:34:25 -05:00
if cmd.DashboardId == 0 {
return m.ErrDashboardPermissionDashboardEmpty
}
if res, err := sess.Query("SELECT 1 from "+dialect.Quote("dashboard_acl")+" WHERE dashboard_id =? and (user_group_id=? or user_id=?)", cmd.DashboardId, cmd.UserGroupId, cmd.UserId); err != nil {
return err
} else if len(res) == 1 {
entity := m.DashboardAcl{
2017-06-17 17:24:38 -05:00
Permissions: cmd.Permissions,
Updated: time.Now(),
}
if _, err := sess.Cols("updated", "permissions").Where("dashboard_id =? and (user_group_id=? or user_id=?)", cmd.DashboardId, cmd.UserGroupId, cmd.UserId).Update(&entity); err != nil {
return err
}
return nil
}
entity := m.DashboardAcl{
OrgId: cmd.OrgId,
UserGroupId: cmd.UserGroupId,
UserId: cmd.UserId,
Created: time.Now(),
Updated: time.Now(),
DashboardId: cmd.DashboardId,
2017-06-17 17:24:38 -05:00
Permissions: cmd.Permissions,
}
cols := []string{"org_id", "created", "updated", "dashboard_id", "permissions"}
if cmd.UserId != 0 {
cols = append(cols, "user_id")
}
if cmd.UserGroupId != 0 {
cols = append(cols, "user_group_id")
}
2017-06-09 14:56:13 -05:00
entityId, err := sess.Cols(cols...).Insert(&entity)
if err != nil {
return err
}
2017-06-09 14:56:13 -05:00
cmd.Result = entity
cmd.Result.Id = entityId
2017-06-09 14:56:13 -05:00
// Update dashboard HasAcl flag
dashboard := m.Dashboard{
HasAcl: true,
}
2017-06-17 17:24:38 -05:00
if _, err := sess.Cols("has_acl").Where("id=? OR parent_id=?", cmd.DashboardId, cmd.DashboardId).Update(&dashboard); err != nil {
return err
}
return nil
})
}
func RemoveDashboardAcl(cmd *m.RemoveDashboardAclCommand) error {
return inTransaction(func(sess *DBSession) error {
var rawSQL = "DELETE FROM " + dialect.Quote("dashboard_acl") + " WHERE org_id =? and id=?"
_, err := sess.Exec(rawSQL, cmd.OrgId, cmd.AclId)
if err != nil {
return err
}
return err
})
}
2017-06-19 10:54:37 -05:00
func GetInheritedDashboardAcl(query *m.GetInheritedDashboardAclQuery) error {
2017-06-19 10:03:54 -05:00
rawSQL := `SELECT
da.id,
da.org_id,
da.dashboard_id,
da.user_id,
da.user_group_id,
da.permissions,
da.created,
2017-06-19 10:54:37 -05:00
da.updated
FROM dashboard_acl as da
WHERE da.dashboard_id IN (
2017-06-19 10:03:54 -05:00
SELECT id FROM dashboard where id = ?
UNION
SELECT parent_id from dashboard where id = ?
2017-06-19 10:54:37 -05:00
) AND org_id = ?`
2017-06-19 10:03:54 -05:00
query.Result = make([]*m.DashboardAcl, 0)
2017-06-19 10:54:37 -05:00
return x.SQL(rawSQL, query.DashboardId, query.DashboardId, query.OrgId).Find(&query.Result)
2017-06-19 10:03:54 -05:00
}
2017-06-19 16:30:54 -05:00
func GetDashboardAclInfoList(query *m.GetDashboardAclInfoListQuery) error {
rawSQL := `SELECT
da.id,
da.org_id,
da.dashboard_id,
da.user_id,
da.user_group_id,
2017-06-16 20:25:24 -05:00
da.permissions,
da.created,
da.updated,
u.login AS user_login,
u.email AS user_email,
ug.name AS user_group
FROM` + dialect.Quote("dashboard_acl") + ` as da
LEFT OUTER JOIN ` + dialect.Quote("user") + ` AS u ON u.id = da.user_id
LEFT OUTER JOIN user_group ug on ug.id = da.user_group_id
WHERE dashboard_id=?`
query.Result = make([]*m.DashboardAclInfoDTO, 0)
err := x.SQL(rawSQL, query.DashboardId).Find(&query.Result)
for _, p := range query.Result {
2017-06-16 20:25:24 -05:00
p.PermissionName = p.Permissions.String()
}
return err
}