2022-12-16 10:09:06 -06:00
|
|
|
package db
|
2018-02-07 10:54:21 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2023-09-06 04:16:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth/identity"
|
2023-01-26 07:46:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2023-04-06 03:16:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-12-16 10:09:06 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
2022-08-10 03:32:03 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-02-07 10:54:21 -06:00
|
|
|
)
|
|
|
|
|
2023-04-06 03:16:15 -05:00
|
|
|
func NewSqlBuilder(cfg *setting.Cfg, features featuremgmt.FeatureToggles, dialect migrator.Dialect, recursiveQueriesAreSupported bool) SQLBuilder {
|
|
|
|
return SQLBuilder{cfg: cfg, features: features, dialect: dialect, recursiveQueriesAreSupported: recursiveQueriesAreSupported}
|
2022-08-10 03:32:03 -05:00
|
|
|
}
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
type SQLBuilder struct {
|
2023-04-06 03:16:15 -05:00
|
|
|
cfg *setting.Cfg
|
|
|
|
features featuremgmt.FeatureToggles
|
|
|
|
sql bytes.Buffer
|
2023-08-30 10:46:47 -05:00
|
|
|
params []any
|
2023-08-02 02:39:25 -05:00
|
|
|
leftJoin string
|
2023-04-06 03:16:15 -05:00
|
|
|
recQry string
|
2023-08-30 10:46:47 -05:00
|
|
|
recQryParams []any
|
2023-04-06 03:16:15 -05:00
|
|
|
recursiveQueriesAreSupported bool
|
|
|
|
|
2022-12-16 10:09:06 -06:00
|
|
|
dialect migrator.Dialect
|
2018-02-07 10:54:21 -06:00
|
|
|
}
|
|
|
|
|
2023-08-30 10:46:47 -05:00
|
|
|
func (sb *SQLBuilder) Write(sql string, params ...any) {
|
2018-02-16 06:56:04 -06:00
|
|
|
sb.sql.WriteString(sql)
|
|
|
|
|
|
|
|
if len(params) > 0 {
|
|
|
|
sb.params = append(sb.params, params...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
func (sb *SQLBuilder) GetSQLString() string {
|
2023-04-06 03:16:15 -05:00
|
|
|
if sb.recQry == "" {
|
|
|
|
return sb.sql.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
var bf bytes.Buffer
|
|
|
|
bf.WriteString(sb.recQry)
|
|
|
|
bf.WriteString(sb.sql.String())
|
2023-08-02 02:39:25 -05:00
|
|
|
if sb.leftJoin != "" {
|
|
|
|
bf.WriteString(" LEFT OUTER JOIN " + sb.leftJoin)
|
|
|
|
}
|
2023-04-06 03:16:15 -05:00
|
|
|
return bf.String()
|
2018-02-16 06:56:04 -06:00
|
|
|
}
|
|
|
|
|
2023-08-30 10:46:47 -05:00
|
|
|
func (sb *SQLBuilder) GetParams() []any {
|
2023-04-06 03:16:15 -05:00
|
|
|
if len(sb.recQryParams) == 0 {
|
|
|
|
return sb.params
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.params = append(sb.recQryParams, sb.params...)
|
2021-02-24 07:06:22 -06:00
|
|
|
return sb.params
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:46:47 -05:00
|
|
|
func (sb *SQLBuilder) AddParams(params ...any) {
|
2018-02-16 06:56:04 -06:00
|
|
|
sb.params = append(sb.params, params...)
|
|
|
|
}
|
|
|
|
|
2023-09-06 04:16:10 -05:00
|
|
|
func (sb *SQLBuilder) WriteDashboardPermissionFilter(user identity.Requester, permission dashboards.PermissionType, queryType string) {
|
2022-08-10 03:32:03 -05:00
|
|
|
var (
|
2023-04-06 03:16:15 -05:00
|
|
|
sql string
|
2023-08-30 10:46:47 -05:00
|
|
|
params []any
|
2023-04-06 03:16:15 -05:00
|
|
|
recQry string
|
2023-08-30 10:46:47 -05:00
|
|
|
recQryParams []any
|
2023-08-02 02:39:25 -05:00
|
|
|
leftJoin string
|
2022-08-10 03:32:03 -05:00
|
|
|
)
|
2023-07-10 07:14:21 -05:00
|
|
|
|
|
|
|
filterRBAC := permissions.NewAccessControlDashboardPermissionFilter(user, permission, queryType, sb.features, sb.recursiveQueriesAreSupported)
|
2023-08-02 02:39:25 -05:00
|
|
|
leftJoin = filterRBAC.LeftJoin()
|
2023-07-10 07:14:21 -05:00
|
|
|
sql, params = filterRBAC.Where()
|
|
|
|
recQry, recQryParams = filterRBAC.With()
|
2018-02-07 10:54:21 -06:00
|
|
|
|
2022-08-10 03:32:03 -05:00
|
|
|
sb.sql.WriteString(" AND " + sql)
|
|
|
|
sb.params = append(sb.params, params...)
|
2023-04-06 03:16:15 -05:00
|
|
|
sb.recQry = recQry
|
|
|
|
sb.recQryParams = recQryParams
|
2023-08-02 02:39:25 -05:00
|
|
|
sb.leftJoin = leftJoin
|
2018-02-07 10:54:21 -06:00
|
|
|
}
|