From d42a9c6bb8431d054dd02cc5bf36ff09e8cc73d7 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 7 May 2021 19:44:44 +0530 Subject: [PATCH] view sitekey --- src/pages/panel/sitekey/mod.rs | 8 + src/pages/panel/sitekey/view.rs | 139 ++++++++++++++++++ templates/panel/sitekey/view/css/main.scss | 55 +++++++ .../panel/sitekey/view/existing-level.html | 29 ++++ templates/panel/sitekey/view/index.html | 51 +++++++ templates/panel/sitekey/view/ts/index.ts | 18 +++ 6 files changed, 300 insertions(+) create mode 100644 src/pages/panel/sitekey/view.rs create mode 100644 templates/panel/sitekey/view/css/main.scss create mode 100644 templates/panel/sitekey/view/existing-level.html create mode 100644 templates/panel/sitekey/view/index.html create mode 100644 templates/panel/sitekey/view/ts/index.ts diff --git a/src/pages/panel/sitekey/mod.rs b/src/pages/panel/sitekey/mod.rs index 9412c3dc..6945defb 100644 --- a/src/pages/panel/sitekey/mod.rs +++ b/src/pages/panel/sitekey/mod.rs @@ -17,6 +17,7 @@ mod add; mod list; +mod view; pub mod routes { pub struct Sitekey { @@ -53,4 +54,11 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) { Methods::ProtectGet, list::list_sitekeys ); + + define_resource!( + cfg, + PAGES.panel.sitekey.view, + Methods::ProtectGet, + view::view_sitekey + ); } diff --git a/src/pages/panel/sitekey/view.rs b/src/pages/panel/sitekey/view.rs new file mode 100644 index 00000000..f61d527d --- /dev/null +++ b/src/pages/panel/sitekey/view.rs @@ -0,0 +1,139 @@ +/* +* Copyright (C) 2021 Aravinth Manivannan +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see . +*/ + +use actix_identity::Identity; +use actix_web::{web, HttpResponse, Responder}; +use sailfish::TemplateOnce; + +use crate::errors::*; +use crate::Data; + +const PAGE: &str = "SiteKeys"; + +#[derive(Clone)] +struct McaptchaConfig { + config_id: i32, + duration: i32, + name: String, +} + +#[derive(Clone)] +struct Level { + difficulty_factor: i32, + visitor_threshold: i32, +} +#[derive(TemplateOnce, Clone)] +#[template(path = "panel/sitekey/view/index.html")] +struct IndexPage { + duration: u32, + name: String, + levels: Vec, +} + +impl IndexPage { + fn new(config: McaptchaConfig, levels: Vec) -> Self { + IndexPage { + duration: config.duration as u32, + name: config.name, + levels, + } + } +} + +pub async fn view_sitekey( + path: web::Path, + data: web::Data, + id: Identity, +) -> PageResult { + let username = id.identity().unwrap(); + let key = path.0; + + let config = sqlx::query_as!( + McaptchaConfig, + "SELECT config_id, duration, name from mcaptcha_config WHERE + key = $1 AND + user_id = (SELECT ID FROM mcaptcha_users WHERE name = $2) ", + &key, + &username, + ) + .fetch_one(&data.db) + .await?; + + let levels = sqlx::query_as!( + Level, + "SELECT difficulty_factor, visitor_threshold from mcaptcha_levels WHERE config_id = $1", + &config.config_id + ) + .fetch_all(&data.db) + .await?; + + let body = IndexPage::new(config, levels).render_once().unwrap(); + Ok(HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(body)) +} + +#[cfg(test)] +mod test { + use actix_web::http::StatusCode; + use actix_web::test; + use actix_web::web::Bytes; + + use crate::tests::*; + use crate::*; + + #[actix_rt::test] + async fn view_sitekey_work() { + const NAME: &str = "viewsitekeyuser"; + const PASSWORD: &str = "longpassworddomain"; + const EMAIL: &str = "viewsitekeyuser@a.com"; + + { + let data = Data::new().await; + delete_user(NAME, &data).await; + } + + register_and_signin(NAME, EMAIL, PASSWORD).await; + let (data, _, signin_resp, key) = add_levels_util(NAME, PASSWORD).await; + let cookies = get_cookie!(signin_resp); + + let mut app = get_app!(data).await; + + let url = format!("/sitekey/{}/view", &key.key); + + let list_sitekey_resp = test::call_service( + &mut app, + test::TestRequest::get() + .uri(&url) + .cookie(cookies.clone()) + .to_request(), + ) + .await; + + assert_eq!(list_sitekey_resp.status(), StatusCode::OK); + + let body: Bytes = test::read_body(list_sitekey_resp).await; + let body = String::from_utf8(body.to_vec()).unwrap(); + + assert!(body.contains(&key.name)); + + assert!(body.contains(&L1.visitor_threshold.to_string())); + assert!(body.contains(&L1.difficulty_factor.to_string())); + assert!(body.contains(&L2.difficulty_factor.to_string())); + assert!(body.contains(&L2.visitor_threshold.to_string())); + } +} diff --git a/templates/panel/sitekey/view/css/main.scss b/templates/panel/sitekey/view/css/main.scss new file mode 100644 index 00000000..0ef0636e --- /dev/null +++ b/templates/panel/sitekey/view/css/main.scss @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +@import '../../../../reset'; +@import '../../../../vars'; +@import '../../../../components/box'; + +.sitekey-list__box { + @include box; + padding-bottom: 0px; +} + +.sitekey-list__title { + @include box-title; +} + +.sitekey-list__item-container { + display: block; + width: 100%; + box-sizing: border-box; + border-bottom: 0.1px solid $light-grey; + padding: 20px; + color: $black-text; +} + +.sitekey-list__item { + display: flex; + width: 100%; +} + +.sitekey-list__item-container:hover { + background-color: $light-grey; +} + +.sitekey-list__name { + flex: 3; +} + +.sitekey-list__key { + flex: 1; +} diff --git a/templates/panel/sitekey/view/existing-level.html b/templates/panel/sitekey/view/existing-level.html new file mode 100644 index 00000000..3bef6caa --- /dev/null +++ b/templates/panel/sitekey/view/existing-level.html @@ -0,0 +1,29 @@ +<. let num = count + 1; .> +
+ + Level <.= num .> + + + + +
diff --git a/templates/panel/sitekey/view/index.html b/templates/panel/sitekey/view/index.html new file mode 100644 index 00000000..0774dbae --- /dev/null +++ b/templates/panel/sitekey/view/index.html @@ -0,0 +1,51 @@ +<. include!("../../../components/headers.html"); .> <. +include!("../../header/index.html"); .> + +
+ <. include!("../../taskbar/index.html"); .> <. + include!("../../help-banner/index.html"); .> + +
+ + +
+

+ Sitekey: <.= name .> +

+ + + + + + <. for (count, level) in levels.iter().enumerate() { .> + <. include!("./existing-level.html"); .> + <. } .> + +
+
+ +
+<. include!("../../../components/footers.html"); .> diff --git a/templates/panel/sitekey/view/ts/index.ts b/templates/panel/sitekey/view/ts/index.ts new file mode 100644 index 00000000..661a4391 --- /dev/null +++ b/templates/panel/sitekey/view/ts/index.ts @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +export const index = () => {};