Skip to content

Commit 155e18e

Browse files
acquire write lock for save dashboard
1 parent df47a10 commit 155e18e

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub struct Options {
451451
help = "Object store sync threshold in seconds"
452452
)]
453453
pub object_store_sync_threshold: u64,
454-
// the oidc scope
454+
// the oidc scope
455455
#[arg(
456456
long = "oidc-scope",
457457
name = "oidc-scope",

src/handlers/http/oidc.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ pub async fn login(
7777
let session_key = extract_session_key_from_req(&req).ok();
7878
let (session_key, oidc_client) = match (session_key, oidc_client) {
7979
(None, None) => return Ok(redirect_no_oauth_setup(query.redirect.clone())),
80-
(None, Some(client)) => return Ok(redirect_to_oidc(query, client, PARSEABLE.options.scope.to_string().as_str())),
80+
(None, Some(client)) => {
81+
return Ok(redirect_to_oidc(
82+
query,
83+
client,
84+
PARSEABLE.options.scope.to_string().as_str(),
85+
))
86+
}
8187
(Some(session_key), client) => (session_key, client),
8288
};
8389
// try authorize
@@ -113,7 +119,11 @@ pub async fn login(
113119
} else {
114120
Users.remove_session(&key);
115121
if let Some(oidc_client) = oidc_client {
116-
redirect_to_oidc(query, oidc_client, PARSEABLE.options.scope.to_string().as_str())
122+
redirect_to_oidc(
123+
query,
124+
oidc_client,
125+
PARSEABLE.options.scope.to_string().as_str(),
126+
)
117127
} else {
118128
redirect_to_client(query.redirect.as_str(), None)
119129
}

src/users/dashboards.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,7 @@ impl Dashboards {
206206
.dashboard_id
207207
.ok_or(DashboardError::Metadata("Dashboard ID must be provided"))?;
208208

209-
// ensure the dashboard has unique title
210-
let dashboards = self.0.read().await;
211-
let has_duplicate = dashboards
212-
.iter()
213-
.any(|d| d.title == dashboard.title && d.dashboard_id != dashboard.dashboard_id);
214-
215-
if has_duplicate {
216-
return Err(DashboardError::Metadata("Dashboard title must be unique"));
217-
}
218209
let path = dashboard_path(user_id, &format!("{dashboard_id}.json"));
219-
220210
let store = PARSEABLE.storage.get_object_store();
221211
let dashboard_bytes = serde_json::to_vec(&dashboard)?;
222212
store
@@ -237,8 +227,19 @@ impl Dashboards {
237227
dashboard.created = Some(Utc::now());
238228
dashboard.set_metadata(user_id, None);
239229

230+
let mut dashboards = self.0.write().await;
231+
232+
let has_duplicate = dashboards
233+
.iter()
234+
.any(|d| d.title == dashboard.title && d.dashboard_id != dashboard.dashboard_id);
235+
236+
if has_duplicate {
237+
return Err(DashboardError::Metadata("Dashboard title must be unique"));
238+
}
239+
240240
self.save_dashboard(user_id, dashboard).await?;
241-
self.0.write().await.push(dashboard.clone());
241+
242+
dashboards.push(dashboard.clone());
242243

243244
Ok(())
244245
}
@@ -252,16 +253,32 @@ impl Dashboards {
252253
dashboard_id: Ulid,
253254
dashboard: &mut Dashboard,
254255
) -> Result<(), DashboardError> {
255-
let existing_dashboard = self
256-
.ensure_dashboard_ownership(dashboard_id, user_id)
257-
.await?;
256+
let mut dashboards = self.0.write().await;
257+
258+
let existing_dashboard = dashboards
259+
.iter()
260+
.find(|d| d.dashboard_id == Some(dashboard_id) && d.author == Some(user_id.to_string()))
261+
.cloned()
262+
.ok_or_else(|| {
263+
DashboardError::Metadata(
264+
"Dashboard does not exist or you do not have permission to access it",
265+
)
266+
})?;
258267

259268
dashboard.set_metadata(user_id, Some(dashboard_id));
260269
dashboard.created = existing_dashboard.created;
270+
271+
let has_duplicate = dashboards
272+
.iter()
273+
.any(|d| d.title == dashboard.title && d.dashboard_id != dashboard.dashboard_id);
274+
275+
if has_duplicate {
276+
return Err(DashboardError::Metadata("Dashboard title must be unique"));
277+
}
278+
261279
self.save_dashboard(user_id, dashboard).await?;
262280

263-
let mut dashboards = self.0.write().await;
264-
dashboards.retain(|d| d.dashboard_id != dashboard.dashboard_id);
281+
dashboards.retain(|d| d.dashboard_id != Some(dashboard_id));
265282
dashboards.push(dashboard.clone());
266283

267284
Ok(())

0 commit comments

Comments
 (0)