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

Commit

Permalink
Organization ownership (#796)
Browse files Browse the repository at this point in the history
* organization changes

* changes

* fixes failing test

* version changes

* removed printlns

* add_team_member comes pre-accepted

* no notification on force accept

* fixes tests

* merge fixes
  • Loading branch information
thesuzerain authored Dec 20, 2023
1 parent 60c535e commit f7b4b78
Show file tree
Hide file tree
Showing 31 changed files with 910 additions and 125 deletions.

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

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

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

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

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

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

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

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

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

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

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

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

This file was deleted.

10 changes: 10 additions & 0 deletions migrations/20231213103100_enforces-owner-unique.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Enforces that there can only be one owner per team
CREATE UNIQUE INDEX idx_one_owner_per_team
ON team_members (team_id)
WHERE is_owner = TRUE;

-- Enforces one team_member per user/team
CREATE UNIQUE INDEX idx_unique_user_team
ON team_members (user_id, team_id);


43 changes: 35 additions & 8 deletions src/auth/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ pub async fn filter_authorized_projects(
"
SELECT m.id id, m.team_id team_id FROM team_members tm
INNER JOIN mods m ON m.team_id = tm.team_id
LEFT JOIN organizations o ON o.team_id = tm.team_id
WHERE (tm.team_id = ANY($1) or o.id = ANY($2)) AND tm.user_id = $3
WHERE tm.team_id = ANY($1) AND tm.user_id = $3
UNION
SELECT m.id id, m.team_id team_id FROM team_members tm
INNER JOIN organizations o ON o.team_id = tm.team_id
INNER JOIN mods m ON m.organization_id = o.id
WHERE o.id = ANY($2) AND tm.user_id = $3
",
&check_projects
.iter()
Expand All @@ -126,7 +132,8 @@ pub async fn filter_authorized_projects(
.try_for_each(|e| {
if let Some(row) = e.right() {
check_projects.retain(|x| {
let bool = x.inner.id.0 == row.id && x.inner.team_id.0 == row.team_id;
let bool =
Some(x.inner.id.0) == row.id && Some(x.inner.team_id.0) == row.team_id;

if bool {
return_projects.push(x.clone().into());
Expand Down Expand Up @@ -160,15 +167,35 @@ pub async fn is_authorized_version(
let user_id: models::ids::UserId = user.id.into();

let version_exists = sqlx::query!(
"SELECT EXISTS(SELECT 1 FROM mods m INNER JOIN team_members tm ON tm.team_id = m.team_id AND user_id = $2 WHERE m.id = $1)",
"SELECT EXISTS(
SELECT 1 FROM mods m
INNER JOIN team_members tm ON tm.team_id = m.team_id AND user_id = $2
WHERE m.id = $1
)",
version_data.project_id as database::models::ids::ProjectId,
user_id as database::models::ids::UserId,
)
.fetch_one(&***pool)
.await?
.exists;
.fetch_one(&***pool)
.await?
.exists;

let version_organization_exists = sqlx::query!(
"SELECT EXISTS(
SELECT 1 FROM mods m
INNER JOIN organizations o ON m.organization_id = o.id
INNER JOIN team_members tm ON tm.team_id = o.team_id AND user_id = $2
WHERE m.id = $1
)",
version_data.project_id as database::models::ids::ProjectId,
user_id as database::models::ids::UserId,
)
.fetch_one(&***pool)
.await?
.exists;

authorized = version_exists.unwrap_or(false);
authorized = version_exists
.or(version_organization_exists)
.unwrap_or(false);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/database/models/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ pub struct ProjectTypeId(pub i32);
pub struct StatusId(pub i32);
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize)]
#[sqlx(transparent)]
pub struct SideTypeId(pub i32);
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize)]
#[sqlx(transparent)]
pub struct GameId(pub i32);
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[sqlx(transparent)]
Expand Down
6 changes: 0 additions & 6 deletions src/database/models/loader_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,6 @@ pub struct QueryLoaderFieldEnumValue {
pub metadata: Option<serde_json::Value>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct SideType {
pub id: SideTypeId,
pub name: String,
}

impl LoaderField {
pub async fn get_field<'a, E>(
field: &str,
Expand Down
Loading

0 comments on commit f7b4b78

Please sign in to comment.