2018-02-07 10:54:21 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SqlBuilder struct {
|
|
|
|
sql bytes.Buffer
|
|
|
|
params []interface{}
|
|
|
|
}
|
|
|
|
|
2018-02-16 06:56:04 -06:00
|
|
|
func (sb *SqlBuilder) Write(sql string, params ...interface{}) {
|
|
|
|
sb.sql.WriteString(sql)
|
|
|
|
|
|
|
|
if len(params) > 0 {
|
|
|
|
sb.params = append(sb.params, params...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *SqlBuilder) GetSqlString() string {
|
|
|
|
return sb.sql.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *SqlBuilder) AddParams(params ...interface{}) {
|
|
|
|
sb.params = append(sb.params, params...)
|
|
|
|
}
|
|
|
|
|
2018-02-08 10:11:01 -06:00
|
|
|
func (sb *SqlBuilder) writeDashboardPermissionFilter(user *m.SignedInUser, permission m.PermissionType) {
|
2018-02-07 10:54:21 -06:00
|
|
|
|
|
|
|
if user.OrgRole == m.ROLE_ADMIN {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
okRoles := []interface{}{user.OrgRole}
|
|
|
|
|
|
|
|
if user.OrgRole == m.ROLE_EDITOR {
|
|
|
|
okRoles = append(okRoles, m.ROLE_VIEWER)
|
|
|
|
}
|
|
|
|
|
2018-02-13 09:49:00 -06:00
|
|
|
falseStr := dialect.BooleanStr(false)
|
|
|
|
|
2018-02-07 10:54:21 -06:00
|
|
|
sb.sql.WriteString(` AND
|
|
|
|
(
|
2018-02-13 09:49:00 -06:00
|
|
|
dashboard.id IN (
|
2018-02-07 10:54:21 -06:00
|
|
|
SELECT distinct d.id AS DashboardId
|
|
|
|
FROM dashboard AS d
|
2018-02-13 09:49:00 -06:00
|
|
|
LEFT JOIN dashboard folder on folder.id = d.folder_id
|
|
|
|
LEFT JOIN dashboard_acl AS da ON
|
|
|
|
da.dashboard_id = d.id OR
|
|
|
|
da.dashboard_id = d.folder_id OR
|
|
|
|
(
|
|
|
|
-- include default permissions -->
|
|
|
|
da.org_id = -1 AND (
|
|
|
|
(folder.id IS NOT NULL AND folder.has_acl = ` + falseStr + `) OR
|
|
|
|
(folder.id IS NULL AND d.has_acl = ` + falseStr + `)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
LEFT JOIN team_member as ugm on ugm.team_id = da.team_id
|
2018-02-07 10:54:21 -06:00
|
|
|
WHERE
|
|
|
|
d.org_id = ? AND
|
|
|
|
da.permission >= ? AND
|
2018-02-13 09:49:00 -06:00
|
|
|
(
|
|
|
|
da.user_id = ? OR
|
|
|
|
ugm.user_id = ? OR
|
|
|
|
da.role IN (?` + strings.Repeat(",?", len(okRoles)-1) + `)
|
|
|
|
)
|
2018-02-07 10:54:21 -06:00
|
|
|
)
|
|
|
|
)`)
|
|
|
|
|
2018-02-08 10:11:01 -06:00
|
|
|
sb.params = append(sb.params, user.OrgId, permission, user.UserId, user.UserId)
|
2018-02-07 10:54:21 -06:00
|
|
|
sb.params = append(sb.params, okRoles...)
|
|
|
|
}
|