| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use crate::db::{self, accounts::Account, repos::RepoRow, Pool}; |
| 6 | | use hub_api::Visibility; |
| 7 | |
|
| 8 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 9 | | #[repr(u8)] |
| 10 | | pub enum Role { |
| 11 | | Read = 1, |
| 12 | | Triage = 2, |
| 13 | | Write = 3, |
| 14 | | Maintain = 4, |
| 15 | | Admin = 5, |
| 16 | | } |
| 17 | |
|
| 18 | | impl Role { |
| 19 | 109× | pub fn parse(s: &str) -> Option<Role> { |
| 20 | 109× | Some(match s { |
| 21 | 109× | "read" => Role::Read, |
| 22 | 108× | "triage" => Role::Triage, |
| 23 | 108× | "write" => Role::Write, |
| 24 | 64× | "maintain" => Role::Maintain, |
| 25 | 0× | "admin" => Role::Admin, |
| 26 | 0× | _ => return None, |
| 27 | | }) |
| 28 | 109× | } |
| 29 | |
|
| 30 | 15× | pub fn as_str(self) -> &'static str { |
| 31 | 15× | match self { |
| 32 | 0× | Role::Read => "read", |
| 33 | 0× | Role::Triage => "triage", |
| 34 | 12× | Role::Write => "write", |
| 35 | 3× | Role::Maintain => "maintain", |
| 36 | 0× | Role::Admin => "admin", |
| 37 | | } |
| 38 | 15× | } |
| 39 | |
|
| 40 | | pub const ALL: [Role; 5] = [Role::Read, Role::Triage, Role::Write, Role::Maintain, Role::Admin]; |
| 41 | | } |
| 42 | |
|
| 43 | |
|
| 44 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 45 | | pub enum Access { |
| 46 | | Read, |
| 47 | | Write, |
| 48 | | Maintain, |
| 49 | | Admin, |
| 50 | | } |
| 51 | |
|
| 52 | | impl Access { |
| 53 | 3880× | fn needs(self) -> Role { |
| 54 | 3880× | match self { |
| 55 | 2204× | Access::Read => Role::Read, |
| 56 | 1039× | Access::Write => Role::Write, |
| 57 | 591× | Access::Maintain => Role::Maintain, |
| 58 | 46× | Access::Admin => Role::Admin, |
| 59 | | } |
| 60 | 3880× | } |
| 61 | | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | 1686× | pub async fn role_for(pool: &Pool, account: Option<&Account>, repo: &RepoRow) -> sqlx::Result<Option<Role>> { |
| 66 | | |
| 67 | | |
| 68 | | |
| 69 | 1686× | if repo.is_drop() { |
| 70 | 24× | return Ok(match account { |
| 71 | 54× | Some(a) if !a.blocked() && a.username.eq_ignore_ascii_case(&repo.owner) => Some(Role::Admin), |
| 72 | 24× | Some(a) if a.blocked() => None, |
| 73 | 52× | _ => Some(Role::Read), |
| 74 | | }); |
| 75 | 1604× | } |
| 76 | 1604× | let visibility = repo.visibility(); |
| 77 | 1604× | let Some(account) = account else { |
| 78 | 492× | return Ok(if visibility == Visibility::Public { Some(Role::Read) } else { None }); |
| 79 | | }; |
| 80 | 1112× | if account.blocked() { |
| 81 | 0× | return Ok(None); |
| 82 | 1112× | } |
| 83 | 1112× | if account.is_admin || account.username.eq_ignore_ascii_case(&repo.owner) { |
| 84 | 932× | return Ok(Some(Role::Admin)); |
| 85 | 180× | } |
| 86 | 180× | let mut best: Option<Role> = None; |
| 87 | 180× | if let Some(org) = db::accounts::org_by_name(pool, &repo.owner).await? { |
| 88 | 3× | match db::accounts::org_role(pool, org.id, account.id).await?.as_deref() { |
| 89 | 1× | Some("owner") => return Ok(Some(Role::Admin)), |
| 90 | 1× | Some(_) => best = Role::parse(&org.default_role), |
| 91 | 2× | None => {} |
| 92 | | } |
| 93 | 177× | } |
| 94 | 180× | if let Some(r) = db::accounts::collaborator_role(pool, repo.id, account.id).await?.as_deref().and_then(Role::parse) { |
| 95 | 41× | best = Some(best.map_or(r, |b| b.max(r))); |
| 96 | 139× | } |
| 97 | 180× | if best.is_none() && visibility != Visibility::Private { |
| 98 | 132× | best = Some(Role::Read); |
| 99 | 132× | } |
| 100 | 180× | Ok(best) |
| 101 | 1686× | } |
| 102 | |
|
| 103 | |
|
| 104 | 3906× | pub fn allows(role: Option<Role>, access: Access) -> bool { |
| 105 | 3906× | role.is_some_and(|r| r >= access.needs()) |
| 106 | 3906× | } |
| 107 | |
|
| 108 | |
|
| 109 | 57× | pub async fn may_create_under(pool: &Pool, account: &Account, owner: &str) -> sqlx::Result<bool> { |
| 110 | 57× | if account.blocked() { |
| 111 | 0× | return Ok(false); |
| 112 | 57× | } |
| 113 | 57× | if account.username.eq_ignore_ascii_case(owner) || account.is_admin { |
| 114 | 54× | return Ok(true); |
| 115 | 3× | } |
| 116 | 3× | match db::accounts::org_by_name(pool, owner).await? { |
| 117 | 1× | Some(org) => Ok(db::accounts::org_role(pool, org.id, account.id).await?.is_some()), |
| 118 | 2× | None => Ok(false), |
| 119 | | } |
| 120 | 57× | } |
| 121 | |
|
| 122 | |
|
| 123 | 41× | pub async fn is_org_owner(pool: &Pool, account: &Account, org_id: i64) -> sqlx::Result<bool> { |
| 124 | 41× | if account.is_admin { |
| 125 | 34× | return Ok(true); |
| 126 | 7× | } |
| 127 | 7× | Ok(db::accounts::org_role(pool, org_id, account.id).await?.as_deref() == Some("owner")) |
| 128 | 41× | } |