| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use super::{now_ms, Pool}; |
| 5 | | use sqlx::FromRow; |
| 6 | |
|
| 7 | | #[derive(Clone, Debug, FromRow)] |
| 8 | | pub struct Account { |
| 9 | | pub id: i64, |
| 10 | | pub username: String, |
| 11 | | pub display_name: String, |
| 12 | | pub email: String, |
| 13 | | pub email_verified: bool, |
| 14 | | pub password_hash: String, |
| 15 | | pub session_salt: String, |
| 16 | | pub totp_secret: Option<String>, |
| 17 | | pub recovery_codes: String, |
| 18 | | pub is_admin: bool, |
| 19 | | pub banned: bool, |
| 20 | | |
| 21 | | #[sqlx(default)] |
| 22 | | pub disabled: bool, |
| 23 | | pub created_ms: i64, |
| 24 | | pub last_seen_ms: i64, |
| 25 | | pub email_notifications: bool, |
| 26 | | pub unsubscribe_key: String, |
| 27 | | |
| 28 | | #[sqlx(default)] |
| 29 | | pub bio: String, |
| 30 | | #[sqlx(default)] |
| 31 | | pub website: String, |
| 32 | | #[sqlx(default)] |
| 33 | | pub location: String, |
| 34 | | |
| 35 | | pub avatar: String, |
| 36 | | |
| 37 | | |
| 38 | | #[sqlx(default)] |
| 39 | | pub bluesky: String, |
| 40 | | #[sqlx(default)] |
| 41 | | pub mastodon: String, |
| 42 | | #[sqlx(default)] |
| 43 | | pub github: String, |
| 44 | | } |
| 45 | |
|
| 46 | | impl Account { |
| 47 | | |
| 48 | 2464× | pub fn blocked(&self) -> bool { |
| 49 | 2464× | self.banned || self.disabled |
| 50 | 2464× | } |
| 51 | |
|
| 52 | 6× | pub fn name(&self) -> &str { |
| 53 | 6× | if self.display_name.is_empty() { |
| 54 | 3× | &self.username |
| 55 | | } else { |
| 56 | 3× | &self.display_name |
| 57 | | } |
| 58 | 6× | } |
| 59 | |
|
| 60 | 33× | pub fn totp_enabled(&self) -> bool { |
| 61 | 33× | self.totp_secret.is_some() |
| 62 | 33× | } |
| 63 | | } |
| 64 | |
|
| 65 | | #[derive(Clone, Debug, FromRow)] |
| 66 | | pub struct Organisation { |
| 67 | | pub id: i64, |
| 68 | | pub name: String, |
| 69 | | pub display_name: String, |
| 70 | | pub description: String, |
| 71 | | pub default_role: String, |
| 72 | | pub created_ms: i64, |
| 73 | | |
| 74 | | pub avatar: String, |
| 75 | | |
| 76 | | |
| 77 | | #[sqlx(default)] |
| 78 | | pub flow_priority: String, |
| 79 | | } |
| 80 | |
|
| 81 | 0× | pub async fn set_org_flow_priority(pool: &Pool, org_id: i64, priority: &str) -> sqlx::Result<()> { |
| 82 | 0× | sqlx::query("UPDATE organisations SET flow_priority = $1 WHERE id = $2").bind(priority).bind(org_id).execute(pool).await?; |
| 83 | 0× | Ok(()) |
| 84 | 0× | } |
| 85 | |
|
| 86 | | #[derive(Clone, Debug, FromRow)] |
| 87 | | pub struct Team { |
| 88 | | pub id: i64, |
| 89 | | pub org_id: i64, |
| 90 | | pub name: String, |
| 91 | | } |
| 92 | |
|
| 93 | | #[derive(Clone, Debug, FromRow)] |
| 94 | | pub struct Token { |
| 95 | | pub id: i64, |
| 96 | | pub account_id: i64, |
| 97 | | pub name: String, |
| 98 | | pub hash: String, |
| 99 | | pub scopes: String, |
| 100 | | pub repos: String, |
| 101 | | pub created_ms: i64, |
| 102 | | pub expires_ms: Option<i64>, |
| 103 | | pub last_used_ms: Option<i64>, |
| 104 | | |
| 105 | | pub renew_ms: Option<i64>, |
| 106 | | |
| 107 | | |
| 108 | | #[sqlx(default)] |
| 109 | | pub agent: bool, |
| 110 | | } |
| 111 | |
|
| 112 | | impl Token { |
| 113 | 485× | pub fn scope_list(&self) -> Vec<&str> { |
| 114 | 818× | self.scopes.split(',').filter(|s| !s.is_empty()).collect() |
| 115 | 485× | } |
| 116 | |
|
| 117 | 304× | pub fn has_scope(&self, scope: &str) -> bool { |
| 118 | 304× | self.scope_list().contains(&scope) |
| 119 | 304× | } |
| 120 | |
|
| 121 | | |
| 122 | | |
| 123 | 155× | pub fn covers(&self, slug: &str) -> bool { |
| 124 | 155× | self.repos.is_empty() || self.repos.split(',').any(|r| r.trim().eq_ignore_ascii_case(slug)) |
| 125 | 155× | } |
| 126 | |
|
| 127 | 376× | pub fn expired(&self, now: i64) -> bool { |
| 128 | 376× | self.expires_ms.is_some_and(|e| e < now) |
| 129 | 376× | } |
| 130 | | } |
| 131 | |
|
| 132 | | #[derive(Clone, Debug, FromRow)] |
| 133 | | pub struct LinkedIdentity { |
| 134 | | pub key: String, |
| 135 | | pub account_id: i64, |
| 136 | | pub name: String, |
| 137 | | pub email: String, |
| 138 | | pub object: Vec<u8>, |
| 139 | | pub linked_ms: i64, |
| 140 | | |
| 141 | | #[sqlx(default)] |
| 142 | | pub last_used_ms: Option<i64>, |
| 143 | | } |
| 144 | |
|
| 145 | | impl LinkedIdentity { |
| 146 | | |
| 147 | 7× | pub fn expires_ms(&self) -> Option<i64> { |
| 148 | 7× | oxy_core::identity::Identity::decode(&self.object).ok()?.expires.map(|e| e as i64) |
| 149 | 7× | } |
| 150 | | } |
| 151 | |
|
| 152 | | #[derive(Clone, Debug, FromRow)] |
| 153 | | pub struct AuditRow { |
| 154 | | pub id: i64, |
| 155 | | pub at_ms: i64, |
| 156 | | pub actor: String, |
| 157 | | pub action: String, |
| 158 | | pub target: String, |
| 159 | | pub detail: String, |
| 160 | | } |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | 1077× | pub async fn account_by_name(pool: &Pool, username: &str) -> sqlx::Result<Option<Account>> { |
| 165 | 1077× | sqlx::query_as("SELECT * FROM accounts WHERE username = $1").bind(username).fetch_optional(pool).await |
| 166 | 1077× | } |
| 167 | |
|
| 168 | 1709× | pub async fn account_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<Account>> { |
| 169 | 1709× | sqlx::query_as("SELECT * FROM accounts WHERE id = $1").bind(id).fetch_optional(pool).await |
| 170 | 1709× | } |
| 171 | |
|
| 172 | 293× | pub async fn accounts(pool: &Pool) -> sqlx::Result<Vec<Account>> { |
| 173 | 293× | sqlx::query_as("SELECT * FROM accounts ORDER BY username").fetch_all(pool).await |
| 174 | 293× | } |
| 175 | |
|
| 176 | 330× | pub async fn account_count(pool: &Pool) -> sqlx::Result<i64> { |
| 177 | 330× | sqlx::query_scalar("SELECT COUNT(*) FROM accounts").fetch_one(pool).await |
| 178 | 330× | } |
| 179 | |
|
| 180 | 197× | pub async fn create_account( |
| 181 | 197× | pool: &Pool, |
| 182 | 197× | username: &str, |
| 183 | 197× | email: &str, |
| 184 | 197× | password_hash: &str, |
| 185 | 197× | session_salt: &str, |
| 186 | 197× | is_admin: bool, |
| 187 | 197× | ) -> sqlx::Result<Account> { |
| 188 | 197× | sqlx::query_as( |
| 189 | 197× | "INSERT INTO accounts (username, email, password_hash, session_salt, is_admin, created_ms) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *", |
| 190 | 197× | ) |
| 191 | 197× | .bind(username) |
| 192 | 197× | .bind(email) |
| 193 | 197× | .bind(password_hash) |
| 194 | 197× | .bind(session_salt) |
| 195 | 197× | .bind(is_admin) |
| 196 | 197× | .bind(now_ms()) |
| 197 | 197× | .fetch_one(pool) |
| 198 | 197× | .await |
| 199 | 197× | } |
| 200 | |
|
| 201 | 9× | pub async fn set_profile(pool: &Pool, id: i64, display_name: &str, email: &str) -> sqlx::Result<()> { |
| 202 | | |
| 203 | 9× | sqlx::query( |
| 204 | 9× | "UPDATE accounts SET display_name = $1, email_verified = CASE WHEN email = $2 THEN email_verified ELSE FALSE END, email = $3 WHERE id = $4", |
| 205 | 9× | ) |
| 206 | 9× | .bind(display_name) |
| 207 | 9× | .bind(email) |
| 208 | 9× | .bind(email) |
| 209 | 9× | .bind(id) |
| 210 | 9× | .execute(pool) |
| 211 | 9× | .await?; |
| 212 | 9× | Ok(()) |
| 213 | 9× | } |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | 6× | pub async fn set_links(pool: &Pool, id: i64, bluesky: &str, mastodon: &str, github: &str) -> sqlx::Result<()> { |
| 218 | 6× | sqlx::query("UPDATE accounts SET bluesky = $1, mastodon = $2, github = $3 WHERE id = $4") |
| 219 | 6× | .bind(bluesky) |
| 220 | 6× | .bind(mastodon) |
| 221 | 6× | .bind(github) |
| 222 | 6× | .bind(id) |
| 223 | 6× | .execute(pool) |
| 224 | 6× | .await?; |
| 225 | 6× | Ok(()) |
| 226 | 6× | } |
| 227 | |
|
| 228 | 6× | pub async fn set_bio(pool: &Pool, id: i64, bio: &str, website: &str, location: &str) -> sqlx::Result<()> { |
| 229 | 6× | sqlx::query("UPDATE accounts SET bio = $1, website = $2, location = $3 WHERE id = $4") |
| 230 | 6× | .bind(bio) |
| 231 | 6× | .bind(website) |
| 232 | 6× | .bind(location) |
| 233 | 6× | .bind(id) |
| 234 | 6× | .execute(pool) |
| 235 | 6× | .await?; |
| 236 | 6× | Ok(()) |
| 237 | 6× | } |
| 238 | |
|
| 239 | 2× | pub async fn set_password(pool: &Pool, id: i64, password_hash: &str, session_salt: &str) -> sqlx::Result<()> { |
| 240 | 2× | sqlx::query("UPDATE accounts SET password_hash = $1, session_salt = $2 WHERE id = $3") |
| 241 | 2× | .bind(password_hash) |
| 242 | 2× | .bind(session_salt) |
| 243 | 2× | .bind(id) |
| 244 | 2× | .execute(pool) |
| 245 | 2× | .await?; |
| 246 | 2× | Ok(()) |
| 247 | 2× | } |
| 248 | |
|
| 249 | 1× | pub async fn set_session_salt(pool: &Pool, id: i64, session_salt: &str) -> sqlx::Result<()> { |
| 250 | 1× | sqlx::query("UPDATE accounts SET session_salt = $1 WHERE id = $2").bind(session_salt).bind(id).execute(pool).await?; |
| 251 | 1× | Ok(()) |
| 252 | 1× | } |
| 253 | |
|
| 254 | 1× | pub async fn set_totp(pool: &Pool, id: i64, secret: Option<&str>, recovery_codes: &str) -> sqlx::Result<()> { |
| 255 | 1× | sqlx::query("UPDATE accounts SET totp_secret = $1, recovery_codes = $2 WHERE id = $3") |
| 256 | 1× | .bind(secret) |
| 257 | 1× | .bind(recovery_codes) |
| 258 | 1× | .bind(id) |
| 259 | 1× | .execute(pool) |
| 260 | 1× | .await?; |
| 261 | 1× | Ok(()) |
| 262 | 1× | } |
| 263 | |
|
| 264 | 6× | pub async fn set_admin(pool: &Pool, id: i64, is_admin: bool) -> sqlx::Result<()> { |
| 265 | 6× | sqlx::query("UPDATE accounts SET is_admin = $1 WHERE id = $2").bind(is_admin).bind(id).execute(pool).await?; |
| 266 | 6× | Ok(()) |
| 267 | 6× | } |
| 268 | |
|
| 269 | 0× | pub async fn set_banned(pool: &Pool, id: i64, banned: bool) -> sqlx::Result<()> { |
| 270 | 0× | sqlx::query("UPDATE accounts SET banned = $1 WHERE id = $2").bind(banned).bind(id).execute(pool).await?; |
| 271 | 0× | Ok(()) |
| 272 | 0× | } |
| 273 | |
|
| 274 | 2× | pub async fn set_disabled(pool: &Pool, id: i64, disabled: bool) -> sqlx::Result<()> { |
| 275 | 2× | sqlx::query("UPDATE accounts SET disabled = $1 WHERE id = $2").bind(disabled).bind(id).execute(pool).await?; |
| 276 | 2× | Ok(()) |
| 277 | 2× | } |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | 1× | pub async fn delete_account(pool: &Pool, id: i64, username: &str) -> sqlx::Result<()> { |
| 284 | 1× | let mut tx = super::begin_write(pool).await?; |
| 285 | 1× | sqlx::query("INSERT INTO retired_usernames (username, retired_ms) VALUES ($1, $2) ON CONFLICT DO NOTHING") |
| 286 | 1× | .bind(username) |
| 287 | 1× | .bind(now_ms()) |
| 288 | 1× | .execute(&mut *tx) |
| 289 | 1× | .await?; |
| 290 | 1× | sqlx::query("DELETE FROM accounts WHERE id = $1").bind(id).execute(&mut *tx).await?; |
| 291 | 1× | tx.commit().await |
| 292 | 1× | } |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | 2× | pub async fn orgs_owned_only_by(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<String>> { |
| 297 | 2× | sqlx::query_scalar( |
| 298 | 2× | "SELECT o.name FROM organisations o JOIN org_members m ON m.org_id = o.id \ |
| 299 | 2× | WHERE m.account_id = $1 AND m.role = 'owner' \ |
| 300 | 2× | AND (SELECT COUNT(*) FROM org_members x WHERE x.org_id = o.id AND x.role = 'owner') = 1 \ |
| 301 | 2× | ORDER BY o.name", |
| 302 | 2× | ) |
| 303 | 2× | .bind(account_id) |
| 304 | 2× | .fetch_all(pool) |
| 305 | 2× | .await |
| 306 | 2× | } |
| 307 | |
|
| 308 | |
|
| 309 | 130× | pub async fn username_retired(pool: &Pool, name: &str) -> sqlx::Result<bool> { |
| 310 | 130× | Ok(sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM retired_usernames WHERE username = $1").bind(name).fetch_one(pool).await? > 0) |
| 311 | 130× | } |
| 312 | |
|
| 313 | 1026× | pub async fn touch_seen(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 314 | 1026× | sqlx::query("UPDATE accounts SET last_seen_ms = $1 WHERE id = $2").bind(now_ms()).bind(id).execute(pool).await?; |
| 315 | 1025× | Ok(()) |
| 316 | 1025× | } |
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | 2540× | pub async fn org_by_name(pool: &Pool, name: &str) -> sqlx::Result<Option<Organisation>> { |
| 321 | 2540× | sqlx::query_as("SELECT * FROM organisations WHERE name = $1").bind(name).fetch_optional(pool).await |
| 322 | 2539× | } |
| 323 | |
|
| 324 | 96× | pub async fn orgs(pool: &Pool) -> sqlx::Result<Vec<Organisation>> { |
| 325 | 96× | sqlx::query_as("SELECT * FROM organisations ORDER BY name").fetch_all(pool).await |
| 326 | 96× | } |
| 327 | |
|
| 328 | |
|
| 329 | | #[derive(FromRow)] |
| 330 | | struct Membership { |
| 331 | | #[sqlx(flatten)] |
| 332 | | org: Organisation, |
| 333 | | role: String, |
| 334 | | } |
| 335 | |
|
| 336 | 113× | pub async fn orgs_of(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<(Organisation, String)>> { |
| 337 | 113× | let rows: Vec<Membership> = sqlx::query_as( |
| 338 | 113× | "SELECT o.*, m.role FROM organisations o JOIN org_members m ON m.org_id = o.id WHERE m.account_id = $1 ORDER BY o.name", |
| 339 | 113× | ) |
| 340 | 113× | .bind(account_id) |
| 341 | 113× | .fetch_all(pool) |
| 342 | 113× | .await?; |
| 343 | 113× | Ok(rows.into_iter().map(|m| (m.org, m.role)).collect()) |
| 344 | 113× | } |
| 345 | |
|
| 346 | 13× | pub async fn create_org(pool: &Pool, name: &str, display_name: &str, owner: i64) -> sqlx::Result<Organisation> { |
| 347 | 13× | let mut tx = super::begin_write(pool).await?; |
| 348 | 13× | let org: Organisation = sqlx::query_as("INSERT INTO organisations (name, display_name, created_ms) VALUES ($1, $2, $3) RETURNING *") |
| 349 | 13× | .bind(name) |
| 350 | 13× | .bind(display_name) |
| 351 | 13× | .bind(now_ms()) |
| 352 | 13× | .fetch_one(&mut *tx) |
| 353 | 13× | .await?; |
| 354 | 13× | sqlx::query("INSERT INTO org_members (org_id, account_id, role) VALUES ($1, $2, 'owner')") |
| 355 | 13× | .bind(org.id) |
| 356 | 13× | .bind(owner) |
| 357 | 13× | .execute(&mut *tx) |
| 358 | 13× | .await?; |
| 359 | 13× | tx.commit().await?; |
| 360 | 13× | Ok(org) |
| 361 | 13× | } |
| 362 | |
|
| 363 | 0× | pub async fn set_org(pool: &Pool, id: i64, display_name: &str, description: &str, default_role: &str) -> sqlx::Result<()> { |
| 364 | 0× | sqlx::query("UPDATE organisations SET display_name = $1, description = $2, default_role = $3 WHERE id = $4") |
| 365 | 0× | .bind(display_name) |
| 366 | 0× | .bind(description) |
| 367 | 0× | .bind(default_role) |
| 368 | 0× | .bind(id) |
| 369 | 0× | .execute(pool) |
| 370 | 0× | .await?; |
| 371 | 0× | Ok(()) |
| 372 | 0× | } |
| 373 | |
|
| 374 | |
|
| 375 | 26× | pub async fn org_members(pool: &Pool, org_id: i64) -> sqlx::Result<Vec<(Account, String)>> { |
| 376 | 26× | let rows: Vec<(i64, String)> = |
| 377 | 26× | sqlx::query_as("SELECT account_id, role FROM org_members WHERE org_id = $1").bind(org_id).fetch_all(pool).await?; |
| 378 | 26× | let mut out = Vec::new(); |
| 379 | 36× | for (id, role) in rows { |
| 380 | 36× | if let Some(a) = account_by_id(pool, id).await? { |
| 381 | 36× | out.push((a, role)); |
| 382 | 36× | } |
| 383 | | } |
| 384 | 26× | out.sort_by(|a, b| a.0.username.cmp(&b.0.username)); |
| 385 | 26× | Ok(out) |
| 386 | 26× | } |
| 387 | |
|
| 388 | 14× | pub async fn org_role(pool: &Pool, org_id: i64, account_id: i64) -> sqlx::Result<Option<String>> { |
| 389 | 14× | sqlx::query_scalar("SELECT role FROM org_members WHERE org_id = $1 AND account_id = $2") |
| 390 | 14× | .bind(org_id) |
| 391 | 14× | .bind(account_id) |
| 392 | 14× | .fetch_optional(pool) |
| 393 | 14× | .await |
| 394 | 14× | } |
| 395 | |
|
| 396 | 3× | pub async fn set_org_member(pool: &Pool, org_id: i64, account_id: i64, role: &str) -> sqlx::Result<()> { |
| 397 | 3× | sqlx::query("INSERT INTO org_members (org_id, account_id, role) VALUES ($1, $2, $3) ON CONFLICT (org_id, account_id) DO UPDATE SET role = excluded.role") |
| 398 | 3× | .bind(org_id) |
| 399 | 3× | .bind(account_id) |
| 400 | 3× | .bind(role) |
| 401 | 3× | .execute(pool) |
| 402 | 3× | .await?; |
| 403 | 3× | Ok(()) |
| 404 | 3× | } |
| 405 | |
|
| 406 | 0× | pub async fn remove_org_member(pool: &Pool, org_id: i64, account_id: i64) -> sqlx::Result<()> { |
| 407 | 0× | sqlx::query("DELETE FROM org_members WHERE org_id = $1 AND account_id = $2").bind(org_id).bind(account_id).execute(pool).await?; |
| 408 | 0× | Ok(()) |
| 409 | 0× | } |
| 410 | |
|
| 411 | 32× | pub async fn teams(pool: &Pool, org_id: i64) -> sqlx::Result<Vec<Team>> { |
| 412 | 32× | sqlx::query_as("SELECT * FROM teams WHERE org_id = $1 ORDER BY name").bind(org_id).fetch_all(pool).await |
| 413 | 32× | } |
| 414 | |
|
| 415 | 0× | pub async fn team_by_name(pool: &Pool, org_id: i64, name: &str) -> sqlx::Result<Option<Team>> { |
| 416 | 0× | sqlx::query_as("SELECT * FROM teams WHERE org_id = $1 AND name = $2").bind(org_id).bind(name).fetch_optional(pool).await |
| 417 | 0× | } |
| 418 | |
|
| 419 | 1× | pub async fn create_team(pool: &Pool, org_id: i64, name: &str) -> sqlx::Result<Team> { |
| 420 | 1× | sqlx::query_as("INSERT INTO teams (org_id, name) VALUES ($1, $2) RETURNING *").bind(org_id).bind(name).fetch_one(pool).await |
| 421 | 1× | } |
| 422 | |
|
| 423 | 0× | pub async fn delete_team(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 424 | 0× | sqlx::query("DELETE FROM teams WHERE id = $1").bind(id).execute(pool).await?; |
| 425 | 0× | Ok(()) |
| 426 | 0× | } |
| 427 | |
|
| 428 | 2× | pub async fn team_members(pool: &Pool, team_id: i64) -> sqlx::Result<Vec<Account>> { |
| 429 | 2× | sqlx::query_as("SELECT a.* FROM accounts a JOIN team_members t ON t.account_id = a.id WHERE t.team_id = $1 ORDER BY a.username") |
| 430 | 2× | .bind(team_id) |
| 431 | 2× | .fetch_all(pool) |
| 432 | 2× | .await |
| 433 | 2× | } |
| 434 | |
|
| 435 | 1× | pub async fn add_team_member(pool: &Pool, team_id: i64, account_id: i64) -> sqlx::Result<()> { |
| 436 | 1× | sqlx::query("INSERT INTO team_members (team_id, account_id) VALUES ($1, $2) ON CONFLICT DO NOTHING") |
| 437 | 1× | .bind(team_id) |
| 438 | 1× | .bind(account_id) |
| 439 | 1× | .execute(pool) |
| 440 | 1× | .await?; |
| 441 | 1× | Ok(()) |
| 442 | 1× | } |
| 443 | |
|
| 444 | 0× | pub async fn remove_team_member(pool: &Pool, team_id: i64, account_id: i64) -> sqlx::Result<()> { |
| 445 | 0× | sqlx::query("DELETE FROM team_members WHERE team_id = $1 AND account_id = $2").bind(team_id).bind(account_id).execute(pool).await?; |
| 446 | 0× | Ok(()) |
| 447 | 0× | } |
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | 72× | pub async fn assignable(pool: &Pool, repo_id: i64, owner: &str) -> sqlx::Result<Vec<String>> { |
| 456 | 72× | sqlx::query_scalar( |
| 457 | 72× | "SELECT username FROM accounts WHERE banned = FALSE AND disabled = FALSE AND id IN (\ |
| 458 | 72× | SELECT id FROM accounts WHERE username = $1 \ |
| 459 | 72× | UNION SELECT account_id FROM collaborators WHERE repo_id = $2 AND account_id IS NOT NULL \ |
| 460 | 72× | UNION SELECT tm.account_id FROM team_members tm JOIN collaborators c ON c.team_id = tm.team_id WHERE c.repo_id = $2 \ |
| 461 | 72× | UNION SELECT m.account_id FROM org_members m JOIN organisations o ON o.id = m.org_id WHERE o.name = $1) \ |
| 462 | 72× | ORDER BY lower(username), username", |
| 463 | 72× | ) |
| 464 | 72× | .bind(owner) |
| 465 | 72× | .bind(repo_id) |
| 466 | 72× | .fetch_all(pool) |
| 467 | 72× | .await |
| 468 | 72× | } |
| 469 | |
|
| 470 | | #[derive(Clone, Debug)] |
| 471 | | pub struct Collaborator { |
| 472 | | pub account: Option<Account>, |
| 473 | | pub team: Option<Team>, |
| 474 | | pub role: String, |
| 475 | | } |
| 476 | |
|
| 477 | 133× | pub async fn collaborators(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<Collaborator>> { |
| 478 | 133× | let rows: Vec<(Option<i64>, Option<i64>, String)> = |
| 479 | 133× | sqlx::query_as("SELECT account_id, team_id, role FROM collaborators WHERE repo_id = $1").bind(repo_id).fetch_all(pool).await?; |
| 480 | 133× | let mut out = Vec::new(); |
| 481 | 133× | for (account_id, team_id, role) in rows { |
| 482 | 122× | let account = match account_id { |
| 483 | 122× | Some(id) => account_by_id(pool, id).await?, |
| 484 | 0× | None => None, |
| 485 | | }; |
| 486 | 122× | let team = match team_id { |
| 487 | 0× | Some(id) => sqlx::query_as("SELECT * FROM teams WHERE id = $1").bind(id).fetch_optional(pool).await?, |
| 488 | 122× | None => None, |
| 489 | | }; |
| 490 | 122× | out.push(Collaborator { account, team, role }); |
| 491 | | } |
| 492 | 133× | Ok(out) |
| 493 | 133× | } |
| 494 | |
|
| 495 | 7× | pub async fn set_collaborator(pool: &Pool, repo_id: i64, account_id: Option<i64>, team_id: Option<i64>, role: &str) -> sqlx::Result<()> { |
| 496 | 7× | if let Some(a) = account_id { |
| 497 | 7× | sqlx::query("INSERT INTO collaborators (repo_id, account_id, role) VALUES ($1, $2, $3) ON CONFLICT (repo_id, account_id) WHERE account_id IS NOT NULL DO UPDATE SET role = excluded.role") |
| 498 | 7× | .bind(repo_id) |
| 499 | 7× | .bind(a) |
| 500 | 7× | .bind(role) |
| 501 | 7× | .execute(pool) |
| 502 | 7× | .await?; |
| 503 | 0× | } else if let Some(t) = team_id { |
| 504 | 0× | sqlx::query("INSERT INTO collaborators (repo_id, team_id, role) VALUES ($1, $2, $3) ON CONFLICT (repo_id, team_id) WHERE team_id IS NOT NULL DO UPDATE SET role = excluded.role") |
| 505 | 0× | .bind(repo_id) |
| 506 | 0× | .bind(t) |
| 507 | 0× | .bind(role) |
| 508 | 0× | .execute(pool) |
| 509 | 0× | .await?; |
| 510 | 0× | } |
| 511 | 7× | Ok(()) |
| 512 | 7× | } |
| 513 | |
|
| 514 | 0× | pub async fn remove_collaborator(pool: &Pool, repo_id: i64, account_id: Option<i64>, team_id: Option<i64>) -> sqlx::Result<()> { |
| 515 | 0× | sqlx::query("DELETE FROM collaborators WHERE repo_id = $1 AND ((account_id IS NOT NULL AND account_id = $2) OR (team_id IS NOT NULL AND team_id = $3))") |
| 516 | 0× | .bind(repo_id) |
| 517 | 0× | .bind(account_id) |
| 518 | 0× | .bind(team_id) |
| 519 | 0× | .execute(pool) |
| 520 | 0× | .await?; |
| 521 | 0× | Ok(()) |
| 522 | 0× | } |
| 523 | |
|
| 524 | |
|
| 525 | 182× | pub async fn collaborator_role(pool: &Pool, repo_id: i64, account_id: i64) -> sqlx::Result<Option<String>> { |
| 526 | 182× | let rows: Vec<String> = sqlx::query_scalar( |
| 527 | 182× | "SELECT role FROM collaborators WHERE repo_id = $1 AND (account_id = $2 OR team_id IN (SELECT team_id FROM team_members WHERE account_id = $3))", |
| 528 | 182× | ) |
| 529 | 182× | .bind(repo_id) |
| 530 | 182× | .bind(account_id) |
| 531 | 182× | .bind(account_id) |
| 532 | 182× | .fetch_all(pool) |
| 533 | 182× | .await?; |
| 534 | 182× | Ok(rows.into_iter().max_by_key(|r| crate::auth::Role::parse(r).map(|r| r as u8).unwrap_or(0))) |
| 535 | 182× | } |
| 536 | |
|
| 537 | |
|
| 538 | |
|
| 539 | 37× | pub async fn tokens_of(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<Token>> { |
| 540 | 37× | sqlx::query_as("SELECT * FROM tokens WHERE account_id = $1 ORDER BY created_ms DESC").bind(account_id).fetch_all(pool).await |
| 541 | 37× | } |
| 542 | |
|
| 543 | |
|
| 544 | 3× | pub async fn set_token_agent(pool: &Pool, account_id: i64, id: i64, agent: bool) -> sqlx::Result<bool> { |
| 545 | 3× | let r = sqlx::query("UPDATE tokens SET agent = $1 WHERE id = $2 AND account_id = $3") |
| 546 | 3× | .bind(agent) |
| 547 | 3× | .bind(id) |
| 548 | 3× | .bind(account_id) |
| 549 | 3× | .execute(pool) |
| 550 | 3× | .await?; |
| 551 | 3× | Ok(r.rows_affected() > 0) |
| 552 | 3× | } |
| 553 | |
|
| 554 | 2066× | pub async fn token_by_hash(pool: &Pool, hash: &str) -> sqlx::Result<Option<Token>> { |
| 555 | 2066× | sqlx::query_as("SELECT * FROM tokens WHERE hash = $1").bind(hash).fetch_optional(pool).await |
| 556 | 2066× | } |
| 557 | |
|
| 558 | 42× | pub async fn create_token( |
| 559 | 42× | pool: &Pool, |
| 560 | 42× | account_id: i64, |
| 561 | 42× | name: &str, |
| 562 | 42× | hash: &str, |
| 563 | 42× | scopes: &str, |
| 564 | 42× | repos: &str, |
| 565 | 42× | expires_ms: Option<i64>, |
| 566 | 42× | ) -> sqlx::Result<Token> { |
| 567 | 42× | sqlx::query_as( |
| 568 | 42× | "INSERT INTO tokens (account_id, name, hash, scopes, repos, created_ms, expires_ms) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *", |
| 569 | 42× | ) |
| 570 | 42× | .bind(account_id) |
| 571 | 42× | .bind(name) |
| 572 | 42× | .bind(hash) |
| 573 | 42× | .bind(scopes) |
| 574 | 42× | .bind(repos) |
| 575 | 42× | .bind(now_ms()) |
| 576 | 42× | .bind(expires_ms) |
| 577 | 42× | .fetch_one(pool) |
| 578 | 42× | .await |
| 579 | 42× | } |
| 580 | |
|
| 581 | 1× | pub async fn delete_token(pool: &Pool, account_id: i64, id: i64) -> sqlx::Result<()> { |
| 582 | 1× | sqlx::query("DELETE FROM tokens WHERE id = $1 AND account_id = $2").bind(id).bind(account_id).execute(pool).await?; |
| 583 | 1× | Ok(()) |
| 584 | 1× | } |
| 585 | |
|
| 586 | |
|
| 587 | 357× | pub async fn touch_token(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 588 | 357× | sqlx::query( |
| 589 | 357× | "UPDATE tokens SET last_used_ms = $1, \ |
| 590 | 357× | expires_ms = CASE WHEN renew_ms IS NULL THEN expires_ms WHEN COALESCE(expires_ms, 0) > $1 + renew_ms THEN expires_ms ELSE $1 + renew_ms END \ |
| 591 | 357× | WHERE id = $2", |
| 592 | 357× | ) |
| 593 | 357× | .bind(now_ms()) |
| 594 | 357× | .bind(id) |
| 595 | 357× | .execute(pool) |
| 596 | 357× | .await?; |
| 597 | 357× | Ok(()) |
| 598 | 357× | } |
| 599 | |
|
| 600 | |
|
| 601 | 1× | pub async fn set_token_renewal(pool: &Pool, id: i64, renew_ms: i64) -> sqlx::Result<()> { |
| 602 | 1× | sqlx::query("UPDATE tokens SET renew_ms = $1 WHERE id = $2").bind(renew_ms).bind(id).execute(pool).await?; |
| 603 | 1× | Ok(()) |
| 604 | 1× | } |
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
|
| 609 | |
|
| 610 | | #[derive(Clone, Debug, FromRow)] |
| 611 | | pub struct DeviceCode { |
| 612 | | pub id: i64, |
| 613 | | pub device_hash: String, |
| 614 | | pub user_code: String, |
| 615 | | pub client: String, |
| 616 | | pub scopes: String, |
| 617 | | pub created_ms: i64, |
| 618 | | pub expires_ms: i64, |
| 619 | | pub last_poll_ms: i64, |
| 620 | | pub account_id: Option<i64>, |
| 621 | | pub approved_scopes: String, |
| 622 | | pub denied: bool, |
| 623 | | |
| 624 | | pub requester: String, |
| 625 | | } |
| 626 | |
|
| 627 | | impl DeviceCode { |
| 628 | 17× | pub fn scope_list(&self) -> Vec<&str> { |
| 629 | 34× | self.scopes.split(',').filter(|s| !s.is_empty()).collect() |
| 630 | 17× | } |
| 631 | | } |
| 632 | |
|
| 633 | 3× | pub async fn create_device_code( |
| 634 | 3× | pool: &Pool, |
| 635 | 3× | device_hash: &str, |
| 636 | 3× | user_code: &str, |
| 637 | 3× | client: &str, |
| 638 | 3× | scopes: &str, |
| 639 | 3× | expires_ms: i64, |
| 640 | 3× | requester: &str, |
| 641 | 3× | ) -> sqlx::Result<DeviceCode> { |
| 642 | | |
| 643 | 3× | sqlx::query("DELETE FROM device_codes WHERE expires_ms < $1").bind(now_ms()).execute(pool).await?; |
| 644 | 3× | sqlx::query_as( |
| 645 | 3× | "INSERT INTO device_codes (device_hash, user_code, client, scopes, created_ms, expires_ms, requester) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *", |
| 646 | 3× | ) |
| 647 | 3× | .bind(device_hash) |
| 648 | 3× | .bind(user_code) |
| 649 | 3× | .bind(client) |
| 650 | 3× | .bind(scopes) |
| 651 | 3× | .bind(now_ms()) |
| 652 | 3× | .bind(expires_ms) |
| 653 | 3× | .bind(requester) |
| 654 | 3× | .fetch_one(pool) |
| 655 | 3× | .await |
| 656 | 3× | } |
| 657 | |
|
| 658 | 6× | pub async fn device_by_user_code(pool: &Pool, user_code: &str) -> sqlx::Result<Option<DeviceCode>> { |
| 659 | 6× | sqlx::query_as("SELECT * FROM device_codes WHERE user_code = $1").bind(user_code).fetch_optional(pool).await |
| 660 | 6× | } |
| 661 | |
|
| 662 | 8× | pub async fn device_by_hash(pool: &Pool, device_hash: &str) -> sqlx::Result<Option<DeviceCode>> { |
| 663 | 8× | sqlx::query_as("SELECT * FROM device_codes WHERE device_hash = $1").bind(device_hash).fetch_optional(pool).await |
| 664 | 8× | } |
| 665 | |
|
| 666 | |
|
| 667 | |
|
| 668 | 1× | pub async fn approve_device(pool: &Pool, id: i64, account_id: i64, scopes: &str) -> sqlx::Result<bool> { |
| 669 | 1× | let r = sqlx::query( |
| 670 | 1× | "UPDATE device_codes SET account_id = $1, approved_scopes = $2 WHERE id = $3 AND account_id IS NULL AND denied = FALSE AND expires_ms >= $4", |
| 671 | 1× | ) |
| 672 | 1× | .bind(account_id) |
| 673 | 1× | .bind(scopes) |
| 674 | 1× | .bind(id) |
| 675 | 1× | .bind(now_ms()) |
| 676 | 1× | .execute(pool) |
| 677 | 1× | .await?; |
| 678 | 1× | Ok(r.rows_affected() == 1) |
| 679 | 1× | } |
| 680 | |
|
| 681 | 1× | pub async fn deny_device(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 682 | 1× | sqlx::query("UPDATE device_codes SET denied = TRUE WHERE id = $1 AND account_id IS NULL").bind(id).execute(pool).await?; |
| 683 | 1× | Ok(()) |
| 684 | 1× | } |
| 685 | |
|
| 686 | 2× | pub async fn device_polled(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 687 | 2× | sqlx::query("UPDATE device_codes SET last_poll_ms = $1 WHERE id = $2").bind(now_ms()).bind(id).execute(pool).await?; |
| 688 | 2× | Ok(()) |
| 689 | 2× | } |
| 690 | |
|
| 691 | |
|
| 692 | |
|
| 693 | 3× | pub async fn delete_device(pool: &Pool, id: i64) -> sqlx::Result<bool> { |
| 694 | 3× | let r = sqlx::query("DELETE FROM device_codes WHERE id = $1").bind(id).execute(pool).await?; |
| 695 | 3× | Ok(r.rows_affected() == 1) |
| 696 | 3× | } |
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | 160× | pub async fn identities_of(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<LinkedIdentity>> { |
| 701 | 160× | sqlx::query_as("SELECT * FROM identities WHERE account_id = $1 ORDER BY linked_ms DESC").bind(account_id).fetch_all(pool).await |
| 702 | 160× | } |
| 703 | |
|
| 704 | |
|
| 705 | |
|
| 706 | 79× | pub async fn identities_used(pool: &Pool, keys: &[String]) -> sqlx::Result<()> { |
| 707 | 79× | let now = now_ms(); |
| 708 | 80× | for k in keys { |
| 709 | 80× | sqlx::query("UPDATE identities SET last_used_ms = $1 WHERE key = $2 AND (last_used_ms IS NULL OR last_used_ms < $3)") |
| 710 | 80× | .bind(now) |
| 711 | 80× | .bind(k) |
| 712 | 80× | .bind(now - 60_000) |
| 713 | 80× | .execute(pool) |
| 714 | 80× | .await?; |
| 715 | | } |
| 716 | 79× | Ok(()) |
| 717 | 79× | } |
| 718 | |
|
| 719 | 15× | pub async fn identity_by_key(pool: &Pool, key: &str) -> sqlx::Result<Option<LinkedIdentity>> { |
| 720 | 15× | sqlx::query_as("SELECT * FROM identities WHERE key = $1").bind(key).fetch_optional(pool).await |
| 721 | 15× | } |
| 722 | |
|
| 723 | |
|
| 724 | 316× | pub async fn identity_accounts(pool: &Pool) -> sqlx::Result<Vec<(String, String)>> { |
| 725 | 316× | sqlx::query_as("SELECT i.key, a.username FROM identities i JOIN accounts a ON a.id = i.account_id").fetch_all(pool).await |
| 726 | 316× | } |
| 727 | |
|
| 728 | 15× | pub async fn link_identity(pool: &Pool, account_id: i64, key: &str, name: &str, email: &str, object: &[u8]) -> sqlx::Result<()> { |
| 729 | 15× | sqlx::query("INSERT INTO identities (key, account_id, name, email, object, linked_ms) VALUES ($1, $2, $3, $4, $5, $6)") |
| 730 | 15× | .bind(key) |
| 731 | 15× | .bind(account_id) |
| 732 | 15× | .bind(name) |
| 733 | 15× | .bind(email) |
| 734 | 15× | .bind(object) |
| 735 | 15× | .bind(now_ms()) |
| 736 | 15× | .execute(pool) |
| 737 | 15× | .await?; |
| 738 | 15× | Ok(()) |
| 739 | 15× | } |
| 740 | |
|
| 741 | 0× | pub async fn unlink_identity(pool: &Pool, account_id: i64, key: &str) -> sqlx::Result<()> { |
| 742 | 0× | sqlx::query("DELETE FROM identities WHERE key = $1 AND account_id = $2").bind(key).bind(account_id).execute(pool).await?; |
| 743 | 0× | Ok(()) |
| 744 | 0× | } |
| 745 | |
|
| 746 | 2× | pub async fn create_challenge(pool: &Pool, account_id: i64, bytes_hex: &str) -> sqlx::Result<i64> { |
| 747 | 2× | sqlx::query("DELETE FROM challenges WHERE created_ms < $1").bind(now_ms() - 10 * 60 * 1000).execute(pool).await?; |
| 748 | 2× | let id: i64 = sqlx::query_scalar("INSERT INTO challenges (account_id, bytes, created_ms) VALUES ($1, $2, $3) RETURNING id") |
| 749 | 2× | .bind(account_id) |
| 750 | 2× | .bind(bytes_hex) |
| 751 | 2× | .bind(now_ms()) |
| 752 | 2× | .fetch_one(pool) |
| 753 | 2× | .await?; |
| 754 | 2× | Ok(id) |
| 755 | 2× | } |
| 756 | |
|
| 757 | |
|
| 758 | 3× | pub async fn take_challenge(pool: &Pool, account_id: i64, id: i64) -> sqlx::Result<Option<String>> { |
| 759 | 3× | let bytes: Option<String> = sqlx::query_scalar("SELECT bytes FROM challenges WHERE id = $1 AND account_id = $2 AND created_ms >= $3") |
| 760 | 3× | .bind(id) |
| 761 | 3× | .bind(account_id) |
| 762 | 3× | .bind(now_ms() - 10 * 60 * 1000) |
| 763 | 3× | .fetch_optional(pool) |
| 764 | 3× | .await?; |
| 765 | 3× | sqlx::query("DELETE FROM challenges WHERE id = $1").bind(id).execute(pool).await?; |
| 766 | 3× | Ok(bytes) |
| 767 | 3× | } |
| 768 | |
|
| 769 | |
|
| 770 | |
|
| 771 | 10× | pub async fn create_invitation(pool: &Pool, code: &str, created_by: Option<i64>) -> sqlx::Result<()> { |
| 772 | 10× | sqlx::query("INSERT INTO invitations (code, created_by, created_ms) VALUES ($1, $2, $3)") |
| 773 | 10× | .bind(code) |
| 774 | 10× | .bind(created_by) |
| 775 | 10× | .bind(now_ms()) |
| 776 | 10× | .execute(pool) |
| 777 | 10× | .await?; |
| 778 | 10× | Ok(()) |
| 779 | 10× | } |
| 780 | |
|
| 781 | 15× | pub async fn invitation_unused(pool: &Pool, code: &str) -> sqlx::Result<bool> { |
| 782 | 15× | let n: i64 = |
| 783 | 15× | sqlx::query_scalar("SELECT COUNT(*) FROM invitations WHERE code = $1 AND used_by IS NULL").bind(code).fetch_one(pool).await?; |
| 784 | 15× | Ok(n > 0) |
| 785 | 15× | } |
| 786 | |
|
| 787 | |
|
| 788 | 1× | pub async fn claim_invitation(pool: &Pool, code: &str, account_id: i64) -> sqlx::Result<bool> { |
| 789 | 1× | let r = sqlx::query("UPDATE invitations SET used_by = $1 WHERE code = $2 AND used_by IS NULL") |
| 790 | 1× | .bind(account_id) |
| 791 | 1× | .bind(code) |
| 792 | 1× | .execute(pool) |
| 793 | 1× | .await?; |
| 794 | 1× | Ok(r.rows_affected() > 0) |
| 795 | 1× | } |
| 796 | |
|
| 797 | 8× | pub async fn use_invitation(pool: &Pool, code: &str, account_id: i64) -> sqlx::Result<()> { |
| 798 | 8× | sqlx::query("UPDATE invitations SET used_by = $1 WHERE code = $2").bind(account_id).bind(code).execute(pool).await?; |
| 799 | 8× | Ok(()) |
| 800 | 8× | } |
| 801 | |
|
| 802 | 96× | pub async fn invitations(pool: &Pool) -> sqlx::Result<Vec<(String, Option<i64>, i64)>> { |
| 803 | 96× | sqlx::query_as("SELECT code, used_by, created_ms FROM invitations ORDER BY created_ms DESC").fetch_all(pool).await |
| 804 | 96× | } |
| 805 | |
|
| 806 | |
|
| 807 | |
|
| 808 | 2806× | pub async fn setting(pool: &Pool, key: &str) -> sqlx::Result<Option<String>> { |
| 809 | 2806× | sqlx::query_scalar("SELECT value FROM settings WHERE key = $1").bind(key).fetch_optional(pool).await |
| 810 | 2806× | } |
| 811 | |
|
| 812 | 57× | pub async fn set_setting(pool: &Pool, key: &str, value: &str) -> sqlx::Result<()> { |
| 813 | 57× | sqlx::query("INSERT INTO settings (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = excluded.value") |
| 814 | 57× | .bind(key) |
| 815 | 57× | .bind(value) |
| 816 | 57× | .execute(pool) |
| 817 | 57× | .await?; |
| 818 | 57× | Ok(()) |
| 819 | 57× | } |
| 820 | |
|
| 821 | |
|
| 822 | |
|
| 823 | 575× | pub async fn audit(pool: &Pool, actor: Option<&Account>, action: &str, target: &str, detail: &str) -> sqlx::Result<()> { |
| 824 | 575× | sqlx::query("INSERT INTO audit (at_ms, actor_id, actor, action, target, detail) VALUES ($1, $2, $3, $4, $5, $6)") |
| 825 | 575× | .bind(now_ms()) |
| 826 | 575× | .bind(actor.map(|a| a.id)) |
| 827 | 575× | .bind(actor.map(|a| a.username.as_str()).unwrap_or("")) |
| 828 | 575× | .bind(action) |
| 829 | 575× | .bind(target) |
| 830 | 575× | .bind(detail) |
| 831 | 575× | .execute(pool) |
| 832 | 575× | .await?; |
| 833 | 575× | Ok(()) |
| 834 | 575× | } |
| 835 | |
|
| 836 | |
|
| 837 | 96× | pub async fn audit_page(pool: &Pool, before: Option<i64>, limit: i64) -> sqlx::Result<Vec<AuditRow>> { |
| 838 | 96× | sqlx::query_as("SELECT id, at_ms, actor, action, target, detail FROM audit WHERE ($1 IS NULL OR id < $2) ORDER BY id DESC LIMIT $3") |
| 839 | 96× | .bind(before) |
| 840 | 96× | .bind(before) |
| 841 | 96× | .bind(limit) |
| 842 | 96× | .fetch_all(pool) |
| 843 | 96× | .await |
| 844 | 96× | } |
| 845 | |
|
| 846 | 16× | pub async fn audit_rows(pool: &Pool, limit: i64) -> sqlx::Result<Vec<AuditRow>> { |
| 847 | 16× | sqlx::query_as("SELECT id, at_ms, actor, action, target, detail FROM audit ORDER BY at_ms DESC, id DESC LIMIT $1") |
| 848 | 16× | .bind(limit) |
| 849 | 16× | .fetch_all(pool) |
| 850 | 16× | .await |
| 851 | 16× | } |
| 852 | |
|
| 853 | |
|
| 854 | |
|
| 855 | |
|
| 856 | 39× | pub async fn visible_repo_ids(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<i64>> { |
| 857 | 39× | sqlx::query_scalar( |
| 858 | 39× | "SELECT r.id FROM repositories r WHERE r.kind = 'repo' AND (r.owner IN (SELECT username FROM accounts WHERE id = $1) \ |
| 859 | 39× | OR r.owner IN (SELECT o.name FROM organisations o JOIN org_members m ON m.org_id = o.id WHERE m.account_id = $2) \ |
| 860 | 39× | OR r.id IN (SELECT repo_id FROM collaborators WHERE account_id = $3 OR team_id IN (SELECT team_id FROM team_members WHERE account_id = $4)))", |
| 861 | 39× | ) |
| 862 | 39× | .bind(account_id) |
| 863 | 39× | .bind(account_id) |
| 864 | 39× | .bind(account_id) |
| 865 | 39× | .bind(account_id) |
| 866 | 39× | .fetch_all(pool) |
| 867 | 39× | .await |
| 868 | 39× | } |
| 869 | |
|
| 870 | |
|
| 871 | |
|
| 872 | |
|
| 873 | | #[derive(Clone, Debug, FromRow)] |
| 874 | | pub struct AccountLink { |
| 875 | | pub id: i64, |
| 876 | | pub account_id: i64, |
| 877 | | pub kind: String, |
| 878 | | pub hash: String, |
| 879 | | pub email: String, |
| 880 | | pub created_by: Option<i64>, |
| 881 | | pub created_ms: i64, |
| 882 | | pub expires_ms: i64, |
| 883 | | pub used_ms: Option<i64>, |
| 884 | | } |
| 885 | |
|
| 886 | |
|
| 887 | 6× | pub async fn create_link( |
| 888 | 6× | pool: &Pool, |
| 889 | 6× | account_id: i64, |
| 890 | 6× | kind: &str, |
| 891 | 6× | email: &str, |
| 892 | 6× | created_by: Option<i64>, |
| 893 | 6× | ttl_ms: i64, |
| 894 | 6× | ) -> sqlx::Result<String> { |
| 895 | 6× | let secret = crate::auth::random_hex(32); |
| 896 | 6× | let now = now_ms(); |
| 897 | 6× | sqlx::query( |
| 898 | 6× | "INSERT INTO account_links (account_id, kind, hash, email, created_by, created_ms, expires_ms) VALUES ($1, $2, $3, $4, $5, $6, $7)", |
| 899 | 6× | ) |
| 900 | 6× | .bind(account_id) |
| 901 | 6× | .bind(kind) |
| 902 | 6× | .bind(crate::auth::token_hash(&secret)) |
| 903 | 6× | .bind(email) |
| 904 | 6× | .bind(created_by) |
| 905 | 6× | .bind(now) |
| 906 | 6× | .bind(now + ttl_ms) |
| 907 | 6× | .execute(pool) |
| 908 | 6× | .await?; |
| 909 | 6× | Ok(secret) |
| 910 | 6× | } |
| 911 | |
|
| 912 | |
|
| 913 | 11× | pub async fn live_link(pool: &Pool, kind: &str, secret: &str) -> sqlx::Result<Option<AccountLink>> { |
| 914 | 11× | sqlx::query_as("SELECT * FROM account_links WHERE kind = $1 AND hash = $2 AND used_ms IS NULL AND expires_ms > $3") |
| 915 | 11× | .bind(kind) |
| 916 | 11× | .bind(crate::auth::token_hash(secret)) |
| 917 | 11× | .bind(now_ms()) |
| 918 | 11× | .fetch_optional(pool) |
| 919 | 11× | .await |
| 920 | 11× | } |
| 921 | |
|
| 922 | |
|
| 923 | 4× | pub async fn use_link(pool: &Pool, id: i64) -> sqlx::Result<bool> { |
| 924 | 4× | let r = sqlx::query("UPDATE account_links SET used_ms = $1 WHERE id = $2 AND used_ms IS NULL") |
| 925 | 4× | .bind(now_ms()) |
| 926 | 4× | .bind(id) |
| 927 | 4× | .execute(pool) |
| 928 | 4× | .await?; |
| 929 | 4× | Ok(r.rows_affected() == 1) |
| 930 | 4× | } |
| 931 | |
|
| 932 | |
|
| 933 | 2× | pub async fn void_links(pool: &Pool, account_id: i64, kind: &str) -> sqlx::Result<()> { |
| 934 | 2× | sqlx::query("UPDATE account_links SET used_ms = $1 WHERE account_id = $2 AND kind = $3 AND used_ms IS NULL") |
| 935 | 2× | .bind(now_ms()) |
| 936 | 2× | .bind(account_id) |
| 937 | 2× | .bind(kind) |
| 938 | 2× | .execute(pool) |
| 939 | 2× | .await?; |
| 940 | 2× | Ok(()) |
| 941 | 2× | } |
| 942 | |
|
| 943 | |
|
| 944 | 7× | pub async fn last_link_ms(pool: &Pool, account_id: i64, kind: &str, email: Option<&str>) -> sqlx::Result<Option<i64>> { |
| 945 | 7× | match email { |
| 946 | 5× | Some(e) => { |
| 947 | 5× | sqlx::query_scalar("SELECT MAX(created_ms) FROM account_links WHERE account_id = $1 AND kind = $2 AND email = $3") |
| 948 | 5× | .bind(account_id) |
| 949 | 5× | .bind(kind) |
| 950 | 5× | .bind(e) |
| 951 | 5× | .fetch_one(pool) |
| 952 | 5× | .await |
| 953 | | } |
| 954 | | None => { |
| 955 | 2× | sqlx::query_scalar("SELECT MAX(created_ms) FROM account_links WHERE account_id = $1 AND kind = $2") |
| 956 | 2× | .bind(account_id) |
| 957 | 2× | .bind(kind) |
| 958 | 2× | .fetch_one(pool) |
| 959 | 2× | .await |
| 960 | | } |
| 961 | | } |
| 962 | 7× | } |
| 963 | |
|
| 964 | |
|
| 965 | 4× | pub async fn links_since(pool: &Pool, account_id: i64, kind: &str, since_ms: i64) -> sqlx::Result<i64> { |
| 966 | 4× | sqlx::query_scalar("SELECT COUNT(*) FROM account_links WHERE account_id = $1 AND kind = $2 AND created_ms >= $3") |
| 967 | 4× | .bind(account_id) |
| 968 | 4× | .bind(kind) |
| 969 | 4× | .bind(since_ms) |
| 970 | 4× | .fetch_one(pool) |
| 971 | 4× | .await |
| 972 | 4× | } |
| 973 | |
|
| 974 | 9× | pub async fn set_email_verified(pool: &Pool, id: i64, verified: bool) -> sqlx::Result<()> { |
| 975 | 9× | sqlx::query("UPDATE accounts SET email_verified = $1 WHERE id = $2").bind(verified).bind(id).execute(pool).await?; |
| 976 | 9× | Ok(()) |
| 977 | 9× | } |
| 978 | |
|
| 979 | |
|
| 980 | 7× | pub async fn account_by_verified_email(pool: &Pool, email: &str) -> sqlx::Result<Option<Account>> { |
| 981 | 7× | let found: Vec<Account> = sqlx::query_as("SELECT * FROM accounts WHERE email_verified = TRUE AND lower(email) = lower($1) LIMIT 2") |
| 982 | 7× | .bind(email) |
| 983 | 7× | .fetch_all(pool) |
| 984 | 7× | .await?; |
| 985 | 7× | Ok(if found.len() == 1 { found.into_iter().next() } else { None }) |
| 986 | 7× | } |
| 987 | |
|
| 988 | |
|
| 989 | 3× | pub async fn set_avatar(pool: &Pool, kind: &str, id: i64, value: &str) -> sqlx::Result<()> { |
| 990 | 3× | let table = if kind == "org" { "organisations" } else { "accounts" }; |
| 991 | 3× | sqlx::query(&format!("UPDATE {table} SET avatar = $1 WHERE id = $2")).bind(value).bind(id).execute(pool).await?; |
| 992 | 3× | Ok(()) |
| 993 | 3× | } |
| 994 | |
|
| 995 | |
|
| 996 | |
|
| 997 | |
|
| 998 | | #[derive(Clone, Debug, FromRow)] |
| 999 | | pub struct AgentGrant { |
| 1000 | | pub id: i64, |
| 1001 | | pub account_id: i64, |
| 1002 | | pub agent: String, |
| 1003 | | pub subject: String, |
| 1004 | | |
| 1005 | | pub grantor: String, |
| 1006 | | pub scope: String, |
| 1007 | | |
| 1008 | | pub signed: Vec<u8>, |
| 1009 | | pub token_hash: String, |
| 1010 | | pub created_ms: i64, |
| 1011 | | pub expires_ms: i64, |
| 1012 | | pub revoked_ms: Option<i64>, |
| 1013 | | pub last_used_ms: Option<i64>, |
| 1014 | | } |
| 1015 | |
|
| 1016 | | impl AgentGrant { |
| 1017 | | |
| 1018 | 26× | pub fn live(&self, now: i64) -> bool { |
| 1019 | 26× | self.revoked_ms.is_none() && now < self.expires_ms |
| 1020 | 26× | } |
| 1021 | | } |
| 1022 | |
|
| 1023 | | pub struct NewAgentGrant<'a> { |
| 1024 | | pub account_id: i64, |
| 1025 | | pub agent: &'a str, |
| 1026 | | pub subject: &'a str, |
| 1027 | | pub grantor: &'a str, |
| 1028 | | pub scope: &'a str, |
| 1029 | | pub signed: &'a [u8], |
| 1030 | | pub token_hash: &'a str, |
| 1031 | | pub expires_ms: i64, |
| 1032 | | } |
| 1033 | |
|
| 1034 | 1× | pub async fn create_agent_grant(pool: &Pool, g: NewAgentGrant<'_>) -> sqlx::Result<i64> { |
| 1035 | 1× | sqlx::query_scalar( |
| 1036 | 1× | "INSERT INTO agent_grants (account_id, agent, subject, grantor, scope, signed, token_hash, created_ms, expires_ms) \ |
| 1037 | 1× | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING id", |
| 1038 | 1× | ) |
| 1039 | 1× | .bind(g.account_id) |
| 1040 | 1× | .bind(g.agent) |
| 1041 | 1× | .bind(g.subject) |
| 1042 | 1× | .bind(g.grantor) |
| 1043 | 1× | .bind(g.scope) |
| 1044 | 1× | .bind(g.signed) |
| 1045 | 1× | .bind(g.token_hash) |
| 1046 | 1× | .bind(now_ms()) |
| 1047 | 1× | .bind(g.expires_ms) |
| 1048 | 1× | .fetch_one(pool) |
| 1049 | 1× | .await |
| 1050 | 1× | } |
| 1051 | |
|
| 1052 | 1× | pub async fn agent_grants_of(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<AgentGrant>> { |
| 1053 | 1× | sqlx::query_as("SELECT * FROM agent_grants WHERE account_id = $1 ORDER BY created_ms DESC, id DESC") |
| 1054 | 1× | .bind(account_id) |
| 1055 | 1× | .fetch_all(pool) |
| 1056 | 1× | .await |
| 1057 | 1× | } |
| 1058 | |
|
| 1059 | 26× | pub async fn agent_grant_by_token(pool: &Pool, token_hash: &str) -> sqlx::Result<Option<AgentGrant>> { |
| 1060 | 26× | sqlx::query_as("SELECT * FROM agent_grants WHERE token_hash = $1").bind(token_hash).fetch_optional(pool).await |
| 1061 | 26× | } |
| 1062 | |
|
| 1063 | 13× | pub async fn touch_agent_grant(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 1064 | 13× | sqlx::query("UPDATE agent_grants SET last_used_ms = $1 WHERE id = $2").bind(now_ms()).bind(id).execute(pool).await?; |
| 1065 | 13× | Ok(()) |
| 1066 | 13× | } |
| 1067 | |
|
| 1068 | |
|
| 1069 | 1× | pub async fn revoke_agent_grant(pool: &Pool, account_id: i64, id: i64) -> sqlx::Result<bool> { |
| 1070 | 1× | let r = sqlx::query("UPDATE agent_grants SET revoked_ms = $1 WHERE id = $2 AND account_id = $3 AND revoked_ms IS NULL") |
| 1071 | 1× | .bind(now_ms()) |
| 1072 | 1× | .bind(id) |
| 1073 | 1× | .bind(account_id) |
| 1074 | 1× | .execute(pool) |
| 1075 | 1× | .await?; |
| 1076 | 1× | Ok(r.rows_affected() == 1) |
| 1077 | 1× | } |