Skip to content

Commit

Permalink
can add shared address book rule in web consolle
Browse files Browse the repository at this point in the history
  • Loading branch information
eltorio committed May 31, 2024
1 parent 55aaa82 commit e1a1f00
Show file tree
Hide file tree
Showing 17 changed files with 676 additions and 51 deletions.
74 changes: 74 additions & 0 deletions libs/state/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,4 +1394,78 @@ impl Database {
}
Some(ab_rules)
}

pub async fn delete_ab_rule(&self, rule: &str) -> Option<()> {
let mut conn = self.pool.acquire().await.unwrap();
let rule_guid = Uuid::parse_str(rule);
if rule_guid.is_err() {
log::error!("delete_ab_rule error: {:?}", rule_guid);
return None;
}
let rule_guid = rule_guid.unwrap().as_bytes().to_vec();
let res = sqlx::query!(
r#"
DELETE FROM ab_rule WHERE guid = ?
"#,
rule_guid
)
.execute(&mut conn)
.await;
if res.is_err() {
log::error!("delete_ab_rule error: {:?}", res);
return None;
}
Some(())
}

pub async fn add_ab_rule(&self, rule: AbRule) -> Option<()> {
let mut conn = self.pool.acquire().await.unwrap();
let rule_guid = Uuid::new_v4().as_bytes().to_vec();
let ab_guid = Uuid::parse_str(&rule.guid);
if ab_guid.is_err() {
log::error!("add_ab_rule error: {:?}", ab_guid);
return None;
}
let ab_guid = ab_guid.unwrap().as_bytes().to_vec();
let user_guid = if (rule.user.is_some()) {

Check warning on line 1430 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unnecessary parentheses around `if` condition
let uuid = Uuid::parse_str(&rule.user.unwrap());
if (uuid.is_err()){

Check warning on line 1432 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unnecessary parentheses around `if` condition
None
}else{
Some(uuid.unwrap().as_bytes().to_vec())
}
} else {
None
};

let group_guid = if (rule.group.is_some()) {

Check warning on line 1441 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unnecessary parentheses around `if` condition
let uuid = Uuid::parse_str(&rule.group.unwrap());
if (uuid.is_err()){

Check warning on line 1443 in libs/state/src/database.rs

View workflow job for this annotation

GitHub Actions / build (x86_64-unknown-linux-gnu, ubuntu-latest)

unnecessary parentheses around `if` condition
None
}else{
Some(uuid.unwrap().as_bytes().to_vec())
}
} else {
None
};

let res = sqlx::query!(
r#"
INSERT OR IGNORE INTO ab_rule (guid, ab, user, grp, rule)
VALUES (?, ?, ?, ?, ?)
"#,
rule_guid,
ab_guid,
user_guid,
group_guid,
rule.rule
)
.execute(&mut conn)
.await;
if res.is_err() {
log::error!("add_ab_rule error: {:?}", res);
return None;
}
Some(())
}
}
8 changes: 8 additions & 0 deletions libs/state/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,4 +668,12 @@ impl ApiState {
pub async fn get_ab_rules(&self, offset:u32, page_size: u32, ab: &str) -> Option<Vec<AbRule>> {
self.db.get_ab_rules(offset,page_size, ab).await
}

pub async fn delete_ab_rule(&self, rule: &str) -> Option<()> {
self.db.delete_ab_rule(rule).await
}

pub async fn add_ab_rule(&self, rule: AbRule) -> Option<()> {
self.db.add_ab_rule(rule).await
}
}
13 changes: 13 additions & 0 deletions libs/utils/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,17 @@ pub struct AbRulesResponse {
pub msg: String,
pub total: u32,
pub data: Vec<AbRule>,
}

#[derive(Serialize, Deserialize, Clone, JsonSchema)]
pub struct AbRuleAddRequest {
pub guid: String, // address book guid
pub user: Option<String>, // user=None and group=None means all users
pub group: Option<String>,
pub rule: u32,
}

