guardian.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package sqlstore
  2. import (
  3. "strconv"
  4. "github.com/grafana/grafana/pkg/bus"
  5. m "github.com/grafana/grafana/pkg/models"
  6. )
  7. func init() {
  8. bus.AddHandler("sql", GetAllowedDashboards)
  9. }
  10. func GetAllowedDashboards(query *m.GetAllowedDashboardsQuery) error {
  11. rawSQL := `select distinct d.id as DashboardId
  12. from dashboard as d
  13. left join dashboard as df on d.parent_id = df.id
  14. left join dashboard_acl as dfa on d.parent_id = dfa.dashboard_id or d.id = dfa.dashboard_id
  15. left join user_group_member as ugm on ugm.user_group_id = dfa.user_group_id
  16. where (
  17. (d.has_acl = 1 and (dfa.user_id = ? or ugm.user_id = ? or df.created_by = ? or (d.is_folder = 1 and d.created_by = ?)))
  18. or d.has_acl = 0)
  19. and d.org_id = ?`
  20. res, err := x.Query(rawSQL, query.UserId, query.UserId, query.UserId, query.UserId, query.OrgId)
  21. if err != nil {
  22. return err
  23. }
  24. query.Result = make([]int64, 0)
  25. for _, dash := range res {
  26. id, err := strconv.ParseInt(string(dash["DashboardId"]), 10, 64)
  27. if err != nil {
  28. return err
  29. }
  30. query.Result = append(query.Result, id)
  31. }
  32. return nil
  33. }