Skip to content

Commit 9cff858

Browse files
updated logic to get filter/dashboard based on user_id and filer_id/dashboard_id
1 parent 0df2645 commit 9cff858

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

server/src/handlers/http/users/dashboards.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ pub async fn list(req: HttpRequest) -> Result<impl Responder, DashboardError> {
3838
}
3939

4040
pub async fn get(req: HttpRequest) -> Result<impl Responder, DashboardError> {
41+
let user_id = get_user_from_request(&req)?;
4142
let dashboard_id = req
4243
.match_info()
4344
.get("dashboard_id")
4445
.ok_or(DashboardError::Metadata("No Dashboard Id Provided"))?;
4546

46-
if let Some(dashboard) = DASHBOARDS.get_dashboard(dashboard_id) {
47+
if let Some(dashboard) = DASHBOARDS.get_dashboard(dashboard_id, &get_hash(&user_id)) {
4748
return Ok((web::Json(dashboard), StatusCode::OK));
4849
}
4950

@@ -79,7 +80,10 @@ pub async fn update(req: HttpRequest, body: Bytes) -> Result<impl Responder, Das
7980
.match_info()
8081
.get("dashboard_id")
8182
.ok_or(DashboardError::Metadata("No Dashboard Id Provided"))?;
82-
if DASHBOARDS.get_dashboard(dashboard_id).is_none() {
83+
if DASHBOARDS
84+
.get_dashboard(dashboard_id, &get_hash(&user_id))
85+
.is_none()
86+
{
8387
return Err(DashboardError::Metadata("Dashboard does not exist"));
8488
}
8589
let mut dashboard: Dashboard = serde_json::from_slice(&body)?;
@@ -109,6 +113,12 @@ pub async fn delete(req: HttpRequest) -> Result<HttpResponse, DashboardError> {
109113
.match_info()
110114
.get("dashboard_id")
111115
.ok_or(DashboardError::Metadata("No Dashboard Id Provided"))?;
116+
if DASHBOARDS
117+
.get_dashboard(dashboard_id, &get_hash(&user_id))
118+
.is_none()
119+
{
120+
return Err(DashboardError::Metadata("Dashboard does not exist"));
121+
}
112122
let path = dashboard_path(&user_id, &format!("{}.json", dashboard_id));
113123
let store = CONFIG.storage().get_object_store();
114124
store.delete_object(&path).await?;

server/src/handlers/http/users/filters.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ pub async fn list(req: HttpRequest) -> Result<impl Responder, FiltersError> {
3636
}
3737

3838
pub async fn get(req: HttpRequest) -> Result<impl Responder, FiltersError> {
39+
let user_id = get_user_from_request(&req)?;
3940
let filter_id = req
4041
.match_info()
4142
.get("filter_id")
4243
.ok_or(FiltersError::Metadata("No Filter Id Provided"))?;
4344

44-
if let Some(filter) = FILTERS.get_filter(filter_id) {
45+
if let Some(filter) = FILTERS.get_filter(filter_id, &get_hash(&user_id)) {
4546
return Ok((web::Json(filter), StatusCode::OK));
4647
}
4748

@@ -76,7 +77,7 @@ pub async fn update(req: HttpRequest, body: Bytes) -> Result<HttpResponse, Filte
7677
.match_info()
7778
.get("filter_id")
7879
.ok_or(FiltersError::Metadata("No Filter Id Provided"))?;
79-
if FILTERS.get_filter(filter_id).is_none() {
80+
if FILTERS.get_filter(filter_id, &get_hash(&user_id)).is_none() {
8081
return Err(FiltersError::Metadata("Filter does not exist"));
8182
}
8283
let mut filter: Filter = serde_json::from_slice(&body)?;
@@ -104,7 +105,7 @@ pub async fn delete(req: HttpRequest) -> Result<HttpResponse, FiltersError> {
104105
.get("filter_id")
105106
.ok_or(FiltersError::Metadata("No Filter Id Provided"))?;
106107
let filter = FILTERS
107-
.get_filter(filter_id)
108+
.get_filter(filter_id, &get_hash(&user_id))
108109
.ok_or(FiltersError::Metadata("Filter does not exist"))?;
109110

110111
let path = filter_path(

server/src/users/dashboards.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,15 @@ impl Dashboards {
143143
s.retain(|d| d.dashboard_id != Some(dashboard_id.to_string()));
144144
}
145145

146-
pub fn get_dashboard(&self, dashboard_id: &str) -> Option<Dashboard> {
146+
pub fn get_dashboard(&self, dashboard_id: &str, user_id: &str) -> Option<Dashboard> {
147147
self.0
148148
.read()
149149
.expect(LOCK_EXPECT)
150150
.iter()
151-
.find(|d| d.dashboard_id == Some(dashboard_id.to_string()))
151+
.find(|d| {
152+
d.dashboard_id == Some(dashboard_id.to_string())
153+
&& d.user_id == Some(user_id.to_string())
154+
})
152155
.cloned()
153156
}
154157

@@ -157,7 +160,7 @@ impl Dashboards {
157160
.read()
158161
.expect(LOCK_EXPECT)
159162
.iter()
160-
.filter(|d| d.user_id.as_ref().unwrap() == user_id)
163+
.filter(|d| d.user_id == Some(user_id.to_string()))
161164
.cloned()
162165
.collect()
163166
}

server/src/users/filters.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,14 @@ impl Filters {
130130
s.retain(|f| f.filter_id != Some(filter_id.to_string()));
131131
}
132132

133-
pub fn get_filter(&self, filter_id: &str) -> Option<Filter> {
133+
pub fn get_filter(&self, filter_id: &str, user_id: &str) -> Option<Filter> {
134134
self.0
135135
.read()
136136
.expect(LOCK_EXPECT)
137137
.iter()
138-
.find(|f| f.filter_id == Some(filter_id.to_string()))
138+
.find(|f| {
139+
f.filter_id == Some(filter_id.to_string()) && f.user_id == Some(user_id.to_string())
140+
})
139141
.cloned()
140142
}
141143

@@ -144,7 +146,7 @@ impl Filters {
144146
.read()
145147
.expect(LOCK_EXPECT)
146148
.iter()
147-
.filter(|f| f.user_id.as_ref().unwrap() == user_id)
149+
.filter(|f| f.user_id == Some(user_id.to_string()))
148150
.cloned()
149151
.collect()
150152
}

0 commit comments

Comments
 (0)