Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into organization-ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain authored Dec 14, 2023
2 parents e412ebd + 50e89ad commit 39f6d64
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 271 deletions.
56 changes: 13 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async-trait = "0.1.70"
dashmap = "5.4.0"
lazy_static = "1.4.0"

meilisearch-sdk = "0.22.0"
meilisearch-sdk = "0.24.3"
rust-s3 = "0.33.0"
reqwest = { version = "0.11.18", features = ["json", "multipart"] }
hyper = { version = "0.14", features = ["full"] }
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ services:
POSTGRES_PASSWORD: labrinth
POSTGRES_HOST_AUTH_METHOD: trust
meilisearch:
image: getmeili/meilisearch:v1.0.1
image: getmeili/meilisearch:v1.5.0
restart: on-failure
ports:
- "7700:7700"
volumes:
- meilisearch-data:/meili_data
- meilisearch-data:/data.ms
environment:
MEILI_MASTER_KEY: modrinth
redis:
Expand Down
70 changes: 23 additions & 47 deletions src/auth/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::database;
use crate::database::models::project_item::QueryProject;
use crate::database::models::version_item::QueryVersion;
use crate::database::models::Collection;
use crate::database::redis::RedisPool;
use crate::database::{models, Project, Version};
use crate::models::users::User;
use crate::routes::ApiError;
Expand Down Expand Up @@ -215,17 +216,17 @@ pub async fn is_authorized_version(
Ok(authorized)
}

impl ValidateAuthorized for crate::database::models::OAuthClient {
impl ValidateAuthorized for models::OAuthClient {
fn validate_authorized(&self, user_option: Option<&User>) -> Result<(), ApiError> {
if let Some(user) = user_option {
if user.role.is_mod() || user.id == self.created_by.into() {
return Ok(());
return if user.role.is_mod() || user.id == self.created_by.into() {
Ok(())
} else {
return Err(crate::routes::ApiError::CustomAuthentication(
Err(ApiError::CustomAuthentication(
"You don't have sufficient permissions to interact with this OAuth application"
.to_string(),
));
}
))
};
}

Ok(())
Expand All @@ -236,58 +237,33 @@ pub async fn filter_authorized_versions(
versions: Vec<QueryVersion>,
user_option: &Option<User>,
pool: &web::Data<PgPool>,
redis: web::Data<RedisPool>,
) -> Result<Vec<crate::models::projects::Version>, ApiError> {
let mut return_versions = Vec::new();
let mut check_versions = Vec::new();

let project_ids = versions
.iter()
.map(|x| x.inner.project_id)
.collect::<Vec<_>>();

let authorized_projects = filter_authorized_projects(
Project::get_many_ids(&project_ids, &***pool, &redis).await?,
user_option,
pool,
)
.await?;

let authorized_project_ids: Vec<_> = authorized_projects.iter().map(|x| x.id.into()).collect();

for version in versions {
if !version.inner.status.is_hidden()
|| user_option
.as_ref()
.map(|x| x.role.is_mod())
.unwrap_or(false)
|| (user_option.is_some() && authorized_project_ids.contains(&version.inner.project_id))
{
return_versions.push(version.into());
} else if user_option.is_some() {
check_versions.push(version);
}
}

if !check_versions.is_empty() {
if let Some(user) = user_option {
let user_id: models::ids::UserId = user.id.into();

use futures::TryStreamExt;

sqlx::query!(
"
SELECT m.id FROM mods m
INNER JOIN team_members tm ON tm.team_id = m.team_id AND user_id = $2
WHERE m.id = ANY($1)
",
&check_versions
.iter()
.map(|x| x.inner.project_id.0)
.collect::<Vec<_>>(),
user_id as database::models::ids::UserId,
)
.fetch_many(&***pool)
.try_for_each(|e| {
if let Some(row) = e.right() {
check_versions.retain(|x| {
let bool = x.inner.project_id.0 == row.id;

if bool {
return_versions.push(x.clone().into());
}

!bool
});
}

futures::future::ready(Ok(()))
})
.await?;
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/database/models/project_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,12 @@ impl Project {
where
E: sqlx::Acquire<'a, Database = sqlx::Postgres>,
{
let project_strings = project_strings
.iter()
.map(|x| x.to_string())
.unique()
.collect::<Vec<String>>();

if project_strings.is_empty() {
return Ok(Vec::new());
}
Expand All @@ -500,10 +506,7 @@ impl Project {
let mut exec = exec.acquire().await?;

let mut found_projects = Vec::new();
let mut remaining_strings = project_strings
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>();
let mut remaining_strings = project_strings.clone();

let mut project_ids = project_strings
.iter()
Expand Down
6 changes: 6 additions & 0 deletions src/database/models/version_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ impl Version {
where
E: sqlx::Acquire<'a, Database = sqlx::Postgres>,
{
let version_ids = version_ids
.iter()
.unique()
.copied()
.collect::<Vec<VersionId>>();

use futures::stream::TryStreamExt;

if version_ids.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod admin;
pub(crate) mod admin;
pub mod flows;
pub mod pats;
pub mod session;
Expand Down
1 change: 1 addition & 0 deletions src/routes/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub async fn forge_updates(
.collect(),
&user_option,
&pool,
redis,
)
.await?;

Expand Down
Loading

0 comments on commit 39f6d64

Please sign in to comment.