@ -20,6 +20,7 @@ import (
"github.com/grafana/grafana/pkg/kinds/dashboard"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/auth/identity"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/dashboards"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
@ -44,15 +45,27 @@ func (hs *HTTPServer) isDashboardStarredByUser(c *contextmodel.ReqContext, dashI
return false , nil
}
query := star . IsStarredByUserQuery { UserID : c . UserID , DashboardID : dashID }
namespaceID , userIDstr := c . SignedInUser . GetNamespacedID ( )
if namespaceID != identity . NamespaceUser {
return false , nil
}
userID , err := identity . IntIdentifier ( namespaceID , userIDstr )
if err != nil {
return false , err
}
query := star . IsStarredByUserQuery { UserID : userID , DashboardID : dashID }
return hs . starService . IsStarredByUser ( c . Req . Context ( ) , & query )
}
func dashboardGuardianResponse ( err error ) response . Response {
if err != nil {
return response . Error ( 500 , "Error while checking dashboard permissions" , err )
return response . Error ( http . StatusInternalServerError , "Error while checking dashboard permissions" , err )
}
return response . Error ( 403 , "Access denied to this dashboard" , nil )
return response . Error ( http . StatusForbidden , "Access denied to this dashboard" , nil )
}
// swagger:route POST /dashboards/trim dashboards trimDashboard
@ -95,7 +108,7 @@ func (hs *HTTPServer) TrimDashboard(c *contextmodel.ReqContext) response.Respons
// 500: internalServerError
func ( hs * HTTPServer ) GetDashboard ( c * contextmodel . ReqContext ) response . Response {
uid := web . Params ( c . Req ) [ ":uid" ]
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . OrgID , 0 , uid )
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . SignedInUser . Get OrgID( ) , 0 , uid )
if rsp != nil {
return rsp
}
@ -108,9 +121,9 @@ func (hs *HTTPServer) GetDashboard(c *contextmodel.ReqContext) response.Response
// If public dashboards is enabled and we have a public dashboard, update meta
// values
if hs . Features . IsEnabled ( featuremgmt . FlagPublicDashboards ) {
publicDashboard , err := hs . PublicDashboardsApi . PublicDashboardService . FindByDashboardUid ( c . Req . Context ( ) , c . OrgID , dash . UID )
publicDashboard , err := hs . PublicDashboardsApi . PublicDashboardService . FindByDashboardUid ( c . Req . Context ( ) , c . SignedInUser . Get OrgID( ) , dash . UID )
if err != nil && ! errors . Is ( err , publicdashboardModels . ErrPublicDashboardNotFound ) {
return response . Error ( 500 , "Error while retrieving public dashboards" , err )
return response . Error ( http . StatusInternalServerError , "Error while retrieving public dashboards" , err )
}
if publicDashboard != nil {
@ -128,10 +141,10 @@ func (hs *HTTPServer) GetDashboard(c *contextmodel.ReqContext) response.Response
}
}
if isEmptyData {
return response . Error ( 500 , "Error while loading dashboard, dashboard data is invalid" , nil )
return response . Error ( http . StatusInternalServerError , "Error while loading dashboard, dashboard data is invalid" , nil )
}
}
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . OrgID , c . SignedInUser )
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . SignedInUser . Get OrgID( ) , c . SignedInUser )
if err != nil {
return response . Err ( err )
}
@ -146,7 +159,7 @@ func (hs *HTTPServer) GetDashboard(c *contextmodel.ReqContext) response.Response
isStarred , err := hs . isDashboardStarredByUser ( c , dash . ID )
if err != nil {
return response . Error ( 500 , "Error while checking if dashboard was starred by user" , err )
return response . Error ( http . StatusInternalServerError , "Error while checking if dashboard was starred by user" , err )
}
// Finding creator and last updater of the dashboard
updater , creator := anonString , anonString
@ -186,13 +199,13 @@ func (hs *HTTPServer) GetDashboard(c *contextmodel.ReqContext) response.Response
// lookup folder title
if dash . FolderID > 0 {
query := dashboards . GetDashboardQuery { ID : dash . FolderID , OrgID : c . OrgID }
query := dashboards . GetDashboardQuery { ID : dash . FolderID , OrgID : c . SignedInUser . Get OrgID( ) }
queryResult , err := hs . DashboardService . GetDashboard ( c . Req . Context ( ) , & query )
if err != nil {
if errors . Is ( err , dashboards . ErrFolderNotFound ) {
return response . Error ( 404 , "Folder not found" , err )
return response . Error ( http . StatusNotFound , "Folder not found" , err )
}
return response . Error ( 500 , "Dashboard folder could not be read" , err )
return response . Error ( http . StatusInternalServerError , "Dashboard folder could not be read" , err )
}
meta . FolderUid = queryResult . UID
meta . FolderTitle = queryResult . Title
@ -201,7 +214,7 @@ func (hs *HTTPServer) GetDashboard(c *contextmodel.ReqContext) response.Response
provisioningData , err := hs . dashboardProvisioningService . GetProvisionedDashboardDataByDashboardID ( c . Req . Context ( ) , dash . ID )
if err != nil {
return response . Error ( 500 , "Error while checking if dashboard is provisioned" , err )
return response . Error ( http . StatusInternalServerError , "Error while checking if dashboard is provisioned" , err )
}
if provisioningData != nil {
@ -275,7 +288,7 @@ func (hs *HTTPServer) getDashboardHelper(ctx context.Context, orgID int64, id in
queryResult , err := hs . DashboardService . GetDashboard ( ctx , & query )
if err != nil {
return nil , response . Error ( 404 , "Dashboard not found" , err )
return nil , response . Error ( http . StatusNotFound , "Dashboard not found" , err )
}
return queryResult , nil
@ -298,11 +311,11 @@ func (hs *HTTPServer) DeleteDashboardByUID(c *contextmodel.ReqContext) response.
}
func ( hs * HTTPServer ) deleteDashboard ( c * contextmodel . ReqContext ) response . Response {
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . OrgID , 0 , web . Params ( c . Req ) [ ":uid" ] )
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . SignedInUser . Get OrgID( ) , 0 , web . Params ( c . Req ) [ ":uid" ] )
if rsp != nil {
return rsp
}
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . OrgID , c . SignedInUser )
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . SignedInUser . Get OrgID( ) , c . SignedInUser )
if err != nil {
return response . Err ( err )
}
@ -311,10 +324,17 @@ func (hs *HTTPServer) deleteDashboard(c *contextmodel.ReqContext) response.Respo
return dashboardGuardianResponse ( err )
}
namespaceID , userIDStr := c . SignedInUser . GetNamespacedID ( )
// disconnect all library elements for this dashboard
err = hs . LibraryElementService . DisconnectElementsFromDashboard ( c . Req . Context ( ) , dash . ID )
if err != nil {
hs . log . Error ( "Failed to disconnect library elements" , "dashboard" , dash . ID , "user" , c . SignedInUser . UserID , "error" , err )
hs . log . Error (
"Failed to disconnect library elements" ,
"dashboard" , dash . ID ,
"namespaceID" , namespaceID ,
"user" , userIDStr ,
"error" , err )
}
// deletes all related public dashboard entities
@ -323,7 +343,7 @@ func (hs *HTTPServer) deleteDashboard(c *contextmodel.ReqContext) response.Respo
hs . log . Error ( "Failed to delete public dashboard" )
}
err = hs . DashboardService . DeleteDashboard ( c . Req . Context ( ) , dash . ID , c . OrgID )
err = hs . DashboardService . DeleteDashboard ( c . Req . Context ( ) , dash . ID , c . SignedInUser . Get OrgID( ) )
if err != nil {
var dashboardErr dashboards . DashboardErr
if ok := errors . As ( err , & dashboardErr ) ; ok {
@ -331,11 +351,16 @@ func (hs *HTTPServer) deleteDashboard(c *contextmodel.ReqContext) response.Respo
return response . Error ( dashboardErr . StatusCode , dashboardErr . Error ( ) , err )
}
}
return response . Error ( 500 , "Failed to delete dashboard" , err )
return response . Error ( http . StatusInternalServerError , "Failed to delete dashboard" , err )
}
userDTODisplay , err := user . NewUserDisplayDTOFromRequester ( c . SignedInUser )
if err != nil {
return response . Error ( http . StatusInternalServerError , "Error while parsing the user DTO model" , err )
}
if hs . Live != nil {
err := hs . Live . GrafanaScope . Dashboards . DashboardDeleted ( c . OrgID , c . ToUserDisplayDTO ( ) , dash . UID )
err := hs . Live . GrafanaScope . Dashboards . DashboardDeleted ( c . SignedInUser . GetOrgID ( ) , userDTODisplay , dash . UID )
if err != nil {
hs . log . Error ( "Failed to broadcast delete info" , "dashboard" , dash . UID , "error" , err )
}
@ -396,19 +421,30 @@ func (hs *HTTPServer) PostDashboard(c *contextmodel.ReqContext) response.Respons
func ( hs * HTTPServer ) postDashboard ( c * contextmodel . ReqContext , cmd dashboards . SaveDashboardCommand ) response . Response {
ctx := c . Req . Context ( )
var err error
cmd . OrgID = c . OrgID
cmd . UserID = c . UserID
namespaceID , userIDstr := c . SignedInUser . GetNamespacedID ( )
if namespaceID != identity . NamespaceUser && namespaceID != identity . NamespaceServiceAccount {
hs . log . Warn ( "User does not belong to a user or service account namespace" , "namespaceID" , namespaceID , "userID" , userIDstr )
return response . Error ( http . StatusBadRequest , "User does not belong to a user or service account namespace" , nil )
}
userID , err := identity . IntIdentifier ( namespaceID , userIDstr )
if err != nil {
hs . log . Warn ( "Error while parsing user ID" , "namespaceID" , namespaceID , "userID" , userIDstr )
}
cmd . OrgID = c . SignedInUser . GetOrgID ( )
cmd . UserID = userID
if cmd . FolderUID != "" {
folder , err := hs . folderService . Get ( ctx , & folder . GetFolderQuery {
OrgID : c . OrgID ,
OrgID : c . SignedInUser . Get OrgID( ) ,
UID : & cmd . FolderUID ,
SignedInUser : c . SignedInUser ,
} )
if err != nil {
if errors . Is ( err , dashboards . ErrFolderNotFound ) {
return response . Error ( 400 , "Folder not found" , err )
return response . Error ( http . StatusBadRequest , "Folder not found" , err )
}
return response . Error ( 500 , "Error while checking folder ID" , err )
return response . Error ( http . StatusInternalServerError , "Error while checking folder ID" , err )
}
cmd . FolderID = folder . ID
}
@ -418,10 +454,10 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
if newDashboard {
limitReached , err := hs . QuotaService . QuotaReached ( c , dashboards . QuotaTargetSrv )
if err != nil {
return response . Error ( 500 , "failed to get quota" , err )
return response . Error ( http . StatusInternalServerError , "failed to get quota" , err )
}
if limitReached {
return response . Error ( 403 , "Quota reached" , nil )
return response . Error ( http . StatusForbidden , "Quota reached" , nil )
}
}
@ -429,13 +465,13 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
if dash . ID != 0 {
data , err := hs . dashboardProvisioningService . GetProvisionedDashboardDataByDashboardID ( c . Req . Context ( ) , dash . ID )
if err != nil {
return response . Error ( 500 , "Error while checking if dashboard is provisioned using ID" , err )
return response . Error ( http . StatusInternalServerError , "Error while checking if dashboard is provisioned using ID" , err )
}
provisioningData = data
} else if dash . UID != "" {
data , err := hs . dashboardProvisioningService . GetProvisionedDashboardDataByDashboardUID ( c . Req . Context ( ) , dash . OrgID , dash . UID )
if err != nil && ! errors . Is ( err , dashboards . ErrProvisionedDashboardNotFound ) && ! errors . Is ( err , dashboards . ErrDashboardNotFound ) {
return response . Error ( 500 , "Error while checking if dashboard is provisioned" , err )
return response . Error ( http . StatusInternalServerError , "Error while checking if dashboard is provisioned" , err )
}
provisioningData = data
}
@ -448,7 +484,7 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
dashItem := & dashboards . SaveDashboardDTO {
Dashboard : dash ,
Message : cmd . Message ,
OrgID : c . OrgID ,
OrgID : c . SignedInUser . Get OrgID( ) ,
User : c . SignedInUser ,
Overwrite : cmd . Overwrite ,
}
@ -461,14 +497,19 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
dashboard = dash // the original request
}
userDTODisplay , err := user . NewUserDisplayDTOFromRequester ( c . SignedInUser )
if err != nil {
return response . Error ( http . StatusInternalServerError , "Error while parsing the user DTO model" , err )
}
// This will broadcast all save requests only if a `gitops` observer exists.
// gitops is useful when trying to save dashboards in an environment where the user can not save
channel := hs . Live . GrafanaScope . Dashboards
liveerr := channel . DashboardSaved ( c . SignedInUser . OrgID , c . SignedInUser . ToUserDisplayDTO ( ) , cmd . Message , dashboard , err )
liveerr := channel . DashboardSaved ( c . SignedInUser . GetOrgID ( ) , userDTODisplay , cmd . Message , dashboard , err )
// When an error exists, but the value broadcast to a gitops listener return 202
if liveerr == nil && err != nil && channel . HasGitOpsObserver ( c . SignedInUser . OrgID ) {
return response . JSON ( 202 , util . DynMap {
if liveerr == nil && err != nil && channel . HasGitOpsObserver ( c . SignedInUser . GetOrgID ( ) ) {
return response . JSON ( http . StatusAccepted , util . DynMap {
"status" : "pending" ,
"message" : "changes were broadcast to the gitops listener" ,
} )
@ -492,7 +533,7 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
// connect library panels for this dashboard after the dashboard is stored and has an ID
err = hs . LibraryPanelService . ConnectLibraryPanelsForDashboard ( ctx , c . SignedInUser , dashboard )
if err != nil {
return response . Error ( 500 , "Error while connecting library panels" , err )
return response . Error ( http . StatusInternalServerError , "Error while connecting library panels" , err )
}
c . TimeRequest ( metrics . MApiDashboardSave )
@ -515,12 +556,22 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
// 401: unauthorisedError
// 500: internalServerError
func ( hs * HTTPServer ) GetHomeDashboard ( c * contextmodel . ReqContext ) response . Response {
prefsQuery := pref . GetPreferenceWithDefaultsQuery { OrgID : c . OrgID , UserID : c . SignedInUser . UserID , Teams : c . Teams }
namespaceID , userIDstr := c . SignedInUser . GetNamespacedID ( )
if namespaceID != identity . NamespaceUser {
return response . Error ( http . StatusBadRequest , "User does not belong to a user namespace" , nil )
}
userID , err := identity . IntIdentifier ( namespaceID , userIDstr )
if err != nil {
hs . log . Warn ( "Error while parsing user ID" , "namespaceID" , namespaceID , "userID" , userIDstr , "err" , err )
}
prefsQuery := pref . GetPreferenceWithDefaultsQuery { OrgID : c . SignedInUser . GetOrgID ( ) , UserID : userID , Teams : c . SignedInUser . GetTeams ( ) }
homePage := hs . Cfg . HomePage
preference , err := hs . preferenceService . GetWithDefaults ( c . Req . Context ( ) , & prefsQuery )
if err != nil {
return response . Error ( 500 , "Failed to get preferences" , err )
return response . Error ( http . StatusInternalServerError , "Failed to get preferences" , err )
}
if preference . HomeDashboardID == 0 && len ( homePage ) > 0 {
@ -549,7 +600,7 @@ func (hs *HTTPServer) GetHomeDashboard(c *contextmodel.ReqContext) response.Resp
// nolint:gosec
file , err := os . Open ( filePath )
if err != nil {
return response . Error ( 500 , "Failed to load home dashboard" , err )
return response . Error ( http . StatusInternalServerError , "Failed to load home dashboard" , err )
}
defer func ( ) {
if err := file . Close ( ) ; err != nil {
@ -564,7 +615,7 @@ func (hs *HTTPServer) GetHomeDashboard(c *contextmodel.ReqContext) response.Resp
jsonParser := json . NewDecoder ( file )
if err := jsonParser . Decode ( dash . Dashboard ) ; err != nil {
return response . Error ( 500 , "Failed to load home dashboard" , err )
return response . Error ( http . StatusInternalServerError , "Failed to load home dashboard" , err )
}
hs . addGettingStartedPanelToHomeDashboard ( c , dash . Dashboard )
@ -636,12 +687,12 @@ func (hs *HTTPServer) GetDashboardVersions(c *contextmodel.ReqContext) response.
}
}
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . OrgID , dashID , dashUID )
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . SignedInUser . Get OrgID( ) , dashID , dashUID )
if rsp != nil {
return rsp
}
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . OrgID , c . SignedInUser )
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . SignedInUser . Get OrgID( ) , c . SignedInUser )
if err != nil {
return response . Err ( err )
}
@ -650,7 +701,7 @@ func (hs *HTTPServer) GetDashboardVersions(c *contextmodel.ReqContext) response.
}
query := dashver . ListDashboardVersionsQuery {
OrgID : c . OrgID ,
OrgID : c . SignedInUser . Get OrgID( ) ,
DashboardID : dash . ID ,
DashboardUID : dash . UID ,
Limit : c . QueryInt ( "limit" ) ,
@ -659,7 +710,7 @@ func (hs *HTTPServer) GetDashboardVersions(c *contextmodel.ReqContext) response.
versions , err := hs . dashboardVersionService . List ( c . Req . Context ( ) , & query )
if err != nil {
return response . Error ( 404 , fmt . Sprintf ( "No versions found for dashboardId %d" , dash . ID ) , err )
return response . Error ( http . StatusNotFound , fmt . Sprintf ( "No versions found for dashboardId %d" , dash . ID ) , err )
}
loginMem := make ( map [ int64 ] string , len ( versions ) )
@ -747,12 +798,12 @@ func (hs *HTTPServer) GetDashboardVersion(c *contextmodel.ReqContext) response.R
}
}
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . OrgID , dashID , dashUID )
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . SignedInUser . Get OrgID( ) , dashID , dashUID )
if rsp != nil {
return rsp
}
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . OrgID , c . SignedInUser )
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . SignedInUser . Get OrgID( ) , c . SignedInUser )
if err != nil {
return response . Err ( err )
}
@ -763,7 +814,7 @@ func (hs *HTTPServer) GetDashboardVersion(c *contextmodel.ReqContext) response.R
version , _ := strconv . ParseInt ( web . Params ( c . Req ) [ ":id" ] , 10 , 32 )
query := dashver . GetDashboardVersionQuery {
OrgID : c . OrgID ,
OrgID : c . SignedInUser . Get OrgID( ) ,
DashboardID : dash . ID ,
DashboardUID : dash . UID ,
Version : int ( version ) ,
@ -771,7 +822,7 @@ func (hs *HTTPServer) GetDashboardVersion(c *contextmodel.ReqContext) response.R
res , err := hs . dashboardVersionService . Get ( c . Req . Context ( ) , & query )
if err != nil {
return response . Error ( 500 , fmt . Sprintf ( "Dashboard version %d not found for dashboardId %d" , query . Version , dash . ID ) , err )
return response . Error ( http . StatusInternalServerError , fmt . Sprintf ( "Dashboard version %d not found for dashboardId %d" , query . Version , dash . ID ) , err )
}
creator := anonString
@ -879,7 +930,7 @@ func (hs *HTTPServer) CalculateDashboardDiff(c *contextmodel.ReqContext) respons
if err := web . Bind ( c . Req , & apiOptions ) ; err != nil {
return response . Error ( http . StatusBadRequest , "bad request data" , err )
}
guardianBase , err := guardian . New ( c . Req . Context ( ) , apiOptions . Base . DashboardId , c . OrgID , c . SignedInUser )
guardianBase , err := guardian . New ( c . Req . Context ( ) , apiOptions . Base . DashboardId , c . SignedInUser . Get OrgID( ) , c . SignedInUser )
if err != nil {
return response . Err ( err )
}
@ -889,7 +940,7 @@ func (hs *HTTPServer) CalculateDashboardDiff(c *contextmodel.ReqContext) respons
}
if apiOptions . Base . DashboardId != apiOptions . New . DashboardId {
guardianNew , err := guardian . New ( c . Req . Context ( ) , apiOptions . New . DashboardId , c . OrgID , c . SignedInUser )
guardianNew , err := guardian . New ( c . Req . Context ( ) , apiOptions . New . DashboardId , c . SignedInUser . Get OrgID( ) , c . SignedInUser )
if err != nil {
return response . Err ( err )
}
@ -900,7 +951,7 @@ func (hs *HTTPServer) CalculateDashboardDiff(c *contextmodel.ReqContext) respons
}
options := dashdiffs . Options {
OrgId : c . OrgID ,
OrgId : c . SignedInUser . Get OrgID( ) ,
DiffType : dashdiffs . ParseDiffType ( apiOptions . DiffType ) ,
Base : dashdiffs . DiffTarget {
DashboardId : apiOptions . Base . DashboardId ,
@ -923,9 +974,9 @@ func (hs *HTTPServer) CalculateDashboardDiff(c *contextmodel.ReqContext) respons
baseVersionRes , err := hs . dashboardVersionService . Get ( c . Req . Context ( ) , & baseVersionQuery )
if err != nil {
if errors . Is ( err , dashver . ErrDashboardVersionNotFound ) {
return response . Error ( 404 , "Dashboard version not found" , err )
return response . Error ( http . StatusNotFound , "Dashboard version not found" , err )
}
return response . Error ( 500 , "Unable to compute diff" , err )
return response . Error ( http . StatusInternalServerError , "Unable to compute diff" , err )
}
newVersionQuery := dashver . GetDashboardVersionQuery {
@ -937,9 +988,9 @@ func (hs *HTTPServer) CalculateDashboardDiff(c *contextmodel.ReqContext) respons
newVersionRes , err := hs . dashboardVersionService . Get ( c . Req . Context ( ) , & newVersionQuery )
if err != nil {
if errors . Is ( err , dashver . ErrDashboardVersionNotFound ) {
return response . Error ( 404 , "Dashboard version not found" , err )
return response . Error ( http . StatusNotFound , "Dashboard version not found" , err )
}
return response . Error ( 500 , "Unable to compute diff" , err )
return response . Error ( http . StatusInternalServerError , "Unable to compute diff" , err )
}
baseData := baseVersionRes . Data
@ -949,9 +1000,9 @@ func (hs *HTTPServer) CalculateDashboardDiff(c *contextmodel.ReqContext) respons
if err != nil {
if errors . Is ( err , dashver . ErrDashboardVersionNotFound ) {
return response . Error ( 404 , "Dashboard version not found" , err )
return response . Error ( http . StatusNotFound , "Dashboard version not found" , err )
}
return response . Error ( 500 , "Unable to compute diff" , err )
return response . Error ( http . StatusInternalServerError , "Unable to compute diff" , err )
}
if options . DiffType == dashdiffs . DiffDelta {
@ -1003,12 +1054,12 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *contextmodel.ReqContext) respon
}
}
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . OrgID , dashID , dashUID )
dash , rsp := hs . getDashboardHelper ( c . Req . Context ( ) , c . SignedInUser . Get OrgID( ) , dashID , dashUID )
if rsp != nil {
return rsp
}
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . OrgID , c . SignedInUser )
guardian , err := guardian . NewByDashboard ( c . Req . Context ( ) , dash , c . SignedInUser . Get OrgID( ) , c . SignedInUser )
if err != nil {
return response . Err ( err )
}
@ -1017,16 +1068,25 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *contextmodel.ReqContext) respon
return dashboardGuardianResponse ( err )
}
versionQuery := dashver . GetDashboardVersionQuery { DashboardID : dashID , DashboardUID : dash . UID , Version : apiCmd . Version , OrgID : c . OrgID }
versionQuery := dashver . GetDashboardVersionQuery { DashboardID : dashID , DashboardUID : dash . UID , Version : apiCmd . Version , OrgID : c . SignedInUser . Get OrgID( ) }
version , err := hs . dashboardVersionService . Get ( c . Req . Context ( ) , & versionQuery )
if err != nil {
return response . Error ( 404 , "Dashboard version not found" , nil )
return response . Error ( http . StatusNotFound , "Dashboard version not found" , nil )
}
namespaceID , userIDstr := c . SignedInUser . GetNamespacedID ( )
if namespaceID != identity . NamespaceUser && namespaceID != identity . NamespaceServiceAccount {
return response . Error ( http . StatusBadRequest , "User does not belong to a user or service namespace" , nil )
}
userID , err := identity . IntIdentifier ( namespaceID , userIDstr )
if err != nil {
return response . Error ( http . StatusInternalServerError , "failed to get user id" , err )
}
saveCmd := dashboards . SaveDashboardCommand { }
saveCmd . RestoredFrom = version . Version
saveCmd . OrgID = c . OrgID
saveCmd . UserID = c . UserID
saveCmd . OrgID = c . SignedInUser . Get OrgID( )
saveCmd . UserID = u serID
saveCmd . Dashboard = version . Data
saveCmd . Dashboard . Set ( "version" , dash . Version )
saveCmd . Dashboard . Set ( "uid" , dash . UID )
@ -1045,7 +1105,7 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *contextmodel.ReqContext) respon
// 401: unauthorisedError
// 500: internalServerError
func ( hs * HTTPServer ) GetDashboardTags ( c * contextmodel . ReqContext ) {
query := dashboards . GetDashboardTagsQuery { OrgID : c . OrgID }
query := dashboards . GetDashboardTagsQuery { OrgID : c . SignedInUser . Get OrgID( ) }
queryResult , err := hs . DashboardService . GetDashboardTags ( c . Req . Context ( ) , & query )
if err != nil {
c . JsonApiErr ( 500 , "Failed to get tags from database" , err )