Skip to content

Commit

Permalink
Add support to:
Browse files Browse the repository at this point in the history
- menu_item
- role
- menu_tree
  • Loading branch information
yamelsenih committed Jun 18, 2024
1 parent 31853f9 commit 817bbc9
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ RUST_LOG=info
PORT=7878
ALLOWED_ORIGIN="*"
KAFKA_ENABLED="Y"
KAFKA_QUEUES="menu browser form process window menu_item menu_tree"
KAFKA_QUEUES="menu browser form process window menu_item menu_tree role"
KAFKA_HOST="localhost:29092"
KAFKA_GROUP="default"
OPENSEARCH_URL="http://localhost:9200"
Expand Down
19 changes: 18 additions & 1 deletion src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use dictionary_rs::{controller::{kafka::create_consumer, opensearch::{create, delete, IndexDocument}}, models::{browser::{browser_from_id, browsers, BrowserDocument}, form::{form_from_id, forms, FormDocument}, menu::{menu_from_id, menus, MenuDocument}, menu_item::MenuItemDocument, menu_tree::MenuTreeDocument, process::{process_from_id, processes, ProcessDocument}, window::{window_from_id, windows, WindowDocument}}};
use dictionary_rs::{controller::{kafka::create_consumer, opensearch::{create, delete, IndexDocument}}, models::{browser::{browser_from_id, browsers, BrowserDocument}, form::{form_from_id, forms, FormDocument}, menu::{menu_from_id, menus, MenuDocument}, menu_item::MenuItemDocument, menu_tree::MenuTreeDocument, process::{process_from_id, processes, ProcessDocument}, role::RoleDocument, window::{window_from_id, windows, WindowDocument}}};
use dotenv::dotenv;
use rdkafka::{Message, consumer::{CommitMode, Consumer}};
use salvo::{conn::tcp::TcpAcceptor, cors::Cors, http::header, hyper::Method, prelude::*};
Expand Down Expand Up @@ -505,6 +505,23 @@ async fn consume_queue() {
Err(error) => log::warn!("{}", error)
}
}
} else if topic == "role" {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Err(error) => {
log::warn!("{}", error);
RoleDocument {
document: None
}
},
};
if _document.document.is_some() {
let _menu_document: &dyn IndexDocument = &(_document.document.unwrap());
match process_index(event_type, _menu_document).await {
Ok(_) => consumer.commit_message(&message, CommitMode::Async).unwrap(),
Err(error) => log::warn!("{}", error)
}
}
} else if topic == "process" {
let _document = match serde_json::from_str(payload) {
Ok(value) => value,
Expand Down
42 changes: 42 additions & 0 deletions src/controller/opensearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,48 @@ pub async fn find(_document: &dyn IndexDocument, _search_value: String, _from: i
Ok(list)
}

pub async fn find_from_dsl_body(_document: &dyn IndexDocument, _body: serde_json::Value, _from: i64, _size: i64) -> Result<Vec<Value>, std::string::String> {
let client: OpenSearch = match create_opensearch_client() {
Ok(client_value) => client_value,
Err(error) => {
log::error!("{:?}", error);
return Err(error.to_string());
}
};
// Create
let _response: Result<opensearch::http::response::Response, opensearch::Error> = client
.search(SearchParts::Index(&[&_document.index_name()]))
.from(_from)
.size(_size)
.body(_body)
.send()
.await
;
let response = match _response {
Ok(value) => value,
Err(error) => {
log::error!("{:?}", error);
return Err(error.to_string());
}
};
if !response.status_code().is_success() {
return Err(format!("Error finding record {:?}", response.text().await));
}
let response_body = match response.json::<Value>().await {
Ok(response) => response,
Err(error) => {
log::error!("{:?}", error);
return Err(error.to_string());
},
};
let mut list: Vec::<Value> = Vec::new();
for hit in response_body["hits"]["hits"].as_array().unwrap() {
let value = hit["_source"].to_owned();
list.push(value)
}
Ok(list)
}

pub async fn get_by_id(_document: &dyn IndexDocument) -> Result<Value, std::string::String> {
let client = match create_opensearch_client() {
Ok(client_value) => client_value,
Expand Down
119 changes: 119 additions & 0 deletions src/models/menu_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::{io::ErrorKind, io::Error};

use crate::{controller::opensearch::{IndexDocument, get_by_id, find, exists_index}, models::{user_index, role_index}};

use super::role::Role;

#[derive(Deserialize, Extractible, Debug, Clone)]
#[salvo(extract(default_source(from = "body")))]
pub struct MenuItemDocument {
Expand Down Expand Up @@ -98,6 +100,123 @@ impl MenuItem {
menu.id = _id;
menu
}

fn get_find_body_from_role(self: &Self, _role: Role) -> serde_json::Value {
// "W" Window
// "X" Form
// "S" Smart Browser
// "R" Report
// "P" Process
// "F" Workflow
let _window_access = match _role.to_owned().window_access {
Some(value) => value,
None => Vec::new()
};
let _form_access = match _role.to_owned().form_access {
Some(value) => value,
None => Vec::new()
};
let _browser_access = match _role.to_owned().browser_access {
Some(value) => value,
None => Vec::new()
};
let _process_access = match _role.to_owned().process_access {
Some(value) => value,
None => Vec::new()
};
let _workflow_access = match _role.to_owned().workflow_access {
Some(value) => value,
None => Vec::new()
};
json!({
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"terms": {
"action_id": _window_access
}
},
{
"match": {
"action": "W"
}
}
]
}
},
{
"bool": {
"must": [
{
"terms": {
"action_id": _form_access
}
},
{
"match": {
"action": "X"
}
}
]
}
},
{
"bool": {
"must": [
{
"terms": {
"action_id": _browser_access
}
},
{
"match": {
"action": "S"
}
}
]
}
},
{
"bool": {
"must": [
{
"terms": {
"action_id": _process_access
}
},
{
"terms": {
"action": ["R", "P"]
}
}
]
}
},
{
"bool": {
"must": [
{
"terms": {
"action_id": _workflow_access
}
},
{
"match": {
"action": "F"
}
}
]
}
}
]
}
}
})
}
}

impl IndexDocument for MenuItem {
Expand Down
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod window;
pub mod generic;
pub mod menu_item;
pub mod menu_tree;
pub mod role;

use serde::{Deserialize, Serialize};
use salvo::prelude::*;
Expand Down
Loading

0 comments on commit 817bbc9

Please sign in to comment.