mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-02-17 08:59:46 +03:00
feat: implement adding captcha for sqlx postgres
This commit is contained in:
parent
277d2bb9e5
commit
79ff7b9917
3 changed files with 55 additions and 3 deletions
|
@ -9,9 +9,10 @@ license = "AGPLv3 or later version"
|
|||
authors = ["realaravinth <realaravinth@batsense.net>"]
|
||||
|
||||
[dependencies]
|
||||
sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline" ] }
|
||||
db-core = {path = "../db-core"}
|
||||
async-trait = "0.1.51"
|
||||
db-core = {path = "../db-core"}
|
||||
futures = "0.3.15"
|
||||
sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline" ] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2"
|
||||
|
|
|
@ -295,6 +295,42 @@ impl MCDatabase for Database {
|
|||
.map_err(map_register_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add levels to captcha
|
||||
async fn add_captcha_levels(
|
||||
&self,
|
||||
username: &str,
|
||||
captcha_key: &str,
|
||||
levels: &[Level],
|
||||
) -> DBResult<()> {
|
||||
use futures::future::try_join_all;
|
||||
let mut futs = Vec::with_capacity(levels.len());
|
||||
|
||||
for level in levels.iter() {
|
||||
let difficulty_factor = level.difficulty_factor as i32;
|
||||
let visitor_threshold = level.visitor_threshold as i32;
|
||||
let fut = sqlx::query!(
|
||||
"INSERT INTO mcaptcha_levels (
|
||||
difficulty_factor,
|
||||
visitor_threshold,
|
||||
config_id) VALUES (
|
||||
$1, $2, (
|
||||
SELECT config_id FROM mcaptcha_config WHERE
|
||||
key = ($3) AND user_id = (
|
||||
SELECT ID FROM mcaptcha_users WHERE name = $4
|
||||
)));",
|
||||
difficulty_factor,
|
||||
visitor_threshold,
|
||||
&captcha_key,
|
||||
username,
|
||||
)
|
||||
.execute(&self.pool);
|
||||
futs.push(fut);
|
||||
}
|
||||
|
||||
try_join_all(futs).await.map_err(map_register_err)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn now_unix_time_stamp() -> i64 {
|
||||
|
|
|
@ -34,6 +34,21 @@ async fn everyting_works() {
|
|||
const CAPTCHA_DESCRIPTION: &str = "postgrescaptchadescription";
|
||||
const CAPTCHA_DURATION: i32 = 30;
|
||||
|
||||
const LEVELS: [Level; 3] = [
|
||||
Level {
|
||||
difficulty_factor: 1,
|
||||
visitor_threshold: 1,
|
||||
},
|
||||
Level {
|
||||
difficulty_factor: 2,
|
||||
visitor_threshold: 2,
|
||||
},
|
||||
Level {
|
||||
difficulty_factor: 3,
|
||||
visitor_threshold: 3,
|
||||
},
|
||||
];
|
||||
|
||||
let url = env::var("POSTGRES_DATABASE_URL").unwrap();
|
||||
let pool_options = PgPoolOptions::new().max_connections(2);
|
||||
let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url });
|
||||
|
@ -52,5 +67,5 @@ async fn everyting_works() {
|
|||
key: CAPTCHA_SECRET,
|
||||
description: CAPTCHA_DESCRIPTION,
|
||||
};
|
||||
database_works(&db, &p, &c).await;
|
||||
database_works(&db, &p, &c, &LEVELS).await;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue