add site key form

This commit is contained in:
realaravinth
2021-05-03 20:24:03 +05:30
parent 0531a26274
commit 812b0ff2c9
49 changed files with 1253 additions and 597 deletions

View File

@@ -20,6 +20,7 @@ use actix_web::{web, HttpResponse, Responder};
use m_captcha::{defense::Level, DefenseBuilder};
use serde::{Deserialize, Serialize};
use super::mcaptcha::add_mcaptcha_util;
use crate::api::v1::mcaptcha::mcaptcha::MCaptchaDetails;
use crate::errors::*;
use crate::Data;
@@ -52,8 +53,6 @@ pub mod routes {
#[derive(Serialize, Deserialize)]
pub struct AddLevels {
pub levels: Vec<Level>,
/// name is config_name
pub key: String,
}
pub fn services(cfg: &mut web::ServiceConfig) {
@@ -104,6 +103,8 @@ async fn add_levels(
defense.build()?;
let mcaptcha_config = add_mcaptcha_util(&data, &id).await?;
for level in payload.levels.iter() {
let difficulty_factor = level.difficulty_factor as i32;
let visitor_threshold = level.visitor_threshold as i32;
@@ -119,18 +120,25 @@ async fn add_levels(
)));",
difficulty_factor,
visitor_threshold,
&payload.key,
&mcaptcha_config.key,
&username,
)
.execute(&data.db)
.await?;
}
Ok(HttpResponse::Ok())
Ok(HttpResponse::Ok().json(mcaptcha_config))
}
#[derive(Serialize, Deserialize)]
pub struct UpdateLevels {
pub levels: Vec<Level>,
/// name is config_name
pub key: String,
}
async fn update_levels(
payload: web::Json<AddLevels>,
payload: web::Json<UpdateLevels>,
data: web::Data<Data>,
id: Identity,
) -> ServiceResult<impl Responder> {
@@ -187,7 +195,7 @@ async fn update_levels(
}
async fn delete_levels(
payload: web::Json<AddLevels>,
payload: web::Json<UpdateLevels>,
data: web::Data<Data>,
id: Identity,
) -> ServiceResult<impl Responder> {
@@ -304,7 +312,7 @@ mod tests {
visitor_threshold: 5000,
};
let levels = vec![l1, l2];
let add_level = AddLevels {
let add_level = UpdateLevels {
levels: levels.clone(),
key: key.key.clone(),
};
@@ -337,7 +345,7 @@ mod tests {
visitor_threshold: 5000,
};
let levels = vec![l1, l2];
let add_level = AddLevels {
let add_level = UpdateLevels {
levels: levels.clone(),
key: key.key.clone(),
};

View File

@@ -88,7 +88,7 @@ pub struct MCaptchaDetails {
}
// this should be called from within add levels
async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
pub async fn add_mcaptcha_util(data: &Data, id: &Identity) -> ServiceResult<MCaptchaDetails> {
let username = id.identity().unwrap();
let mut key;
@@ -125,7 +125,12 @@ async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl
}
}
}
Ok(resp)
}
// this should be called from within add levels
async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
let resp = add_mcaptcha_util(&data, &id).await?;
Ok(HttpResponse::Ok().json(resp))
}