#[derive(Serialize, Deserialize, Clone, JsonSchema)]
pub struct AbRuleDeleteRequest {
pub guid: String, // rule guid
}
70 changes: 70 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ use state::{self};
use ui;
use utils::guid_into_uuid;
use utils::AbProfile;
use utils::AbRule;
use utils::AbRuleAddRequest;
use utils::AbRuleDeleteRequest;
use utils::AbRulesResponse;
use utils::AbSharedAddRequest;
use utils::{
Expand Down Expand Up @@ -146,6 +149,8 @@ pub async fn build_rocket(figment: Figment) -> Rocket<Build> {
ab_shared_add,
ab_settings,
ab_rules,
ab_rule_add,
ab_rule_delete,
software,
software_version,
webconsole_index,
Expand Down Expand Up @@ -1739,6 +1744,71 @@ async fn ab_rules(
Ok(Json(response))
}

/// # Add a Rule
///
/// This function is an API endpoint that adds a new rule to a shared address book.
/// It is tagged with "address book" for OpenAPI documentation.
///
/// ## Parameters
///
/// - `request`: The request containing the details of the rule to be added.
///
/// ## Returns
///
/// If successful, this function returns an `ActionResponse::Empty` indicating that the rule was successfully added. <br>
/// If the system is in maintenance mode, this function returns a `status::Unauthorized` error.
///
/// ## Errors
///
/// This function will return an error if the system is in maintenance mode.
#[openapi(tag = "address book")]
#[post("/api/ab/rule", format = "application/json", data = "<request>")]
async fn ab_rule_add(
state: &State<ApiState>,
_user: AuthenticatedAdmin,
request: Json<AbRuleAddRequest>,
) -> Result<ActionResponse, status::Unauthorized<()>> {
state.check_maintenance().await;
let rule = AbRule {
guid: request.0.guid,
user: request.0.user,
group: request.0.group,
rule: request.0.rule,
};
state.add_ab_rule(rule).await;
Ok(ActionResponse::Empty)
}

/// # Delete a Rule
///
/// This function is an API endpoint that deletes a rule from a shared address book.
/// It is tagged with "address book" for OpenAPI documentation.
///
/// ## Parameters
///
/// - `request`: The request containing the GUID of the rule to be deleted.
///
/// ## Returns
///
/// If successful, this function returns an `ActionResponse::Empty` indicating that the rule was successfully deleted. <br>
/// If the system is in maintenance mode, this function returns a `status::Unauthorized` error.
///
/// ## Errors
///
/// This function will return an error if the system is in maintenance mode.
#[openapi(tag = "address book")]
#[delete("/api/ab/rule", format = "application/json", data = "<request>")]
async fn ab_rule_delete(
state: &State<ApiState>,
_user: AuthenticatedAdmin,
request: Json<AbRuleDeleteRequest>,
) -> Result<ActionResponse, status::Unauthorized<()>> {
state.check_maintenance().await;
let rule = request.0.guid;
state.delete_ab_rule(rule.as_str()).await;
Ok(ActionResponse::Empty)
}

async fn webconsole_index_multi() -> Redirect {
Redirect::to(uri!("/ui/"))
}
Expand Down
46 changes: 23 additions & 23 deletions webconsole/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@
"version": "1.1.99-29",
"type": "module",
"scripts": {
"preview": "vite preview",
"build": "vite build && gulp licenses",
"devserver": "npx nodemon -V -w ./src -e js,vue,ts,css,html --exec 'npm run build && node devserver.js'",
"create-cert": "openssl req -x509 -newkey rsa:4096 -keyout localhost.key -out localhost.pem -sha256 -nodes -days 365",
"dev": "vite"
"build": "vite build && gulp licenses",
"dev": "vite",
"preview": "vite preview"
},
"dependencies": {
"@pqina/flip": "^1.8.3",
"jdenticon": "^3.3.0",
"vue-router": "^4.3.2",
"@heroicons/vue": "^2.1.3",
"vue": "^3.4.27",
"pinia": "^2.1.7",
"@headlessui/vue": "^1.7.22",
"axios": "^1.7.2"
"vue": "^3.4.27",
"jdenticon": "^3.3.0",
"axios": "^1.7.2",
"@pqina/flip": "^1.8.3",
"@heroicons/vue": "^2.1.3",
"@headlessui/vue": "^1.7.22"
},
"devDependencies": {
"nodemon": "^3.1.0",
"glob": "10.4.1",
"autoprefixer": "^10.4.19",
"gulp-append-prepend": "^1.0.9",
"postcss": "^8.4.38",
"express": "^4.19.2",
"sass": "^1.77.2",
"@tailwindcss/typography": "^0.5.13",
"ts-node": "^10.9.2",
"@tailwindcss/forms": "^0.5.7",
"vite-plugin-static-copy": "^1.0.5",
"typescript": "^5.4.5",
"@fullhuman/postcss-purgecss": "^6.0.0",
"tailwindcss": "^3.4.3",
"postcss-purgefonts": "^1.0.2",
"@fullhuman/postcss-purgecss": "^6.0.0",
"@vitejs/plugin-vue": "^5.0.4",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"vite-plugin-static-copy": "^1.0.5",
"express": "^4.19.2",
"npm-check-updates": "^16.14.20",
"autoprefixer": "^10.4.19",
"@tailwindcss/forms": "^0.5.7",
"@types/glob": "^8.1.0",
"gulp-if": "^3.0.0",
"@vitejs/plugin-vue": "^5.0.4",
"glob": "10.4.1",
"sass": "^1.77.2",
"@tailwindcss/typography": "^0.5.13",
"postcss": "^8.4.38",
"vite": "^5.2.11",
"gulp": "^5.0.0",
"npm-check-updates": "^16.14.20"
"ts-node": "^10.9.2"
}
}
Loading

0 comments on commit e1a1f00

Please sign in to comment.