@ -39,7 +39,7 @@ func NewAuthService(db db.DB, features featuremgmt.FeatureToggles) *AuthService
}
// Authorize checks if the user has permission to read annotations, then returns a struct containing dashboards and scope types that the user has access to.
func ( authz * AuthService ) Authorize ( ctx context . Context , orgID int64 , query * annotations . ItemQuery ) ( * AccessResources , error ) {
func ( authz * AuthService ) Authorize ( ctx context . Context , query * annotations . ItemQuery ) ( * AccessResources , error ) {
user := query . SignedInUser
if user == nil || user . IsNil ( ) {
return nil , ErrReadForbidden . Errorf ( "missing user" )
@ -60,14 +60,14 @@ func (authz *AuthService) Authorize(ctx context.Context, orgID int64, query *ann
var err error
if canAccessDashAnnotations {
if query . AnnotationID != 0 {
annotationDashboardID , err := authz . getAnnotationDashboard ( ctx , query , orgID )
annotationDashboardID , err := authz . getAnnotationDashboard ( ctx , query )
if err != nil {
return nil , ErrAccessControlInternal . Errorf ( "failed to fetch annotations: %w" , err )
}
query . DashboardID = annotationDashboardID
}
visibleDashboards , err = authz . dashboardsWithVisibleAnnotations ( ctx , query , orgID )
visibleDashboards , err = authz . dashboardsWithVisibleAnnotations ( ctx , query )
if err != nil {
return nil , ErrAccessControlInternal . Errorf ( "failed to fetch dashboards: %w" , err )
}
@ -80,7 +80,7 @@ func (authz *AuthService) Authorize(ctx context.Context, orgID int64, query *ann
} , nil
}
func ( authz * AuthService ) getAnnotationDashboard ( ctx context . Context , query * annotations . ItemQuery , orgID int64 ) ( int64 , error ) {
func ( authz * AuthService ) getAnnotationDashboard ( ctx context . Context , query * annotations . ItemQuery ) ( int64 , error ) {
var items [ ] annotations . Item
params := make ( [ ] any , 0 )
err := authz . db . WithDbSession ( ctx , func ( sess * db . Session ) error {
@ -92,7 +92,7 @@ func (authz *AuthService) getAnnotationDashboard(ctx context.Context, query *ann
FROM annotation as a
WHERE a . org_id = ? AND a . id = ?
`
params = append ( params , o rgID, query . AnnotationID )
params = append ( params , query . O rgID, query . AnnotationID )
return sess . SQL ( sql , params ... ) . Find ( & items )
} )
@ -106,7 +106,7 @@ func (authz *AuthService) getAnnotationDashboard(ctx context.Context, query *ann
return items [ 0 ] . DashboardID , nil
}
func ( authz * AuthService ) dashboardsWithVisibleAnnotations ( ctx context . Context , query * annotations . ItemQuery , orgID int64 ) ( map [ string ] int64 , error ) {
func ( authz * AuthService ) dashboardsWithVisibleAnnotations ( ctx context . Context , query * annotations . ItemQuery ) ( map [ string ] int64 , error ) {
recursiveQueriesSupported , err := authz . db . RecursiveQueriesAreSupported ( )
if err != nil {
return nil , err
@ -119,7 +119,7 @@ func (authz *AuthService) dashboardsWithVisibleAnnotations(ctx context.Context,
filters := [ ] any {
permissions . NewAccessControlDashboardPermissionFilter ( query . SignedInUser , dashboardaccess . PERMISSION_VIEW , filterType , authz . features , recursiveQueriesSupported ) ,
searchstore . OrgFilter { OrgId : o rgID} ,
searchstore . OrgFilter { OrgId : query . O rgID} ,
}
if query . DashboardUID != "" {
@ -134,32 +134,25 @@ func (authz *AuthService) dashboardsWithVisibleAnnotations(ctx context.Context,
}
sb := & searchstore . Builder { Dialect : authz . db . GetDialect ( ) , Filters : filters , Features : authz . features }
visibleDashboards := make ( map [ string ] int64 )
var page int64 = 1
// This is a limit for a batch size, not for the end query result.
var limit int64 = 1000
for {
var res [ ] dashboardProjection
sql , params := sb . ToSQL ( limit , page )
err = authz . db . WithDbSession ( ctx , func ( sess * db . Session ) error {
return sess . SQL ( sql , params ... ) . Find ( & res )
} )
if err != nil {
return nil , err
}
if query . Page == 0 {
query . Page = 1
}
sql , params := sb . ToSQL ( limit , query . Page )
for _ , p := range res {
visibleDashboards [ p . UID ] = p . ID
}
visibleDashboards := make ( map [ string ] int64 )
var res [ ] dashboardProjection
// if the result is less than the limit, we have reached the end
if len ( res ) < int ( limit ) {
break
}
err = authz . db . WithDbSession ( ctx , func ( sess * db . Session ) error {
return sess . SQL ( sql , params ... ) . Find ( & res )
} )
if err != nil {
return nil , err
}
page ++
for _ , p := range res {
visibleDashboards [ p . UID ] = p . ID
}
return visibleDashboards , nil