|
1 | 1 | //! All routes related to managing owners of a crate |
2 | 2 |
|
3 | | -use crate::controllers::helpers::authorization::Rights; |
4 | | -use crate::controllers::krate::CratePath; |
5 | | -use crate::models::krate::OwnerRemoveError; |
6 | | -use crate::models::{Crate, Owner, PublicUser, Team, User}; |
| 3 | +use crate::controllers::{helpers::authorization::Rights, krate::CratePath}; |
7 | 4 | use crate::models::{ |
8 | | - CrateOwner, NewCrateOwnerInvitation, NewCrateOwnerInvitationOutcome, NewTeam, |
9 | | - krate::NewOwnerInvite, token::EndpointScope, |
| 5 | + Crate, CrateOwner, NewCrateOwnerInvitation, NewCrateOwnerInvitationOutcome, NewTeam, Owner, |
| 6 | + PublicUser, Team, User, |
| 7 | + krate::{NewOwnerInvite, OwnerRemoveError}, |
| 8 | + token::EndpointScope, |
| 9 | + users_by_username, |
10 | 10 | }; |
| 11 | +use crate::schema::oauth_github; |
11 | 12 | use crate::util::errors::{AppResult, BoxedAppError, bad_request, custom, forbidden}; |
12 | 13 | use crate::views::EncodableOwner; |
13 | | -use crate::worker::jobs::SendEmail; |
14 | | -use crate::{App, app::AppState}; |
15 | | -use crate::{auth::AuthCheck, email::EmailMessage}; |
| 14 | +use crate::{App, app::AppState, auth::AuthCheck, email::EmailMessage, worker::jobs::SendEmail}; |
16 | 15 | use axum::Json; |
17 | 16 | use chrono::Utc; |
18 | 17 | use crates_io_encryption::TokenEncryption; |
19 | 18 | use crates_io_github::{GitHubAuth, GitHubClient, GitHubError}; |
20 | 19 | use crates_io_worker::BackgroundJob; |
21 | 20 | use diesel::prelude::*; |
22 | | -use diesel_async::{AsyncConnection, AsyncPgConnection}; |
| 21 | +use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl}; |
23 | 22 | use http::StatusCode; |
24 | 23 | use http::request::Parts; |
25 | 24 | use minijinja::context; |
@@ -368,7 +367,12 @@ async fn add_owner( |
368 | 367 | add_github_team_owner(github, conn, req_user, krate, team, encryption).await |
369 | 368 | } |
370 | 369 | Login::Unprefixed(login) => { |
371 | | - let user = User::find_by_login(conn, login).await.optional()?; |
| 370 | + let user = users_by_username(login) |
| 371 | + .left_join(oauth_github::table) |
| 372 | + .select(User::as_select()) |
| 373 | + .first(conn) |
| 374 | + .await |
| 375 | + .optional()?; |
372 | 376 | let user = user.ok_or_else(|| { |
373 | 377 | bad_request(format_args!("could not find user with login `{login}`")) |
374 | 378 | })?; |
@@ -543,13 +547,16 @@ pub async fn create_or_update_github_team( |
543 | 547 | let org_id = team.organization.id; |
544 | 548 | let gh_login = &req_user.gh_login; |
545 | 549 |
|
546 | | - let is_team_member = gh_client |
547 | | - .team_membership(org_id, team.id, gh_login, &auth) |
548 | | - .await? |
549 | | - .is_some_and(|m| m.is_active()); |
| 550 | + let can_add_team = if let Some(gh_login) = gh_login { |
| 551 | + let is_team_member = gh_client |
| 552 | + .team_membership(org_id, team.id, gh_login, &auth) |
| 553 | + .await? |
| 554 | + .is_some_and(|m| m.is_active()); |
550 | 555 |
|
551 | | - let can_add_team = |
552 | | - is_team_member || is_gh_org_owner(gh_client, org_id, gh_login, &auth).await?; |
| 556 | + is_team_member || is_gh_org_owner(gh_client, org_id, gh_login, &auth).await? |
| 557 | + } else { |
| 558 | + false |
| 559 | + }; |
553 | 560 |
|
554 | 561 | if !can_add_team { |
555 | 562 | return Err(custom( |
|
0 commit comments