Skip to content

Commit

Permalink
Merge pull request #57 from EdwinBetanc0urt/bugfix/find-role-with-uuid
Browse files Browse the repository at this point in the history
fix: Save role includes access with uuid null.
  • Loading branch information
yamelsenih authored Oct 1, 2024
2 parents b7eb0b3 + 3c47901 commit 2fceede
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 70 deletions.
14 changes: 7 additions & 7 deletions src/models/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand Down Expand Up @@ -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<MenuListResponse, std::io::Error> {
let _expected_role = role_from_id(_role_id, _client_id, _dictionary_code).await;
let _role = match _expected_role {
let _expected_role: Result<Role, String> = 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<Vec<MenuItem>, Error> = menu_items_from_role(_role.to_owned(), _language, _dictionary_code, None, None).await;
let _menu_items: Vec<MenuItem> = match _menu_items {
Ok(menu) => menu,
Err(error) => return Err(Error::new(ErrorKind::InvalidData.into(), error))
};
Expand All @@ -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<MenuTree, Error> = 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))
};
Expand Down
118 changes: 69 additions & 49 deletions src/models/menu_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,59 @@ impl Default for MenuItem {
}

impl MenuItem {
pub fn from_id(_id: Option<String>) -> Self {
let mut menu = MenuItem::default();
pub fn from_id(_id: Option<String>) -> Self {
let mut menu: MenuItem = MenuItem::default();
menu.id = _id;
menu
}

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<String> = match _role.to_owned().window_access {
Some(value) => value,
None => Vec::new()
};
Some(value) => {
// remove none values into vector
value.into_iter().flatten().collect()
},
None => Vec::new()
};

// "X" Form
let _form_access: Vec<String> = match _role.to_owned().form_access {
Some(value) => value,
None => Vec::new()
};
Some(value) => {
// remove none values into vector
value.into_iter().flatten().collect()
},
None => Vec::new()
};

// "S" Smart Browser
let _browser_access: Vec<String> = match _role.to_owned().browser_access {
Some(value) => value,
None => Vec::new()
};
Some(value) => {
// remove none values into vector
value.into_iter().flatten().collect()
},
None => Vec::new()
};

// "R" Report
// "P" Process
let _process_access: Vec<String> = match _role.to_owned().process_access {
Some(value) => value,
None => Vec::new()
};
Some(value) => {
// remove none values into vector
value.into_iter().flatten().collect()
},
None => Vec::new()
};

// "F" Workflow
let _workflow_access: Vec<String> = match _role.to_owned().workflow_access {
Some(value) => value,
None => Vec::new()
};
Some(value) => {
// remove none values into vector
value.into_iter().flatten().collect()
},
None => Vec::new()
};

json!({
"query": {
"bool": {
Expand All @@ -135,7 +155,7 @@ impl MenuItem {
"must": [
{
"match": {
"is_summary": true
"is_summary": true
}
}
]
Expand All @@ -145,13 +165,13 @@ impl MenuItem {
"bool": {
"must": [
{
"terms": {
"action_uuid": _window_access
"match": {
"action": "W"
}
},
{
"match": {
"action": "W"
"terms": {
"action_uuid": _window_access
}
}
]
Expand All @@ -161,13 +181,13 @@ impl MenuItem {
"bool": {
"must": [
{
"terms": {
"action_uuid": _form_access
"match": {
"action": "X"
}
},
{
"match": {
"action": "X"
"terms": {
"action_uuid": _form_access
}
}
]
Expand All @@ -177,13 +197,13 @@ impl MenuItem {
"bool": {
"must": [
{
"terms": {
"action_uuid": _browser_access
"match": {
"action": "S"
}
},
{
"match": {
"action": "S"
"terms": {
"action_uuid": _browser_access
}
}
]
Expand All @@ -193,13 +213,13 @@ impl MenuItem {
"bool": {
"must": [
{
"terms": {
"action_uuid": _process_access
"match": {
"action": "P"
}
},
{
"match": {
"action": "P"
"terms": {
"action_uuid": _process_access
}
}
]
Expand All @@ -209,13 +229,13 @@ impl MenuItem {
"bool": {
"must": [
{
"terms": {
"action_uuid": _process_access
"match": {
"action": "R"
}
},
{
"match": {
"action": "R"
"terms": {
"action_uuid": _process_access
}
}
]
Expand All @@ -225,13 +245,13 @@ impl MenuItem {
"bool": {
"must": [
{
"terms": {
"action_uuid": _workflow_access
"match": {
"action": "F"
}
},
{
"match": {
"action": "F"
"terms": {
"action_uuid": _workflow_access
}
}
]
Expand All @@ -241,7 +261,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)
Expand Down
28 changes: 14 additions & 14 deletions src/models/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ pub struct Role {
pub role_id: Option<String>,
pub user_id: Option<String>,
// Access
pub window_access: Option<Vec<String>>,
pub process_access: Option<Vec<String>>,
pub form_access: Option<Vec<String>>,
pub browser_access: Option<Vec<String>>,
pub workflow_access: Option<Vec<String>>,
pub dashboard_access: Option<Vec<String>>
pub window_access: Option<Vec<Option<String>>>,
pub process_access: Option<Vec<Option<String>>>,
pub form_access: Option<Vec<Option<String>>>,
pub browser_access: Option<Vec<Option<String>>>,
pub workflow_access: Option<Vec<Option<String>>>,
pub dashboard_access: Option<Vec<Option<String>>>
}

impl Default for Role {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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()
})
Expand Down Expand Up @@ -166,7 +166,7 @@ pub async fn role_from_id(_uuid: Option<&String>, _client_uuid: Option<&String>,
Ok(value) => {
match serde_json::from_value::<Role>(value) {
Ok(role) => {
log::info!("Finded Role Value: {:?}", role.id);
log::info!("Finded Role {:?} Value: {:?}", role.name, role.id);
Ok(role)
},
Err(error) => {
Expand Down

0 comments on commit 2fceede

Please sign in to comment.