2023-11-02 22:52:50 +05:30
package api
import (
2024-07-29 15:47:09 +05:30
"errors"
2024-05-24 12:11:34 +05:30
"net/http"
2024-07-29 15:47:09 +05:30
"strings"
2024-05-24 12:11:34 +05:30
2023-11-02 22:52:50 +05:30
"github.com/gorilla/mux"
"go.signoz.io/signoz/pkg/query-service/app/dashboards"
"go.signoz.io/signoz/pkg/query-service/auth"
"go.signoz.io/signoz/pkg/query-service/common"
"go.signoz.io/signoz/pkg/query-service/model"
)
func ( ah * APIHandler ) lockDashboard ( w http . ResponseWriter , r * http . Request ) {
ah . lockUnlockDashboard ( w , r , true )
}
func ( ah * APIHandler ) unlockDashboard ( w http . ResponseWriter , r * http . Request ) {
ah . lockUnlockDashboard ( w , r , false )
}
func ( ah * APIHandler ) lockUnlockDashboard ( w http . ResponseWriter , r * http . Request , lock bool ) {
// Locking can only be done by the owner of the dashboard
// or an admin
// - Fetch the dashboard
// - Check if the user is the owner or an admin
// - If yes, lock/unlock the dashboard
// - If no, return 403
// Get the dashboard UUID from the request
uuid := mux . Vars ( r ) [ "uuid" ]
2024-07-29 15:47:09 +05:30
if strings . HasPrefix ( uuid , "integration" ) {
RespondError ( w , & model . ApiError { Typ : model . ErrorForbidden , Err : errors . New ( "dashboards created by integrations cannot be unlocked" ) } , "You are not authorized to lock/unlock this dashboard" )
return
}
2023-11-02 22:52:50 +05:30
dashboard , err := dashboards . GetDashboard ( r . Context ( ) , uuid )
if err != nil {
RespondError ( w , & model . ApiError { Typ : model . ErrorInternal , Err : err } , err . Error ( ) )
return
}
user := common . GetUserFromContext ( r . Context ( ) )
2023-11-03 15:45:45 +05:30
if ! auth . IsAdmin ( user ) && ( dashboard . CreateBy != nil && * dashboard . CreateBy != user . Email ) {
2023-11-02 22:52:50 +05:30
RespondError ( w , & model . ApiError { Typ : model . ErrorForbidden , Err : err } , "You are not authorized to lock/unlock this dashboard" )
return
}
// Lock/Unlock the dashboard
err = dashboards . LockUnlockDashboard ( r . Context ( ) , uuid , lock )
if err != nil {
RespondError ( w , & model . ApiError { Typ : model . ErrorInternal , Err : err } , err . Error ( ) )
return
}
ah . Respond ( w , "Dashboard updated successfully" )
}