2014-12-22 05:25:08 -06:00
package sqlstore
import (
2021-03-17 10:06:10 -05:00
"context"
"fmt"
2020-04-27 07:16:03 -05:00
"strings"
"time"
2020-04-20 09:20:45 -05:00
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
"github.com/prometheus/client_golang/prometheus"
2015-02-04 04:35:59 -06:00
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/bus"
2019-02-23 16:35:26 -06:00
"github.com/grafana/grafana/pkg/infra/metrics"
2019-08-12 13:03:48 -05:00
"github.com/grafana/grafana/pkg/models"
2015-06-05 01:15:38 -05:00
"github.com/grafana/grafana/pkg/services/search"
2018-01-31 10:27:28 -06:00
"github.com/grafana/grafana/pkg/util"
2014-12-22 05:25:08 -06:00
)
2020-04-20 09:20:45 -05:00
var shadowSearchCounter = prometheus . NewCounterVec (
prometheus . CounterOpts {
Subsystem : "db_dashboard" ,
Name : "search_shadow" ,
} ,
[ ] string { "equal" , "error" } ,
)
2014-12-22 05:25:08 -06:00
func init ( ) {
2014-12-29 06:58:06 -06:00
bus . AddHandler ( "sql" , GetDashboard )
2016-01-28 18:41:23 -06:00
bus . AddHandler ( "sql" , GetDashboards )
2014-12-29 06:58:06 -06:00
bus . AddHandler ( "sql" , DeleteDashboard )
bus . AddHandler ( "sql" , SearchDashboards )
2015-01-06 11:39:26 -06:00
bus . AddHandler ( "sql" , GetDashboardTags )
2016-03-17 03:01:58 -05:00
bus . AddHandler ( "sql" , GetDashboardSlugById )
2018-02-01 06:32:00 -06:00
bus . AddHandler ( "sql" , GetDashboardUIDById )
2016-07-08 02:35:06 -05:00
bus . AddHandler ( "sql" , GetDashboardsByPluginId )
2018-01-30 16:31:02 -06:00
bus . AddHandler ( "sql" , GetDashboardPermissionsForUser )
2018-02-01 07:13:42 -06:00
bus . AddHandler ( "sql" , GetDashboardsBySlug )
2018-04-30 08:34:31 -05:00
bus . AddHandler ( "sql" , HasEditPermissionInFolders )
2019-08-12 13:03:48 -05:00
bus . AddHandler ( "sql" , HasAdminPermissionInFolders )
2020-04-20 09:20:45 -05:00
prometheus . MustRegister ( shadowSearchCounter )
2014-12-22 05:25:08 -06:00
}
2019-01-28 15:37:44 -06:00
var generateNewUid func ( ) string = util . GenerateShortUID
2018-01-31 10:27:28 -06:00
2021-03-17 10:06:10 -05:00
func ( ss * SQLStore ) SaveDashboard ( cmd models . SaveDashboardCommand ) ( * models . Dashboard , error ) {
err := ss . WithTransactionalDbSession ( context . Background ( ) , func ( sess * DBSession ) error {
return saveDashboard ( sess , & cmd )
2018-01-23 05:28:56 -06:00
} )
2021-03-17 10:06:10 -05:00
return cmd . Result , err
2018-01-23 05:28:56 -06:00
}
2017-06-01 16:30:31 -05:00
2019-08-12 13:03:48 -05:00
func saveDashboard ( sess * DBSession , cmd * models . SaveDashboardCommand ) error {
2018-01-23 05:28:56 -06:00
dash := cmd . GetDashboardModel ( )
2015-01-05 10:04:29 -06:00
2018-02-22 04:54:28 -06:00
userId := cmd . UserId
if userId == 0 {
userId = - 1
}
2018-02-19 04:12:56 -06:00
if dash . Id > 0 {
2019-08-12 13:03:48 -05:00
var existing models . Dashboard
2018-02-19 04:12:56 -06:00
dashWithIdExists , err := sess . Where ( "id=? AND org_id=?" , dash . Id , dash . OrgId ) . Get ( & existing )
if err != nil {
return err
}
if ! dashWithIdExists {
2019-08-12 13:03:48 -05:00
return models . ErrDashboardNotFound
2018-02-19 04:12:56 -06:00
}
2015-05-04 01:19:29 -05:00
2018-02-19 04:12:56 -06:00
// check for is someone else has written in between
if dash . Version != existing . Version {
2018-02-08 06:43:19 -06:00
if cmd . Overwrite {
2018-02-19 04:12:56 -06:00
dash . SetVersion ( existing . Version )
2018-02-08 06:43:19 -06:00
} else {
2019-08-12 13:03:48 -05:00
return models . ErrDashboardVersionMismatch
2015-05-04 01:19:29 -05:00
}
}
2018-02-19 04:12:56 -06:00
// do not allow plugin dashboard updates without overwrite flag
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
if existing . PluginId != "" && ! cmd . Overwrite {
2019-08-12 13:03:48 -05:00
return models . UpdatePluginDashboardError { PluginId : existing . PluginId }
2018-02-19 04:12:56 -06:00
}
2018-01-23 05:28:56 -06:00
}
2015-05-04 01:19:29 -05:00
2018-02-08 04:01:09 -06:00
if dash . Uid == "" {
uid , err := generateNewDashboardUid ( sess , dash . OrgId )
2018-01-31 06:47:28 -06:00
if err != nil {
return err
2015-05-04 01:19:29 -05:00
}
2018-02-19 04:12:56 -06:00
dash . SetUid ( uid )
2018-01-23 05:28:56 -06:00
}
parentVersion := dash . Version
2018-04-23 12:28:54 -05:00
var affectedRows int64
2018-02-19 04:12:56 -06:00
var err error
2018-01-23 05:28:56 -06:00
if dash . Id == 0 {
2018-02-19 04:12:56 -06:00
dash . SetVersion ( 1 )
2018-02-22 04:54:28 -06:00
dash . Created = time . Now ( )
dash . CreatedBy = userId
dash . Updated = time . Now ( )
dash . UpdatedBy = userId
2019-07-16 09:58:46 -05:00
metrics . MApiDashboardInsert . Inc ( )
2018-01-23 05:28:56 -06:00
affectedRows , err = sess . Insert ( dash )
} else {
2018-02-22 04:54:28 -06:00
dash . SetVersion ( dash . Version + 1 )
2018-01-23 05:28:56 -06:00
if ! cmd . UpdatedAt . IsZero ( ) {
dash . Updated = cmd . UpdatedAt
2018-02-22 04:54:28 -06:00
} else {
dash . Updated = time . Now ( )
2017-06-23 16:22:09 -05:00
}
2018-02-22 04:54:28 -06:00
dash . UpdatedBy = userId
2018-02-14 08:04:26 -06:00
affectedRows , err = sess . MustCols ( "folder_id" ) . ID ( dash . Id ) . Update ( dash )
2018-01-23 05:28:56 -06:00
}
2017-06-05 09:34:32 -05:00
2018-01-23 05:28:56 -06:00
if err != nil {
return err
}
2017-11-23 04:29:06 -06:00
2018-01-23 05:28:56 -06:00
if affectedRows == 0 {
2019-08-12 13:03:48 -05:00
return models . ErrDashboardNotFound
2018-01-23 05:28:56 -06:00
}
2019-08-12 13:03:48 -05:00
dashVersion := & models . DashboardVersion {
2018-01-23 05:28:56 -06:00
DashboardId : dash . Id ,
ParentVersion : parentVersion ,
RestoredFrom : cmd . RestoredFrom ,
Version : dash . Version ,
Created : time . Now ( ) ,
CreatedBy : dash . UpdatedBy ,
Message : cmd . Message ,
Data : dash . Data ,
}
2017-11-23 04:29:06 -06:00
2018-01-23 05:28:56 -06:00
// insert version entry
if affectedRows , err = sess . Insert ( dashVersion ) ; err != nil {
return err
} else if affectedRows == 0 {
2019-08-12 13:03:48 -05:00
return models . ErrDashboardNotFound
2018-01-23 05:28:56 -06:00
}
2014-12-22 05:25:08 -06:00
2018-01-23 05:28:56 -06:00
// delete existing tags
_ , err = sess . Exec ( "DELETE FROM dashboard_tag WHERE dashboard_id=?" , dash . Id )
if err != nil {
return err
}
2015-01-07 05:37:24 -06:00
2018-01-23 05:28:56 -06:00
// insert new tags
tags := dash . GetTags ( )
if len ( tags ) > 0 {
for _ , tag := range tags {
if _ , err := sess . Insert ( & DashboardTag { DashboardId : dash . Id , Term : tag } ) ; err != nil {
return err
2015-01-07 05:37:24 -06:00
}
2015-05-04 00:46:46 -05:00
}
2018-01-23 05:28:56 -06:00
}
2014-12-22 05:25:08 -06:00
2018-01-23 05:28:56 -06:00
cmd . Result = dash
2017-06-05 09:34:32 -05:00
2021-03-17 10:06:10 -05:00
return nil
2014-12-22 05:25:08 -06:00
}
2018-01-31 10:27:28 -06:00
func generateNewDashboardUid ( sess * DBSession , orgId int64 ) ( string , error ) {
for i := 0 ; i < 3 ; i ++ {
uid := generateNewUid ( )
2019-08-12 13:03:48 -05:00
exists , err := sess . Where ( "org_id=? AND uid=?" , orgId , uid ) . Get ( & models . Dashboard { } )
2018-01-31 10:27:28 -06:00
if err != nil {
return "" , err
}
if ! exists {
return uid , nil
}
}
2019-08-12 13:03:48 -05:00
return "" , models . ErrDashboardFailedGenerateUniqueUid
2018-01-31 10:27:28 -06:00
}
2014-12-22 05:25:08 -06:00
2021-03-17 10:06:10 -05:00
// GetDashboard gets a dashboard.
func ( ss * SQLStore ) GetDashboard ( id , orgID int64 , uid , slug string ) ( * models . Dashboard , error ) {
if id == 0 && slug == "" && uid == "" {
return nil , models . ErrDashboardIdentifierNotSet
}
dashboard := models . Dashboard { Slug : slug , OrgId : orgID , Id : id , Uid : uid }
has , err := ss . engine . Get ( & dashboard )
if err != nil {
return nil , err
} else if ! has {
return nil , models . ErrDashboardNotFound
}
dashboard . SetId ( dashboard . Id )
dashboard . SetUid ( dashboard . Uid )
return & dashboard , nil
}
2021-04-14 04:02:50 -05:00
// GetDashboardByTitle gets a dashboard by its title.
func ( ss * SQLStore ) GetFolderByTitle ( orgID int64 , title string ) ( * models . Dashboard , error ) {
if title == "" {
return nil , models . ErrDashboardIdentifierNotSet
}
// there is a unique constraint on org_id, folder_id, title
// there are no nested folders so the parent folder id is always 0
dashboard := models . Dashboard { OrgId : orgID , FolderId : 0 , Title : title }
has , err := ss . engine . Get ( & dashboard )
if err != nil {
return nil , err
} else if ! has {
return nil , models . ErrDashboardNotFound
}
// if there is a dashboard instead of a folder with that title
if ! dashboard . IsFolder {
return nil , models . ErrDashboardNotFound
}
dashboard . SetId ( dashboard . Id )
dashboard . SetUid ( dashboard . Uid )
return & dashboard , nil
}
2021-03-17 10:06:10 -05:00
// TODO: Remove me
2019-08-12 13:03:48 -05:00
func GetDashboard ( query * models . GetDashboardQuery ) error {
2020-02-17 08:32:20 -06:00
if query . Id == 0 && len ( query . Slug ) == 0 && len ( query . Uid ) == 0 {
return models . ErrDashboardIdentifierNotSet
}
2019-08-12 13:03:48 -05:00
dashboard := models . Dashboard { Slug : query . Slug , OrgId : query . OrgId , Id : query . Id , Uid : query . Uid }
2014-12-22 05:25:08 -06:00
has , err := x . Get ( & dashboard )
2017-06-05 10:45:27 -05:00
2014-12-22 05:25:08 -06:00
if err != nil {
2014-12-29 06:58:06 -06:00
return err
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
} else if ! has {
2019-08-12 13:03:48 -05:00
return models . ErrDashboardNotFound
2014-12-22 05:25:08 -06:00
}
2018-02-19 04:12:56 -06:00
dashboard . SetId ( dashboard . Id )
dashboard . SetUid ( dashboard . Uid )
2014-12-29 06:58:06 -06:00
query . Result = & dashboard
return nil
2014-12-22 05:25:08 -06:00
}
2015-01-07 05:37:24 -06:00
type DashboardSearchProjection struct {
2021-02-11 01:49:16 -06:00
ID int64 ` xorm:"id" `
UID string ` xorm:"uid" `
2017-06-16 20:00:13 -05:00
Title string
Slug string
Term string
IsFolder bool
2021-02-11 01:49:16 -06:00
FolderID int64 ` xorm:"folder_id" `
FolderUID string ` xorm:"folder_uid" `
2017-06-16 20:00:13 -05:00
FolderSlug string
FolderTitle string
2021-02-11 01:49:16 -06:00
SortMeta int64
2015-01-07 05:37:24 -06:00
}
2017-03-27 07:36:28 -05:00
func findDashboards ( query * search . FindPersistedDashboardsQuery ) ( [ ] DashboardSearchProjection , error ) {
2020-04-27 07:16:03 -05:00
filters := [ ] interface { } {
2020-04-20 09:20:45 -05:00
permissions . DashboardPermissionFilter {
OrgRole : query . SignedInUser . OrgRole ,
OrgId : query . SignedInUser . OrgId ,
Dialect : dialect ,
UserId : query . SignedInUser . UserId ,
PermissionLevel : query . Permission ,
} ,
}
2021-02-11 01:49:16 -06:00
for _ , filter := range query . Sort . Filter {
filters = append ( filters , filter )
}
2020-05-06 04:42:52 -05:00
2020-04-27 07:16:03 -05:00
if query . OrgId != 0 {
filters = append ( filters , searchstore . OrgFilter { OrgId : query . OrgId } )
} else if query . SignedInUser . OrgId != 0 {
filters = append ( filters , searchstore . OrgFilter { OrgId : query . SignedInUser . OrgId } )
}
2020-04-20 09:20:45 -05:00
if len ( query . Tags ) > 0 {
2020-04-27 07:16:03 -05:00
filters = append ( filters , searchstore . TagsFilter { Tags : query . Tags } )
2020-04-20 09:20:45 -05:00
}
if len ( query . DashboardIds ) > 0 {
2020-04-27 07:16:03 -05:00
filters = append ( filters , searchstore . DashboardFilter { IDs : query . DashboardIds } )
2020-04-20 09:20:45 -05:00
}
2015-02-04 04:35:59 -06:00
if query . IsStarred {
2020-04-27 07:16:03 -05:00
filters = append ( filters , searchstore . StarredFilter { UserId : query . SignedInUser . UserId } )
2017-06-16 19:33:53 -05:00
}
2015-02-04 04:35:59 -06:00
if len ( query . Title ) > 0 {
2020-04-27 07:16:03 -05:00
filters = append ( filters , searchstore . TitleFilter { Dialect : dialect , Title : query . Title } )
2015-02-04 04:35:59 -06:00
}
2014-12-22 05:25:08 -06:00
2017-11-16 17:16:33 -06:00
if len ( query . Type ) > 0 {
2020-04-27 07:16:03 -05:00
filters = append ( filters , searchstore . TypeFilter { Dialect : dialect , Type : query . Type } )
2017-05-24 11:28:13 -05:00
}
2017-11-20 05:47:03 -06:00
if len ( query . FolderIds ) > 0 {
2020-04-27 07:16:03 -05:00
filters = append ( filters , searchstore . FolderFilter { IDs : query . FolderIds } )
2017-11-17 08:30:21 -06:00
}
2017-11-16 17:16:33 -06:00
var res [ ] DashboardSearchProjection
2020-04-27 07:16:03 -05:00
sb := & searchstore . Builder { Dialect : dialect , Filters : filters }
2017-11-16 17:16:33 -06:00
2020-04-27 07:16:03 -05:00
limit := query . Limit
if limit < 1 {
limit = 1000
2017-06-01 16:30:31 -05:00
}
2020-04-27 07:16:03 -05:00
page := query . Page
if page < 1 {
page = 1
}
2020-04-20 09:20:45 -05:00
2020-11-10 23:21:08 -06:00
sql , params := sb . ToSQL ( limit , page )
2020-04-27 07:16:03 -05:00
err := x . SQL ( sql , params ... ) . Find ( & res )
if err != nil {
return nil , err
2020-04-20 09:20:45 -05:00
}
2017-11-16 17:16:33 -06:00
return res , nil
2017-03-27 07:36:28 -05:00
}
func SearchDashboards ( query * search . FindPersistedDashboardsQuery ) error {
res , err := findDashboards ( query )
2015-01-07 05:37:24 -06:00
if err != nil {
return err
}
2017-06-01 16:30:31 -05:00
makeQueryResult ( query , res )
2015-01-07 05:37:24 -06:00
2017-06-01 16:30:31 -05:00
return nil
2014-12-22 05:25:08 -06:00
}
2017-03-27 07:36:28 -05:00
func getHitType ( item DashboardSearchProjection ) search . HitType {
var hitType search . HitType
if item . IsFolder {
hitType = search . DashHitFolder
} else {
hitType = search . DashHitDB
}
return hitType
}
2017-06-01 16:30:31 -05:00
func makeQueryResult ( query * search . FindPersistedDashboardsQuery , res [ ] DashboardSearchProjection ) {
query . Result = make ( [ ] * search . Hit , 0 )
hits := make ( map [ int64 ] * search . Hit )
for _ , item := range res {
2021-02-11 01:49:16 -06:00
hit , exists := hits [ item . ID ]
2017-06-01 16:30:31 -05:00
if ! exists {
hit = & search . Hit {
2021-02-11 01:49:16 -06:00
ID : item . ID ,
UID : item . UID ,
2017-06-23 15:00:26 -05:00
Title : item . Title ,
2021-02-11 01:49:16 -06:00
URI : "db/" + item . Slug ,
URL : models . GetDashboardFolderUrl ( item . IsFolder , item . UID , item . Slug ) ,
2017-06-23 15:00:26 -05:00
Type : getHitType ( item ) ,
2021-02-11 01:49:16 -06:00
FolderID : item . FolderID ,
FolderUID : item . FolderUID ,
2017-06-23 15:00:26 -05:00
FolderTitle : item . FolderTitle ,
Tags : [ ] string { } ,
2017-06-01 16:30:31 -05:00
}
2018-01-30 08:24:14 -06:00
2021-02-11 01:49:16 -06:00
if item . FolderID > 0 {
hit . FolderURL = models . GetFolderUrl ( item . FolderUID , item . FolderSlug )
}
if query . Sort . MetaName != "" {
2021-02-17 06:06:19 -06:00
hit . SortMeta = item . SortMeta
hit . SortMetaName = query . Sort . MetaName
2018-02-05 06:23:24 -06:00
}
2017-06-01 16:30:31 -05:00
query . Result = append ( query . Result , hit )
2021-02-11 01:49:16 -06:00
hits [ item . ID ] = hit
2017-06-01 16:30:31 -05:00
}
if len ( item . Term ) > 0 {
hit . Tags = append ( hit . Tags , item . Term )
}
}
}
2019-08-12 13:03:48 -05:00
func GetDashboardTags ( query * models . GetDashboardTagsQuery ) error {
2015-01-20 08:23:14 -06:00
sql := ` SELECT
COUNT ( * ) as count ,
term
FROM dashboard
INNER JOIN dashboard_tag on dashboard_tag . dashboard_id = dashboard . id
2015-02-23 13:07:49 -06:00
WHERE dashboard . org_id = ?
2018-04-20 11:17:17 -05:00
GROUP BY term
ORDER BY term `
2015-01-20 08:23:14 -06:00
2019-08-12 13:03:48 -05:00
query . Result = make ( [ ] * models . DashboardTagCloudItem , 0 )
2018-09-16 05:26:05 -05:00
sess := x . SQL ( sql , query . OrgId )
2015-01-07 05:37:24 -06:00
err := sess . Find ( & query . Result )
return err
2015-01-06 11:39:26 -06:00
}
2019-08-12 13:03:48 -05:00
func DeleteDashboard ( cmd * models . DeleteDashboardCommand ) error {
2017-05-23 03:56:23 -05:00
return inTransaction ( func ( sess * DBSession ) error {
2020-09-11 02:19:44 -05:00
return deleteDashboard ( cmd , sess )
} )
}
func deleteDashboard ( cmd * models . DeleteDashboardCommand , sess * DBSession ) error {
dashboard := models . Dashboard { Id : cmd . Id , OrgId : cmd . OrgId }
has , err := sess . Get ( & dashboard )
if err != nil {
return err
} else if ! has {
return models . ErrDashboardNotFound
}
deletes := [ ] string {
"DELETE FROM dashboard_tag WHERE dashboard_id = ? " ,
"DELETE FROM star WHERE dashboard_id = ? " ,
"DELETE FROM dashboard WHERE id = ?" ,
"DELETE FROM playlist_item WHERE type = 'dashboard_by_id' AND value = ?" ,
"DELETE FROM dashboard_version WHERE dashboard_id = ?" ,
"DELETE FROM annotation WHERE dashboard_id = ?" ,
"DELETE FROM dashboard_provisioning WHERE dashboard_id = ?" ,
2020-11-06 02:02:31 -06:00
"DELETE FROM dashboard_acl WHERE dashboard_id = ?" ,
2020-09-11 02:19:44 -05:00
}
if dashboard . IsFolder {
deletes = append ( deletes , "DELETE FROM dashboard WHERE folder_id = ?" )
dashIds := [ ] struct {
Id int64
} { }
2020-11-06 02:02:31 -06:00
err := sess . SQL ( "SELECT id FROM dashboard WHERE folder_id = ?" , dashboard . Id ) . Find ( & dashIds )
2015-08-12 02:23:46 -05:00
if err != nil {
return err
}
2014-12-22 05:25:08 -06:00
2020-09-11 02:19:44 -05:00
for _ , id := range dashIds {
if err := deleteAlertDefinition ( id . Id , sess ) ; err != nil {
2015-08-12 02:23:46 -05:00
return err
}
}
2020-11-06 02:02:31 -06:00
if len ( dashIds ) > 0 {
childrenDeletes := [ ] string {
"DELETE FROM dashboard_tag WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)" ,
"DELETE FROM star WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)" ,
"DELETE FROM dashboard_version WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)" ,
"DELETE FROM annotation WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)" ,
"DELETE FROM dashboard_provisioning WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)" ,
"DELETE FROM dashboard_acl WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)" ,
}
for _ , sql := range childrenDeletes {
_ , err := sess . Exec ( sql , dashboard . OrgId , dashboard . Id )
if err != nil {
return err
}
}
}
2021-05-20 07:49:33 -05:00
// clean ngalert tables
ngalertDeletes := [ ] string {
"DELETE FROM alert_rule WHERE namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)" ,
"DELETE FROM alert_rule_version WHERE rule_namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)" ,
}
for _ , sql := range ngalertDeletes {
_ , err := sess . Exec ( sql , dashboard . Id )
if err != nil {
return err
}
}
2020-09-11 02:19:44 -05:00
}
2015-08-12 02:23:46 -05:00
2020-09-11 02:19:44 -05:00
if err := deleteAlertDefinition ( dashboard . Id , sess ) ; err != nil {
return err
}
2016-04-26 08:48:29 -05:00
2020-09-11 02:19:44 -05:00
for _ , sql := range deletes {
_ , err := sess . Exec ( sql , dashboard . Id )
if err != nil {
return err
2018-11-05 07:24:08 -06:00
}
2020-09-11 02:19:44 -05:00
}
2018-11-05 07:24:08 -06:00
2020-09-11 02:19:44 -05:00
return nil
2014-12-22 05:25:08 -06:00
}
2016-01-28 18:41:23 -06:00
2019-08-12 13:03:48 -05:00
func GetDashboards ( query * models . GetDashboardsQuery ) error {
2016-01-28 18:41:23 -06:00
if len ( query . DashboardIds ) == 0 {
2019-08-12 13:03:48 -05:00
return models . ErrCommandValidationFailed
2016-01-28 18:41:23 -06:00
}
2019-08-12 13:03:48 -05:00
var dashboards = make ( [ ] * models . Dashboard , 0 )
2016-01-28 18:41:23 -06:00
err := x . In ( "id" , query . DashboardIds ) . Find ( & dashboards )
2016-06-23 09:30:12 -05:00
query . Result = dashboards
2018-04-16 12:54:23 -05:00
return err
2016-01-28 18:41:23 -06:00
}
2016-03-17 03:01:58 -05:00
2018-01-30 16:31:02 -06:00
// GetDashboardPermissionsForUser returns the maximum permission the specified user has for a dashboard(s)
// The function takes in a list of dashboard ids and the user id and role
2019-08-12 13:03:48 -05:00
func GetDashboardPermissionsForUser ( query * models . GetDashboardPermissionsForUserQuery ) error {
2018-01-30 16:31:02 -06:00
if len ( query . DashboardIds ) == 0 {
2019-08-12 13:03:48 -05:00
return models . ErrCommandValidationFailed
2018-01-30 16:31:02 -06:00
}
2019-08-12 13:03:48 -05:00
if query . OrgRole == models . ROLE_ADMIN {
var permissions = make ( [ ] * models . DashboardPermissionForUser , 0 )
2018-01-30 16:31:02 -06:00
for _ , d := range query . DashboardIds {
2019-08-12 13:03:48 -05:00
permissions = append ( permissions , & models . DashboardPermissionForUser {
2018-01-30 16:31:02 -06:00
DashboardId : d ,
2019-08-12 13:03:48 -05:00
Permission : models . PERMISSION_ADMIN ,
PermissionName : models . PERMISSION_ADMIN . String ( ) ,
2018-01-30 16:31:02 -06:00
} )
}
query . Result = permissions
return nil
}
params := make ( [ ] interface { } , 0 )
// check dashboards that have ACLs via user id, team id or role
sql := ` SELECT d . id AS dashboard_id , MAX ( COALESCE ( da . permission , pt . permission ) ) AS permission
FROM dashboard AS d
LEFT JOIN dashboard_acl as da on d . folder_id = da . dashboard_id or d . id = da . dashboard_id
LEFT JOIN team_member as ugm on ugm . team_id = da . team_id
LEFT JOIN org_user ou ON ou . role = da . role AND ou . user_id = ?
`
params = append ( params , query . UserId )
2020-09-22 09:22:19 -05:00
// check the user's role for dashboards that do not have hasAcl set
2018-01-30 16:31:02 -06:00
sql += ` LEFT JOIN org_user ouRole ON ouRole.user_id = ? AND ouRole.org_id = ? `
params = append ( params , query . UserId )
params = append ( params , query . OrgId )
sql += `
2018-02-01 10:27:29 -06:00
LEFT JOIN ( SELECT 1 AS permission , ' Viewer ' AS role
UNION SELECT 2 AS permission , ' Editor ' AS role
UNION SELECT 4 AS permission , ' Admin ' AS role ) pt ON ouRole . role = pt . role
2018-01-30 16:31:02 -06:00
WHERE
d . Id IN ( ? ` + strings.Repeat(",?", len(query.DashboardIds)-1) + ` ) `
for _ , id := range query . DashboardIds {
params = append ( params , id )
}
sql += ` AND
d . org_id = ? AND
(
( d . has_acl = ? AND ( da . user_id = ? OR ugm . user_id = ? OR ou . id IS NOT NULL ) )
OR ( d . has_acl = ? AND ouRole . id IS NOT NULL )
)
group by d . id
order by d . id asc `
params = append ( params , query . OrgId )
2018-02-01 10:27:29 -06:00
params = append ( params , dialect . BooleanStr ( true ) )
2018-01-30 16:31:02 -06:00
params = append ( params , query . UserId )
params = append ( params , query . UserId )
params = append ( params , dialect . BooleanStr ( false ) )
2018-09-16 05:26:05 -05:00
err := x . SQL ( sql , params ... ) . Find ( & query . Result )
2018-01-30 16:31:02 -06:00
for _ , p := range query . Result {
p . PermissionName = p . Permission . String ( )
}
return err
}
2019-08-12 13:03:48 -05:00
func GetDashboardsByPluginId ( query * models . GetDashboardsByPluginIdQuery ) error {
var dashboards = make ( [ ] * models . Dashboard , 0 )
2017-12-20 05:15:49 -06:00
whereExpr := "org_id=? AND plugin_id=? AND is_folder=" + dialect . BooleanStr ( false )
2016-07-08 02:35:06 -05:00
2017-12-20 05:15:49 -06:00
err := x . Where ( whereExpr , query . OrgId , query . PluginId ) . Find ( & dashboards )
2016-07-08 02:35:06 -05:00
query . Result = dashboards
2018-04-16 12:54:23 -05:00
return err
2016-01-28 18:41:23 -06:00
}
2016-03-17 03:01:58 -05:00
2016-03-20 05:52:19 -05:00
type DashboardSlugDTO struct {
Slug string
}
2019-08-12 13:03:48 -05:00
func GetDashboardSlugById ( query * models . GetDashboardSlugByIdQuery ) error {
2020-11-10 23:21:08 -06:00
var rawSQL = ` SELECT slug from dashboard WHERE Id=? `
2016-03-20 05:52:19 -05:00
var slug = DashboardSlugDTO { }
2020-11-10 23:21:08 -06:00
exists , err := x . SQL ( rawSQL , query . Id ) . Get ( & slug )
2016-03-17 03:01:58 -05:00
if err != nil {
return err
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
} else if ! exists {
2019-08-12 13:03:48 -05:00
return models . ErrDashboardNotFound
2016-03-17 03:01:58 -05:00
}
2016-03-20 05:52:19 -05:00
query . Result = slug . Slug
2016-03-17 03:01:58 -05:00
return nil
}
2018-01-31 09:51:06 -06:00
2019-08-12 13:03:48 -05:00
func GetDashboardsBySlug ( query * models . GetDashboardsBySlugQuery ) error {
var dashboards [ ] * models . Dashboard
2018-01-31 09:51:06 -06:00
if err := x . Where ( "org_id=? AND slug=?" , query . OrgId , query . Slug ) . Find ( & dashboards ) ; err != nil {
return err
}
query . Result = dashboards
return nil
}
2018-02-01 06:32:00 -06:00
2019-08-12 13:03:48 -05:00
func GetDashboardUIDById ( query * models . GetDashboardRefByIdQuery ) error {
2020-11-10 23:21:08 -06:00
var rawSQL = ` SELECT uid, slug from dashboard WHERE Id=? `
2018-02-01 06:32:00 -06:00
2019-08-12 13:03:48 -05:00
us := & models . DashboardRef { }
2018-02-01 06:32:00 -06:00
2020-11-10 23:21:08 -06:00
exists , err := x . SQL ( rawSQL , query . Id ) . Get ( us )
2018-02-01 06:32:00 -06:00
if err != nil {
return err
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
} else if ! exists {
2019-08-12 13:03:48 -05:00
return models . ErrDashboardNotFound
2018-02-01 06:32:00 -06:00
}
query . Result = us
return nil
}
2018-02-19 04:12:56 -06:00
2021-03-17 10:06:10 -05:00
func getExistingDashboardByIdOrUidForUpdate ( sess * DBSession , dash * models . Dashboard , overwrite bool ) ( bool , error ) {
2018-02-19 04:12:56 -06:00
dashWithIdExists := false
2021-03-17 10:06:10 -05:00
isParentFolderChanged := false
2019-08-12 13:03:48 -05:00
var existingById models . Dashboard
2018-02-19 04:12:56 -06:00
if dash . Id > 0 {
2021-03-17 10:06:10 -05:00
var err error
2018-02-19 04:12:56 -06:00
dashWithIdExists , err = sess . Where ( "id=? AND org_id=?" , dash . Id , dash . OrgId ) . Get ( & existingById )
if err != nil {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , fmt . Errorf ( "SQL query for existing dashboard by ID failed: %w" , err )
2018-02-19 04:12:56 -06:00
}
if ! dashWithIdExists {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , models . ErrDashboardNotFound
2018-02-19 04:12:56 -06:00
}
if dash . Uid == "" {
dash . SetUid ( existingById . Uid )
}
}
dashWithUidExists := false
2019-08-12 13:03:48 -05:00
var existingByUid models . Dashboard
2018-02-19 04:12:56 -06:00
if dash . Uid != "" {
2021-03-17 10:06:10 -05:00
var err error
2018-02-19 04:12:56 -06:00
dashWithUidExists , err = sess . Where ( "org_id=? AND uid=?" , dash . OrgId , dash . Uid ) . Get ( & existingByUid )
if err != nil {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , fmt . Errorf ( "SQL query for existing dashboard by UID failed: %w" , err )
2018-02-19 04:12:56 -06:00
}
}
if dash . FolderId > 0 {
2019-08-12 13:03:48 -05:00
var existingFolder models . Dashboard
2021-03-17 10:06:10 -05:00
folderExists , err := sess . Where ( "org_id=? AND id=? AND is_folder=?" , dash . OrgId , dash . FolderId ,
dialect . BooleanStr ( true ) ) . Get ( & existingFolder )
if err != nil {
return isParentFolderChanged , fmt . Errorf ( "SQL query for folder failed: %w" , err )
2018-02-19 04:12:56 -06:00
}
if ! folderExists {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , models . ErrDashboardFolderNotFound
2018-02-19 04:12:56 -06:00
}
}
if ! dashWithIdExists && ! dashWithUidExists {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , nil
2018-02-19 04:12:56 -06:00
}
if dashWithIdExists && dashWithUidExists && existingById . Id != existingByUid . Id {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , models . ErrDashboardWithSameUIDExists
2018-02-19 04:12:56 -06:00
}
existing := existingById
if ! dashWithIdExists && dashWithUidExists {
dash . SetId ( existingByUid . Id )
dash . SetUid ( existingByUid . Uid )
existing = existingByUid
2018-04-10 17:23:50 -05:00
if ! dash . IsFolder {
2021-03-17 10:06:10 -05:00
isParentFolderChanged = true
2018-04-10 17:23:50 -05:00
}
2018-02-19 04:12:56 -06:00
}
if ( existing . IsFolder && ! dash . IsFolder ) ||
( ! existing . IsFolder && dash . IsFolder ) {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , models . ErrDashboardTypeMismatch
2018-02-19 04:12:56 -06:00
}
2018-04-10 17:23:50 -05:00
if ! dash . IsFolder && dash . FolderId != existing . FolderId {
2021-03-17 10:06:10 -05:00
isParentFolderChanged = true
2018-04-10 17:23:50 -05:00
}
2018-02-19 04:12:56 -06:00
// check for is someone else has written in between
if dash . Version != existing . Version {
2021-03-17 10:06:10 -05:00
if overwrite {
2018-02-19 04:12:56 -06:00
dash . SetVersion ( existing . Version )
} else {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , models . ErrDashboardVersionMismatch
2018-02-19 04:12:56 -06:00
}
}
// do not allow plugin dashboard updates without overwrite flag
2021-03-17 10:06:10 -05:00
if existing . PluginId != "" && ! overwrite {
return isParentFolderChanged , models . UpdatePluginDashboardError { PluginId : existing . PluginId }
2018-02-19 04:12:56 -06:00
}
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , nil
2018-02-19 04:12:56 -06:00
}
2021-03-17 10:06:10 -05:00
func getExistingDashboardByTitleAndFolder ( sess * DBSession , dash * models . Dashboard , overwrite ,
isParentFolderChanged bool ) ( bool , error ) {
2019-08-12 13:03:48 -05:00
var existing models . Dashboard
2021-03-17 10:06:10 -05:00
exists , err := sess . Where ( "org_id=? AND slug=? AND (is_folder=? OR folder_id=?)" , dash . OrgId , dash . Slug ,
dialect . BooleanStr ( true ) , dash . FolderId ) . Get ( & existing )
2018-02-19 04:12:56 -06:00
if err != nil {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , fmt . Errorf ( "SQL query for existing dashboard by org ID or folder ID failed: %w" , err )
2018-02-19 04:12:56 -06:00
}
if exists && dash . Id != existing . Id {
if existing . IsFolder && ! dash . IsFolder {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , models . ErrDashboardWithSameNameAsFolder
2018-02-19 04:12:56 -06:00
}
if ! existing . IsFolder && dash . IsFolder {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , models . ErrDashboardFolderWithSameNameAsDashboard
2018-02-19 04:12:56 -06:00
}
2018-04-10 17:23:50 -05:00
if ! dash . IsFolder && ( dash . FolderId != existing . FolderId || dash . Id == 0 ) {
2021-03-17 10:06:10 -05:00
isParentFolderChanged = true
2018-04-10 17:23:50 -05:00
}
2021-03-17 10:06:10 -05:00
if overwrite {
2018-02-19 04:12:56 -06:00
dash . SetId ( existing . Id )
dash . SetUid ( existing . Uid )
dash . SetVersion ( existing . Version )
} else {
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , models . ErrDashboardWithSameNameInFolderExists
2018-02-19 04:12:56 -06:00
}
}
2021-03-17 10:06:10 -05:00
return isParentFolderChanged , nil
2018-02-19 04:12:56 -06:00
}
2021-03-17 10:06:10 -05:00
func ( ss * SQLStore ) ValidateDashboardBeforeSave ( dashboard * models . Dashboard , overwrite bool ) ( bool , error ) {
isParentFolderChanged := false
err := ss . WithTransactionalDbSession ( context . Background ( ) , func ( sess * DBSession ) error {
var err error
isParentFolderChanged , err = getExistingDashboardByIdOrUidForUpdate ( sess , dashboard , overwrite )
if err != nil {
2018-02-19 04:12:56 -06:00
return err
}
2021-03-17 10:06:10 -05:00
isParentFolderChanged , err = getExistingDashboardByTitleAndFolder ( sess , dashboard , overwrite ,
isParentFolderChanged )
if err != nil {
2018-02-19 04:12:56 -06:00
return err
}
return nil
} )
2021-03-17 10:06:10 -05:00
if err != nil {
return false , err
}
return isParentFolderChanged , nil
2018-02-19 04:12:56 -06:00
}
2018-04-30 08:34:31 -05:00
2019-08-12 13:03:48 -05:00
func HasEditPermissionInFolders ( query * models . HasEditPermissionInFoldersQuery ) error {
if query . SignedInUser . HasRole ( models . ROLE_EDITOR ) {
2018-04-30 08:34:31 -05:00
query . Result = true
return nil
}
2020-11-10 23:21:08 -06:00
builder := & SQLBuilder { }
2021-03-17 10:06:10 -05:00
builder . Write ( "SELECT COUNT(dashboard.id) AS count FROM dashboard WHERE dashboard.org_id = ? AND dashboard.is_folder = ?" ,
query . SignedInUser . OrgId , dialect . BooleanStr ( true ) )
2021-02-24 07:06:22 -06:00
builder . WriteDashboardPermissionFilter ( query . SignedInUser , models . PERMISSION_EDIT )
2019-08-12 13:03:48 -05:00
type folderCount struct {
Count int64
}
resp := make ( [ ] * folderCount , 0 )
2020-11-10 23:21:08 -06:00
if err := x . SQL ( builder . GetSQLString ( ) , builder . params ... ) . Find ( & resp ) ; err != nil {
2019-08-12 13:03:48 -05:00
return err
}
query . Result = len ( resp ) > 0 && resp [ 0 ] . Count > 0
return nil
}
func HasAdminPermissionInFolders ( query * models . HasAdminPermissionInFoldersQuery ) error {
if query . SignedInUser . HasRole ( models . ROLE_ADMIN ) {
query . Result = true
return nil
}
2020-11-10 23:21:08 -06:00
builder := & SQLBuilder { }
2019-08-12 13:03:48 -05:00
builder . Write ( "SELECT COUNT(dashboard.id) AS count FROM dashboard WHERE dashboard.org_id = ? AND dashboard.is_folder = ?" , query . SignedInUser . OrgId , dialect . BooleanStr ( true ) )
2021-02-24 07:06:22 -06:00
builder . WriteDashboardPermissionFilter ( query . SignedInUser , models . PERMISSION_ADMIN )
2018-04-30 08:34:31 -05:00
type folderCount struct {
Count int64
}
resp := make ( [ ] * folderCount , 0 )
2020-11-10 23:21:08 -06:00
if err := x . SQL ( builder . GetSQLString ( ) , builder . params ... ) . Find ( & resp ) ; err != nil {
2018-04-30 08:34:31 -05:00
return err
}
query . Result = len ( resp ) > 0 && resp [ 0 ] . Count > 0
return nil
}