add: invite codes
This commit is contained in:
parent
d1a074eaeb
commit
626c6711ef
19 changed files with 410 additions and 10 deletions
|
@ -18,7 +18,7 @@ use axum::{
|
|||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use serde::Deserialize;
|
||||
use tetratto_core::model::addr::RemoteAddr;
|
||||
use tetratto_core::model::{addr::RemoteAddr, permissions::FinePermission};
|
||||
use tetratto_shared::hash::hash;
|
||||
|
||||
use cf_turnstile::{SiteVerifyRequest, TurnstileClient};
|
||||
|
@ -86,6 +86,50 @@ pub async fn register_request(
|
|||
let mut user = User::new(props.username.to_lowercase(), props.password);
|
||||
user.settings.policy_consent = true;
|
||||
|
||||
// check invite code
|
||||
if data.0.0.security.enable_invite_codes {
|
||||
if props.invite_code.is_empty() {
|
||||
return (
|
||||
None,
|
||||
Json(Error::MiscError("Missing invite code".to_string()).into()),
|
||||
);
|
||||
}
|
||||
|
||||
let invite_code = match data.get_invite_code_by_code(&props.invite_code).await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return (None, Json(e.into())),
|
||||
};
|
||||
|
||||
if invite_code.is_used {
|
||||
return (
|
||||
None,
|
||||
Json(Error::MiscError("This code has already been used".to_string()).into()),
|
||||
);
|
||||
}
|
||||
|
||||
let owner = match data.get_user_by_id(invite_code.owner).await {
|
||||
Ok(u) => u,
|
||||
Err(e) => return (None, Json(e.into())),
|
||||
};
|
||||
|
||||
if !owner.permissions.check(FinePermission::SUPPORTER) {
|
||||
return (
|
||||
None,
|
||||
Json(
|
||||
Error::MiscError("Invite code owner must be an active supporter".to_string())
|
||||
.into(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
user.invite_code = invite_code.id;
|
||||
|
||||
if let Err(e) = data.update_invite_code_is_used(invite_code.id, true).await {
|
||||
return (None, Json(e.into()));
|
||||
}
|
||||
}
|
||||
|
||||
// push initial token
|
||||
let (initial_token, t) = User::create_token(&real_ip);
|
||||
user.tokens.push(t);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue