From 60ed6bfc3320c2f8ee59737ab6e2aa1ee95861cc Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Wed, 24 Apr 2024 12:23:08 +0200 Subject: [PATCH] Search: Fix slow query when user does not have roles assigned (#86791) * Search: Fix slow query when user does not have roles assigned * Check all required actions and skip if not found --- pkg/services/sqlstore/permissions/dashboard.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/services/sqlstore/permissions/dashboard.go b/pkg/services/sqlstore/permissions/dashboard.go index 38f8ad92a60..293a19b6bcf 100644 --- a/pkg/services/sqlstore/permissions/dashboard.go +++ b/pkg/services/sqlstore/permissions/dashboard.go @@ -114,8 +114,21 @@ func (f *accessControlDashboardPermissionFilter) Where() (string, []any) { return f.where.string, f.where.params } +// Check if user has no permissions required for search to skip expensive query +func (f *accessControlDashboardPermissionFilter) hasRequiredActions() bool { + permissions := f.user.GetPermissions() + requiredActions := append(f.folderActions, f.dashboardActions...) + for _, action := range requiredActions { + if _, ok := permissions[action]; ok { + return true + } + } + + return false +} + func (f *accessControlDashboardPermissionFilter) buildClauses() { - if f.user == nil || f.user.IsNil() || len(f.user.GetPermissions()) == 0 { + if f.user == nil || f.user.IsNil() || !f.hasRequiredActions() { f.where = clause{string: "(1 = 0)"} return }