From 3242c4643c7021bc2dce9ac4af1cf3cc58fc1994 Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Mon, 30 Sep 2024 22:35:00 -0400 Subject: [PATCH 1/4] fix: Save role includes access with uuid null. --- Cargo.toml | 2 +- src/bin/server.rs | 14 +++--- src/models/menu.rs | 14 +++--- src/models/menu_item.rs | 109 +++++++++++++++++++++++----------------- src/models/role.rs | 28 +++++------ 5 files changed, 91 insertions(+), 76 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c41c15f..3c07d84 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ log = "0.4.21" simple_logger = "5.0.0" futures = "0.3.30" rdkafka = "0.36.2" -opensearch = "2.2.0" +opensearch = "2.3.0" [[bin]] name = "server" diff --git a/src/bin/server.rs b/src/bin/server.rs index 7a57691..ecce8d8 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -437,7 +437,7 @@ async fn consume_queue() { let event_type = key.replace("\"", ""); let topic = message.topic(); if topic == "menu_item" { - let _document = match serde_json::from_str(payload) { + let _document: MenuItemDocument = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -454,7 +454,7 @@ async fn consume_queue() { } } } else if topic == "menu_tree" { - let _document = match serde_json::from_str(payload) { + let _document: MenuTreeDocument = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -471,7 +471,7 @@ async fn consume_queue() { } } } else if topic == "role" { - let _document = match serde_json::from_str(payload) { + let _document: RoleDocument = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -488,7 +488,7 @@ async fn consume_queue() { } } } else if topic == "process" { - let _document = match serde_json::from_str(payload) { + let _document: ProcessDocument = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -505,7 +505,7 @@ async fn consume_queue() { } } } else if topic == "browser" { - let _document = match serde_json::from_str(payload) { + let _document: BrowserDocument = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -522,7 +522,7 @@ async fn consume_queue() { } } } else if topic == "window" { - let _document = match serde_json::from_str(payload) { + let _document: WindowDocument = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -539,7 +539,7 @@ async fn consume_queue() { } } } else if topic == "form" { - let _document = match serde_json::from_str(payload) { + let _document: FormDocument = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); diff --git a/src/models/menu.rs b/src/models/menu.rs index bbe06d9..2e9041c 100644 --- a/src/models/menu.rs +++ b/src/models/menu.rs @@ -4,7 +4,7 @@ use std::{io::ErrorKind, io::Error}; use crate::models::{menu_item::menu_items_from_role, menu_tree::menu_tree_from_id, role::role_from_id}; -use super::{menu_item::MenuItem, menu_tree::MenuTree}; +use super::{menu_item::MenuItem, menu_tree::MenuTree, role::Role}; #[derive(Deserialize, Extractible, Debug, Clone)] #[salvo(extract(default_source(from = "body")))] @@ -128,14 +128,14 @@ impl Menu { } pub async fn allowed_menu(_language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _dictionary_code: Option<&String>) -> Result { - let _expected_role = role_from_id(_role_id, _client_id, _dictionary_code).await; - let _role = match _expected_role { + let _expected_role: Result = role_from_id(_role_id, _client_id, _dictionary_code).await; + let _role: Role = match _expected_role { Ok(role) => role, Err(error) => return Err(Error::new(ErrorKind::InvalidData.into(), error)) }; - let _menu_items = menu_items_from_role(_role.to_owned(), _language, _dictionary_code, None, None).await; - let _menu_items = match _menu_items { + let _menu_items: Result, Error> = menu_items_from_role(_role.to_owned(), _language, _dictionary_code, None, None).await; + let _menu_items: Vec = match _menu_items { Ok(menu) => menu, Err(error) => return Err(Error::new(ErrorKind::InvalidData.into(), error)) }; @@ -144,8 +144,8 @@ pub async fn allowed_menu(_language: Option<&String>, _client_id: Option<&String return Err(Error::new(ErrorKind::InvalidData.into(), "Tree ID not found")) } - let _tree_result = menu_tree_from_id(_role.tree_uuid, _dictionary_code).await; - let _tree = match _tree_result { + let _tree_result: Result = menu_tree_from_id(_role.tree_uuid, _dictionary_code).await; + let _tree: MenuTree = match _tree_result { Ok(tree) => tree, Err(error) => return Err(Error::new(ErrorKind::InvalidData.into(), error)) }; diff --git a/src/models/menu_item.rs b/src/models/menu_item.rs index d27d499..29d61fc 100644 --- a/src/models/menu_item.rs +++ b/src/models/menu_item.rs @@ -100,32 +100,47 @@ impl MenuItem { } fn get_find_body_from_role(_role: Role) -> serde_json::Value { - // "W" Window - // "X" Form - // "S" Smart Browser - // "R" Report - // "P" Process - // "F" Workflow + // "W" Window let _window_access: Vec = match _role.to_owned().window_access { - Some(value) => value, - None => Vec::new() - }; + Some(value) => { + value.into_iter().filter_map(|x| x).collect() + }, + None => Vec::new() + }; + + // "X" Form let _form_access: Vec = match _role.to_owned().form_access { - Some(value) => value, - None => Vec::new() - }; + Some(value) => { + value.into_iter().filter_map(|x| x).collect() + }, + None => Vec::new() + }; + + // "S" Smart Browser let _browser_access: Vec = match _role.to_owned().browser_access { - Some(value) => value, - None => Vec::new() - }; + Some(value) => { + value.into_iter().filter_map(|x| x).collect() + }, + None => Vec::new() + }; + + // "R" Report + // "P" Process let _process_access: Vec = match _role.to_owned().process_access { - Some(value) => value, - None => Vec::new() - }; + Some(value) => { + value.into_iter().filter_map(|x| x).collect() + }, + None => Vec::new() + }; + + // "F" Workflow let _workflow_access: Vec = match _role.to_owned().workflow_access { - Some(value) => value, - None => Vec::new() - }; + Some(value) => { + value.into_iter().filter_map(|x| x).collect() + }, + None => Vec::new() + }; + json!({ "query": { "bool": { @@ -135,7 +150,7 @@ impl MenuItem { "must": [ { "match": { - "is_summary": true + "is_summary": true } } ] @@ -145,13 +160,13 @@ impl MenuItem { "bool": { "must": [ { - "terms": { - "action_uuid": _window_access + "match": { + "action": "W" } }, { - "match": { - "action": "W" + "terms": { + "action_uuid": _window_access } } ] @@ -161,13 +176,13 @@ impl MenuItem { "bool": { "must": [ { - "terms": { - "action_uuid": _form_access + "match": { + "action": "X" } }, { - "match": { - "action": "X" + "terms": { + "action_uuid": _form_access } } ] @@ -177,13 +192,13 @@ impl MenuItem { "bool": { "must": [ { - "terms": { - "action_uuid": _browser_access + "match": { + "action": "S" } }, { - "match": { - "action": "S" + "terms": { + "action_uuid": _browser_access } } ] @@ -193,13 +208,13 @@ impl MenuItem { "bool": { "must": [ { - "terms": { - "action_uuid": _process_access + "match": { + "action": "P" } }, { - "match": { - "action": "P" + "terms": { + "action_uuid": _process_access } } ] @@ -209,13 +224,13 @@ impl MenuItem { "bool": { "must": [ { - "terms": { - "action_uuid": _process_access + "match": { + "action": "R" } }, { - "match": { - "action": "R" + "terms": { + "action_uuid": _process_access } } ] @@ -225,13 +240,13 @@ impl MenuItem { "bool": { "must": [ { - "terms": { - "action_uuid": _workflow_access + "match": { + "action": "F" } }, { - "match": { - "action": "F" + "terms": { + "action_uuid": _workflow_access } } ] @@ -241,7 +256,7 @@ impl MenuItem { } } }) - } + } pub fn to_string(&self) -> String { format!("Menu Item: UUID {:?}, ID {:?}, Name {:?}, Index: {:?}", self.uuid, self.internal_id, self.name, self.index_value) diff --git a/src/models/role.rs b/src/models/role.rs index ae010d3..57e2203 100644 --- a/src/models/role.rs +++ b/src/models/role.rs @@ -47,12 +47,12 @@ pub struct Role { pub role_id: Option, pub user_id: Option, // Access - pub window_access: Option>, - pub process_access: Option>, - pub form_access: Option>, - pub browser_access: Option>, - pub workflow_access: Option>, - pub dashboard_access: Option> + pub window_access: Option>>, + pub process_access: Option>>, + pub form_access: Option>>, + pub browser_access: Option>>, + pub workflow_access: Option>>, + pub dashboard_access: Option>> } impl Default for Role { @@ -83,14 +83,14 @@ impl Default for Role { } impl Role { - pub fn from_id(_id: Option<&String>) -> Self { - let mut menu = Role::default(); - menu.uuid = _id.cloned(); - menu - } + pub fn from_id(_id: Option<&String>) -> Self { + let mut role: Role = Role::default(); + role.uuid = _id.cloned(); + role + } pub fn to_string(&self) -> String { - format!("Form: UUID {:?}, ID {:?}, Name {:?}, Index: {:?}", self.uuid, self.internal_id, self.name, self.index_value) + format!("Role: UUID {:?}, ID {:?}, Name {:?}, Index: {:?}", self.uuid, self.internal_id, self.name, self.index_value) } } @@ -115,7 +115,7 @@ impl IndexDocument for Role { } fn id(self: &Self) -> String { - self.id.to_owned().unwrap_or_else(|| { + self.uuid.to_owned().unwrap_or_else(|| { log::error!("{}", self.to_string()); "".to_string() }) @@ -166,7 +166,7 @@ pub async fn role_from_id(_uuid: Option<&String>, _client_uuid: Option<&String>, Ok(value) => { match serde_json::from_value::(value) { Ok(role) => { - log::info!("Finded Role Value: {:?}", role.id); + log::info!("Finded Role {:?} Value: {:?}", role.name, role.id); Ok(role) }, Err(error) => { From 9245b34ca8f96344b20d9c2cab6b54603fa5c654 Mon Sep 17 00:00:00 2001 From: Edwin Betancourt Date: Tue, 1 Oct 2024 06:44:43 -0400 Subject: [PATCH 2/4] revert changes --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3c07d84..c41c15f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ log = "0.4.21" simple_logger = "5.0.0" futures = "0.3.30" rdkafka = "0.36.2" -opensearch = "2.3.0" +opensearch = "2.2.0" [[bin]] name = "server" From 42a95620215d6fe533496e05a2413b37fa148d86 Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Tue, 1 Oct 2024 07:02:15 -0400 Subject: [PATCH 3/4] change to flatten. --- src/models/menu_item.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/models/menu_item.rs b/src/models/menu_item.rs index 29d61fc..2e83960 100644 --- a/src/models/menu_item.rs +++ b/src/models/menu_item.rs @@ -93,8 +93,8 @@ impl Default for MenuItem { } impl MenuItem { - pub fn from_id(_id: Option) -> Self { - let mut menu = MenuItem::default(); + pub fn from_id(_id: Option) -> Self { + let mut menu: MenuItem = MenuItem::default(); menu.id = _id; menu } @@ -103,7 +103,8 @@ impl MenuItem { // "W" Window let _window_access: Vec = match _role.to_owned().window_access { Some(value) => { - value.into_iter().filter_map(|x| x).collect() + // remove none values into vector + value.into_iter().flatten().collect() }, None => Vec::new() }; @@ -111,7 +112,8 @@ impl MenuItem { // "X" Form let _form_access: Vec = match _role.to_owned().form_access { Some(value) => { - value.into_iter().filter_map(|x| x).collect() + // remove none values into vector + value.into_iter().flatten().collect() }, None => Vec::new() }; @@ -119,7 +121,8 @@ impl MenuItem { // "S" Smart Browser let _browser_access: Vec = match _role.to_owned().browser_access { Some(value) => { - value.into_iter().filter_map(|x| x).collect() + // remove none values into vector + value.into_iter().flatten().collect() }, None => Vec::new() }; @@ -128,7 +131,8 @@ impl MenuItem { // "P" Process let _process_access: Vec = match _role.to_owned().process_access { Some(value) => { - value.into_iter().filter_map(|x| x).collect() + // remove none values into vector + value.into_iter().flatten().collect() }, None => Vec::new() }; @@ -136,7 +140,8 @@ impl MenuItem { // "F" Workflow let _workflow_access: Vec = match _role.to_owned().workflow_access { Some(value) => { - value.into_iter().filter_map(|x| x).collect() + // remove none values into vector + value.into_iter().flatten().collect() }, None => Vec::new() }; From 3c479018ff34cc032ab2185046a0eb487fe52219 Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Tue, 1 Oct 2024 10:38:14 -0400 Subject: [PATCH 4/4] revert changes. --- src/bin/server.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bin/server.rs b/src/bin/server.rs index ecce8d8..7a57691 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -437,7 +437,7 @@ async fn consume_queue() { let event_type = key.replace("\"", ""); let topic = message.topic(); if topic == "menu_item" { - let _document: MenuItemDocument = match serde_json::from_str(payload) { + let _document = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -454,7 +454,7 @@ async fn consume_queue() { } } } else if topic == "menu_tree" { - let _document: MenuTreeDocument = match serde_json::from_str(payload) { + let _document = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -471,7 +471,7 @@ async fn consume_queue() { } } } else if topic == "role" { - let _document: RoleDocument = match serde_json::from_str(payload) { + let _document = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -488,7 +488,7 @@ async fn consume_queue() { } } } else if topic == "process" { - let _document: ProcessDocument = match serde_json::from_str(payload) { + let _document = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -505,7 +505,7 @@ async fn consume_queue() { } } } else if topic == "browser" { - let _document: BrowserDocument = match serde_json::from_str(payload) { + let _document = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -522,7 +522,7 @@ async fn consume_queue() { } } } else if topic == "window" { - let _document: WindowDocument = match serde_json::from_str(payload) { + let _document = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error); @@ -539,7 +539,7 @@ async fn consume_queue() { } } } else if topic == "form" { - let _document: FormDocument = match serde_json::from_str(payload) { + let _document = match serde_json::from_str(payload) { Ok(value) => value, Err(error) => { log::warn!("Topic: {:?}, {}", topic, error);