|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
use crate::{request::purge_image_from_pictrs, sensitive::Sensitive, site::FederatedInstances};
|
|
|
|
|
use chrono::NaiveDateTime;
|
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
|
impls::person::is_banned,
|
|
|
|
|
newtypes::{CommunityId, LocalUserId, PersonId, PostId},
|
|
|
|
|
source::{
|
|
|
|
|
comment::Comment,
|
|
|
|
@ -129,15 +131,11 @@ pub async fn get_local_user_view_from_jwt(
|
|
|
|
|
let local_user_id = LocalUserId(claims.sub);
|
|
|
|
|
let local_user_view =
|
|
|
|
|
blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
|
|
|
|
|
// Check for a site ban
|
|
|
|
|
if local_user_view.person.is_banned() {
|
|
|
|
|
return Err(LemmyError::from_message("site_ban"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for user deletion
|
|
|
|
|
if local_user_view.person.deleted {
|
|
|
|
|
return Err(LemmyError::from_message("deleted"));
|
|
|
|
|
}
|
|
|
|
|
check_user_valid(
|
|
|
|
|
local_user_view.person.banned,
|
|
|
|
|
local_user_view.person.ban_expires,
|
|
|
|
|
local_user_view.person.deleted,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
|
|
|
|
|
|
|
|
|
@ -146,7 +144,7 @@ pub async fn get_local_user_view_from_jwt(
|
|
|
|
|
|
|
|
|
|
/// Checks if user's token was issued before user's password reset.
|
|
|
|
|
pub fn check_validator_time(
|
|
|
|
|
validator_time: &chrono::NaiveDateTime,
|
|
|
|
|
validator_time: &NaiveDateTime,
|
|
|
|
|
claims: &Claims,
|
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
|
let user_validation_time = validator_time.timestamp();
|
|
|
|
@ -169,30 +167,6 @@ pub async fn get_local_user_view_from_jwt_opt(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
|
pub async fn get_local_user_settings_view_from_jwt(
|
|
|
|
|
jwt: &Sensitive<String>,
|
|
|
|
|
pool: &DbPool,
|
|
|
|
|
secret: &Secret,
|
|
|
|
|
) -> Result<LocalUserSettingsView, LemmyError> {
|
|
|
|
|
let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret)
|
|
|
|
|
.map_err(|e| e.with_message("not_logged_in"))?
|
|
|
|
|
.claims;
|
|
|
|
|
let local_user_id = LocalUserId(claims.sub);
|
|
|
|
|
let local_user_view = blocking(pool, move |conn| {
|
|
|
|
|
LocalUserSettingsView::read(conn, local_user_id)
|
|
|
|
|
})
|
|
|
|
|
.await??;
|
|
|
|
|
// Check for a site ban
|
|
|
|
|
if local_user_view.person.is_banned() {
|
|
|
|
|
return Err(LemmyError::from_message("site_ban"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
|
|
|
|
|
|
|
|
|
|
Ok(local_user_view)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
|
pub async fn get_local_user_settings_view_from_jwt_opt(
|
|
|
|
|
jwt: Option<&Sensitive<String>>,
|
|
|
|
@ -200,12 +174,45 @@ pub async fn get_local_user_settings_view_from_jwt_opt(
|
|
|
|
|
secret: &Secret,
|
|
|
|
|
) -> Result<Option<LocalUserSettingsView>, LemmyError> {
|
|
|
|
|
match jwt {
|
|
|
|
|
Some(jwt) => Ok(Some(
|
|
|
|
|
get_local_user_settings_view_from_jwt(jwt, pool, secret).await?,
|
|
|
|
|
)),
|
|
|
|
|
Some(jwt) => {
|
|
|
|
|
let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret)
|
|
|
|
|
.map_err(|e| e.with_message("not_logged_in"))?
|
|
|
|
|
.claims;
|
|
|
|
|
let local_user_id = LocalUserId(claims.sub);
|
|
|
|
|
let local_user_view = blocking(pool, move |conn| {
|
|
|
|
|
LocalUserSettingsView::read(conn, local_user_id)
|
|
|
|
|
})
|
|
|
|
|
.await??;
|
|
|
|
|
check_user_valid(
|
|
|
|
|
local_user_view.person.banned,
|
|
|
|
|
local_user_view.person.ban_expires,
|
|
|
|
|
local_user_view.person.deleted,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
|
|
|
|
|
|
|
|
|
|
Ok(Some(local_user_view))
|
|
|
|
|
}
|
|
|
|
|
None => Ok(None),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn check_user_valid(
|
|
|
|
|
banned: bool,
|
|
|
|
|
ban_expires: Option<NaiveDateTime>,
|
|
|
|
|
deleted: bool,
|
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
|
// Check for a site ban
|
|
|
|
|
if is_banned(banned, ban_expires) {
|
|
|
|
|
return Err(LemmyError::from_message("site_ban"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for account deletion
|
|
|
|
|
if deleted {
|
|
|
|
|
return Err(LemmyError::from_message("deleted"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
|
pub async fn check_community_ban(
|
|
|
|
|