You've already forked AstralRinth
forked from didirus/AstralRinth
Filtering refactoring (#806)
* switching computers * fmt clippy sqlx prepare * merge fixes
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use crate::auth::checks::{is_visible_project, is_visible_version};
|
||||
use crate::database::models::legacy_loader_fields::MinecraftGameVersion;
|
||||
use crate::database::models::loader_fields::Loader;
|
||||
use crate::database::models::project_item::QueryProject;
|
||||
@@ -7,10 +8,7 @@ use crate::models::pats::Scopes;
|
||||
use crate::models::projects::{ProjectId, VersionId};
|
||||
use crate::queue::session::AuthQueue;
|
||||
use crate::routes::ApiError;
|
||||
use crate::{
|
||||
auth::{get_user_from_headers, is_authorized, is_authorized_version},
|
||||
database,
|
||||
};
|
||||
use crate::{auth::get_user_from_headers, database};
|
||||
use actix_web::{get, route, web, HttpRequest, HttpResponse};
|
||||
use sqlx::PgPool;
|
||||
use std::collections::HashSet;
|
||||
@@ -94,7 +92,7 @@ pub async fn maven_metadata(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
if !is_authorized(&project.inner, &user_option, &pool).await? {
|
||||
if !is_visible_project(&project.inner, &user_option, &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -288,7 +286,7 @@ pub async fn version_file(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
if !is_authorized(&project.inner, &user_option, &pool).await? {
|
||||
if !is_visible_project(&project.inner, &user_option, &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -296,7 +294,7 @@ pub async fn version_file(
|
||||
return Err(ApiError::NotFound);
|
||||
};
|
||||
|
||||
if !is_authorized_version(&version.inner, &user_option, &pool).await? {
|
||||
if !is_visible_version(&version.inner, &user_option, &pool, &redis).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -349,7 +347,7 @@ pub async fn version_file_sha1(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
if !is_authorized(&project.inner, &user_option, &pool).await? {
|
||||
if !is_visible_project(&project.inner, &user_option, &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -357,7 +355,7 @@ pub async fn version_file_sha1(
|
||||
return Err(ApiError::NotFound);
|
||||
};
|
||||
|
||||
if !is_authorized_version(&version.inner, &user_option, &pool).await? {
|
||||
if !is_visible_version(&version.inner, &user_option, &pool, &redis).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -391,7 +389,7 @@ pub async fn version_file_sha512(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
if !is_authorized(&project.inner, &user_option, &pool).await? {
|
||||
if !is_visible_project(&project.inner, &user_option, &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -399,7 +397,7 @@ pub async fn version_file_sha512(
|
||||
return Err(ApiError::NotFound);
|
||||
};
|
||||
|
||||
if !is_authorized_version(&version.inner, &user_option, &pool).await? {
|
||||
if !is_visible_version(&version.inner, &user_option, &pool, &redis).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@ use actix_web::{get, web, HttpRequest, HttpResponse};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::auth::{filter_authorized_versions, get_user_from_headers, is_authorized};
|
||||
use crate::auth::checks::{filter_visible_versions, is_visible_project};
|
||||
use crate::auth::get_user_from_headers;
|
||||
use crate::database;
|
||||
use crate::database::models::legacy_loader_fields::MinecraftGameVersion;
|
||||
use crate::database::redis::RedisPool;
|
||||
@@ -56,7 +57,7 @@ pub async fn forge_updates(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
if !is_authorized(&project.inner, &user_option, &pool).await? {
|
||||
if !is_visible_project(&project.inner, &user_option, &pool).await? {
|
||||
return Err(ApiError::InvalidInput(ERROR.to_string()));
|
||||
}
|
||||
|
||||
@@ -68,14 +69,14 @@ pub async fn forge_updates(
|
||||
_ => |x: &String| *x == "forge",
|
||||
};
|
||||
|
||||
let mut versions = filter_authorized_versions(
|
||||
let mut versions = filter_visible_versions(
|
||||
versions
|
||||
.into_iter()
|
||||
.filter(|x| x.loaders.iter().any(loaders))
|
||||
.collect(),
|
||||
&user_option,
|
||||
&pool,
|
||||
redis,
|
||||
&redis,
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::auth::checks::{filter_authorized_collections, is_authorized_collection};
|
||||
use crate::auth::get_user_from_headers;
|
||||
use crate::auth::checks::is_visible_collection;
|
||||
use crate::auth::{filter_visible_collections, get_user_from_headers};
|
||||
use crate::database::models::{collection_item, generate_collection_id, project_item};
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::file_hosting::FileHost;
|
||||
@@ -155,7 +155,7 @@ pub async fn collections_get(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
let collections = filter_authorized_collections(collections_data, &user_option, &pool).await?;
|
||||
let collections = filter_visible_collections(collections_data, &user_option).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(collections))
|
||||
}
|
||||
@@ -183,7 +183,7 @@ pub async fn collection_get(
|
||||
.ok();
|
||||
|
||||
if let Some(data) = collection_data {
|
||||
if is_authorized_collection(&data, &user_option).await? {
|
||||
if is_visible_collection(&data, &user_option).await? {
|
||||
return Ok(HttpResponse::Ok().json(Collection::from(data)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::auth::{get_user_from_headers, is_authorized, is_authorized_version};
|
||||
use crate::auth::checks::{is_team_member_project, is_team_member_version};
|
||||
use crate::auth::get_user_from_headers;
|
||||
use crate::database;
|
||||
use crate::database::models::{project_item, report_item, thread_item, version_item};
|
||||
use crate::database::redis::RedisPool;
|
||||
@@ -62,7 +63,9 @@ pub async fn images_add(
|
||||
if let Some(id) = data.project_id {
|
||||
let project = project_item::Project::get(&id, &**pool, &redis).await?;
|
||||
if let Some(project) = project {
|
||||
if is_authorized(&project.inner, &Some(user.clone()), &pool).await? {
|
||||
if is_team_member_project(&project.inner, &Some(user.clone()), &pool)
|
||||
.await?
|
||||
{
|
||||
*project_id = Some(project.inner.id.into());
|
||||
} else {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
@@ -81,7 +84,13 @@ pub async fn images_add(
|
||||
if let Some(id) = data.version_id {
|
||||
let version = version_item::Version::get(id.into(), &**pool, &redis).await?;
|
||||
if let Some(version) = version {
|
||||
if is_authorized_version(&version.inner, &Some(user.clone()), &pool).await?
|
||||
if is_team_member_version(
|
||||
&version.inner,
|
||||
&Some(user.clone()),
|
||||
&pool,
|
||||
&redis,
|
||||
)
|
||||
.await?
|
||||
{
|
||||
*version_id = Some(version.inner.id.into());
|
||||
} else {
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::ApiError;
|
||||
use crate::auth::{filter_authorized_projects, get_user_from_headers};
|
||||
use crate::auth::{filter_visible_projects, get_user_from_headers};
|
||||
use crate::database::models::team_item::TeamMember;
|
||||
use crate::database::models::{generate_organization_id, team_item, Organization};
|
||||
use crate::database::redis::RedisPool;
|
||||
@@ -85,7 +85,7 @@ pub async fn organization_projects_get(
|
||||
let projects_data =
|
||||
crate::database::models::Project::get_many_ids(&project_ids, &**pool, &redis).await?;
|
||||
|
||||
let projects = filter_authorized_projects(projects_data, ¤t_user, &pool).await?;
|
||||
let projects = filter_visible_projects(projects_data, ¤t_user, &pool).await?;
|
||||
Ok(HttpResponse::Ok().json(projects))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::auth::{filter_authorized_projects, get_user_from_headers, is_authorized};
|
||||
use crate::auth::checks::is_visible_project;
|
||||
use crate::auth::{filter_visible_projects, get_user_from_headers};
|
||||
use crate::database::models::notification_item::NotificationBuilder;
|
||||
use crate::database::models::project_item::{GalleryItem, ModCategory};
|
||||
use crate::database::models::thread_item::ThreadMessageBuilder;
|
||||
@@ -136,7 +137,7 @@ pub async fn projects_get(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
let projects = filter_authorized_projects(projects_data, &user_option, &pool).await?;
|
||||
let projects = filter_visible_projects(projects_data, &user_option, &pool).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(projects))
|
||||
}
|
||||
@@ -163,7 +164,7 @@ pub async fn project_get(
|
||||
.ok();
|
||||
|
||||
if let Some(data) = project_data {
|
||||
if is_authorized(&data.inner, &user_option, &pool).await? {
|
||||
if is_visible_project(&data.inner, &user_option, &pool).await? {
|
||||
return Ok(HttpResponse::Ok().json(Project::from(data)));
|
||||
}
|
||||
}
|
||||
@@ -968,7 +969,7 @@ pub async fn dependency_list(
|
||||
.ok();
|
||||
|
||||
if let Some(project) = result {
|
||||
if !is_authorized(&project.inner, &user_option, &pool).await? {
|
||||
if !is_visible_project(&project.inner, &user_option, &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -2061,7 +2062,7 @@ pub async fn project_follow(
|
||||
let user_id: db_ids::UserId = user.id.into();
|
||||
let project_id: db_ids::ProjectId = result.inner.id;
|
||||
|
||||
if !is_authorized(&result.inner, &Some(user), &pool).await? {
|
||||
if !is_visible_project(&result.inner, &Some(user), &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -2204,7 +2205,7 @@ pub async fn project_get_organization(
|
||||
ApiError::InvalidInput("The specified project does not exist!".to_string())
|
||||
})?;
|
||||
|
||||
if is_authorized(&result.inner, &Some(user), &pool).await? {
|
||||
if is_visible_project(&result.inner, &Some(user), &pool).await? {
|
||||
Err(ApiError::InvalidInput(
|
||||
"The specified project does not exist!".to_string(),
|
||||
))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::auth::{get_user_from_headers, is_authorized};
|
||||
use crate::auth::checks::is_visible_project;
|
||||
use crate::auth::get_user_from_headers;
|
||||
use crate::database::models::notification_item::NotificationBuilder;
|
||||
use crate::database::models::team_item::TeamAssociationId;
|
||||
use crate::database::models::{Organization, Team, TeamMember, User};
|
||||
@@ -59,7 +60,7 @@ pub async fn team_members_get_project(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
if !is_authorized(&project.inner, ¤t_user, &pool).await? {
|
||||
if !is_visible_project(&project.inner, ¤t_user, &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
let members_data =
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use super::ApiError;
|
||||
use crate::auth::{
|
||||
filter_authorized_projects, filter_authorized_versions, get_user_from_headers,
|
||||
is_authorized_version,
|
||||
};
|
||||
use crate::auth::checks::{filter_visible_versions, is_visible_version};
|
||||
use crate::auth::{filter_visible_projects, get_user_from_headers};
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::ids::VersionId;
|
||||
use crate::models::pats::Scopes;
|
||||
@@ -67,7 +65,7 @@ pub async fn get_version_from_hash(
|
||||
if let Some(file) = file {
|
||||
let version = database::models::Version::get(file.version_id, &**pool, &redis).await?;
|
||||
if let Some(version) = version {
|
||||
if !is_authorized_version(&version.inner, &user_option, &pool).await? {
|
||||
if !is_visible_version(&version.inner, &user_option, &pool, &redis).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -179,7 +177,7 @@ pub async fn get_update_from_hash(
|
||||
.sorted();
|
||||
|
||||
if let Some(first) = versions.last() {
|
||||
if !is_authorized_version(&first.inner, &user_option, &pool).await? {
|
||||
if !is_visible_version(&first.inner, &user_option, &pool, &redis).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -230,11 +228,11 @@ pub async fn get_versions_from_hashes(
|
||||
.await?;
|
||||
|
||||
let version_ids = files.iter().map(|x| x.version_id).collect::<Vec<_>>();
|
||||
let versions_data = filter_authorized_versions(
|
||||
let versions_data = filter_visible_versions(
|
||||
database::models::Version::get_many(&version_ids, &**pool, &redis).await?,
|
||||
&user_option,
|
||||
&pool,
|
||||
redis,
|
||||
&redis,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -283,7 +281,7 @@ pub async fn get_projects_from_hashes(
|
||||
|
||||
let project_ids = files.iter().map(|x| x.project_id).collect::<Vec<_>>();
|
||||
|
||||
let projects_data = filter_authorized_projects(
|
||||
let projects_data = filter_visible_projects(
|
||||
database::models::Project::get_many_ids(&project_ids, &**pool, &redis).await?,
|
||||
&user_option,
|
||||
&pool,
|
||||
@@ -394,7 +392,7 @@ pub async fn update_files(
|
||||
.last();
|
||||
|
||||
if let Some(version) = version {
|
||||
if is_authorized_version(&version.inner, &user_option, &pool).await? {
|
||||
if is_visible_version(&version.inner, &user_option, &pool, &redis).await? {
|
||||
if let Some(hash) = file.hashes.get(&algorithm) {
|
||||
response.insert(
|
||||
hash.clone(),
|
||||
@@ -516,7 +514,7 @@ pub async fn update_individual_files(
|
||||
.last();
|
||||
|
||||
if let Some(version) = version {
|
||||
if is_authorized_version(&version.inner, &user_option, &pool).await? {
|
||||
if is_visible_version(&version.inner, &user_option, &pool, &redis).await? {
|
||||
response.insert(
|
||||
hash.clone(),
|
||||
models::projects::Version::from(version.clone()),
|
||||
@@ -693,7 +691,7 @@ pub async fn download_version(
|
||||
let version = database::models::Version::get(file.version_id, &**pool, &redis).await?;
|
||||
|
||||
if let Some(version) = version {
|
||||
if !is_authorized_version(&version.inner, &user_option, &pool).await? {
|
||||
if !is_visible_version(&version.inner, &user_option, &pool, &redis).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::ApiError;
|
||||
use crate::auth::{
|
||||
filter_authorized_versions, get_user_from_headers, is_authorized, is_authorized_version,
|
||||
};
|
||||
use crate::auth::checks::{filter_visible_versions, is_visible_project, is_visible_version};
|
||||
use crate::auth::get_user_from_headers;
|
||||
use crate::database;
|
||||
use crate::database::models::loader_fields::{
|
||||
self, LoaderField, LoaderFieldEnumValue, VersionField,
|
||||
@@ -81,7 +80,7 @@ pub async fn version_project_get_helper(
|
||||
.ok();
|
||||
|
||||
if let Some(project) = result {
|
||||
if !is_authorized(&project.inner, &user_option, &pool).await? {
|
||||
if !is_visible_project(&project.inner, &user_option, &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -94,7 +93,7 @@ pub async fn version_project_get_helper(
|
||||
.find(|x| Some(x.inner.id.0 as u64) == id_opt || x.inner.version_number == id.1);
|
||||
|
||||
if let Some(version) = version {
|
||||
if is_authorized_version(&version.inner, &user_option, &pool).await? {
|
||||
if is_visible_version(&version.inner, &user_option, &pool, &redis).await? {
|
||||
return Ok(HttpResponse::Ok().json(models::projects::Version::from(version)));
|
||||
}
|
||||
}
|
||||
@@ -132,7 +131,7 @@ pub async fn versions_get(
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
let versions = filter_authorized_versions(versions_data, &user_option, &pool, redis).await?;
|
||||
let versions = filter_visible_versions(versions_data, &user_option, &pool, &redis).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(versions))
|
||||
}
|
||||
@@ -169,7 +168,7 @@ pub async fn version_get_helper(
|
||||
.ok();
|
||||
|
||||
if let Some(data) = version_data {
|
||||
if is_authorized_version(&data.inner, &user_option, &pool).await? {
|
||||
if is_visible_version(&data.inner, &user_option, &pool, &redis).await? {
|
||||
return Ok(HttpResponse::Ok().json(models::projects::Version::from(data)));
|
||||
}
|
||||
}
|
||||
@@ -723,7 +722,7 @@ pub async fn version_list(
|
||||
.ok();
|
||||
|
||||
if let Some(project) = result {
|
||||
if !is_authorized(&project.inner, &user_option, &pool).await? {
|
||||
if !is_visible_project(&project.inner, &user_option, &pool).await? {
|
||||
return Err(ApiError::NotFound);
|
||||
}
|
||||
|
||||
@@ -819,7 +818,7 @@ pub async fn version_list(
|
||||
response.sort();
|
||||
response.dedup_by(|a, b| a.inner.id == b.inner.id);
|
||||
|
||||
let response = filter_authorized_versions(response, &user_option, &pool, redis).await?;
|
||||
let response = filter_visible_versions(response, &user_option, &pool, &redis).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(response))
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user