From 347ffeb787299ee6232ddc76fe6d3dd531a16d7f Mon Sep 17 00:00:00 2001 From: Subhra264 Date: Thu, 20 Mar 2025 15:43:13 +0530 Subject: [PATCH] fix: when dashboard owner not present in creation payload, use currently logged in user (#6343) Currently in the main, when creating a dashboard, we check if the `owner` field is there in the backend. If it is not present, it returns Missing Owner error. This PR fixes this by checking first if the `owner` field is non-empty, otherwise it uses the currently logged in user as `owner`. --- src/config/src/meta/dashboards/mod.rs | 31 +++++++++++++++++++++ src/handler/http/request/dashboards/mod.rs | 32 ++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/config/src/meta/dashboards/mod.rs b/src/config/src/meta/dashboards/mod.rs index 59b0164047..9a3beddc81 100644 --- a/src/config/src/meta/dashboards/mod.rs +++ b/src/config/src/meta/dashboards/mod.rs @@ -101,6 +101,37 @@ impl Dashboard { } } + pub fn set_owner(&mut self, owner: String) { + match self { + Self { + version: 1, + v1: Some(inner), + .. + } => inner.owner = owner, + Self { + version: 2, + v2: Some(inner), + .. + } => inner.owner = owner, + Self { + version: 3, + v3: Some(inner), + .. + } => inner.owner = owner, + Self { + version: 4, + v4: Some(inner), + .. + } => inner.owner = owner, + Self { + version: 5, + v5: Some(inner), + .. + } => inner.owner = owner, + _ => {} + }; + } + pub fn title(&self) -> Option<&str> { match self.version { 1 => self.v1.as_ref().map(|inner| inner.title.as_str()), diff --git a/src/handler/http/request/dashboards/mod.rs b/src/handler/http/request/dashboards/mod.rs index 49c0bc43dd..6c0244935e 100644 --- a/src/handler/http/request/dashboards/mod.rs +++ b/src/handler/http/request/dashboards/mod.rs @@ -17,7 +17,7 @@ use actix_web::{HttpRequest, HttpResponse, Responder, delete, get, http, patch, use hashbrown::HashMap; use crate::{ - common::meta::http::HttpResponse as MetaHttpResponse, + common::{meta::http::HttpResponse as MetaHttpResponse, utils::auth::UserEmail}, handler::http::models::dashboards::{ CreateDashboardRequestBody, CreateDashboardResponseBody, GetDashboardResponseBody, ListDashboardsQuery, ListDashboardsResponseBody, MoveDashboardRequestBody, @@ -96,13 +96,18 @@ pub async fn create_dashboard( path: web::Path, req_body: web::Json, req: HttpRequest, + user_email: UserEmail, ) -> impl Responder { let org_id = path.into_inner(); let folder = get_folder(req); - let dashboard = match req_body.into_inner().try_into() { + let mut dashboard: config::meta::dashboards::Dashboard = match req_body.into_inner().try_into() + { Ok(dashboard) => dashboard, Err(_) => return MetaHttpResponse::bad_request("Error parsing request body"), }; + + set_dashboard_owner_if_empty(&mut dashboard, &user_email.user_id); + let saved = match dashboards::create_dashboard(&org_id, &folder, dashboard).await { Ok(saved) => saved, Err(err) => return err.into(), @@ -138,16 +143,21 @@ async fn update_dashboard( path: web::Path<(String, String)>, req_body: web::Json, req: HttpRequest, + user_email: UserEmail, ) -> impl Responder { let (org_id, dashboard_id) = path.into_inner(); let query = web::Query::>::from_query(req.query_string()).unwrap(); let folder = crate::common::utils::http::get_folder(&query); let hash = query.get("hash").map(|h| h.as_str()); - let dashboard = match req_body.into_inner().try_into() { + let mut dashboard: config::meta::dashboards::Dashboard = match req_body.into_inner().try_into() + { Ok(dashboard) => dashboard, Err(_) => return MetaHttpResponse::bad_request("Error parsing request body"), }; + + set_dashboard_owner_if_empty(&mut dashboard, &user_email.user_id); + let saved = match dashboards::update_dashboard(&org_id, &dashboard_id, &folder, dashboard, hash) .await { @@ -339,3 +349,19 @@ fn get_user_id(req: HttpRequest) -> Option { .and_then(|v| v.to_str().ok()) .map(|s| s.to_string()) } + +fn set_dashboard_owner_if_empty( + dashboard: &mut config::meta::dashboards::Dashboard, + user_email: &str, +) { + match dashboard.owner() { + Some(owner) => { + if owner.is_empty() { + dashboard.set_owner(user_email.to_string()); + } + } + None => { + dashboard.set_owner(user_email.to_string()); + } + } +}