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`.
This commit is contained in:
Subhra264
2025-03-20 15:43:13 +05:30
committed by GitHub
parent ab0b3a64f8
commit 347ffeb787
2 changed files with 60 additions and 3 deletions
+31
View File
@@ -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()),
+29 -3
View File
@@ -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<String>,
req_body: web::Json<CreateDashboardRequestBody>,
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<UpdateDashboardRequestBody>,
req: HttpRequest,
user_email: UserEmail,
) -> impl Responder {
let (org_id, dashboard_id) = path.into_inner();
let query = web::Query::<HashMap<String, String>>::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<String> {
.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());
}
}
}