Skip to content

Commit ed303c7

Browse files
committed
Merge branch 'sg-dont-require-emails' into invalid_emails_from_github
2 parents 7389454 + 604ac34 commit ed303c7

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/controllers/user/me.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::controllers::prelude::*;
33
use crate::controllers::helpers::Paginate;
44
use crate::email;
55
use crate::util::bad_request;
6+
use crate::util::errors::CargoError;
67

78
use crate::models::{Email, Follow, NewEmail, User, Version};
89
use crate::schema::{crates, emails, follows, users, versions};
@@ -127,7 +128,7 @@ pub fn update_user(req: &mut dyn Request) -> CargoResult<Response> {
127128
return Err(human("empty email rejected"));
128129
}
129130

130-
conn.transaction(|| {
131+
conn.transaction::<_, Box<dyn CargoError>, _>(|| {
131132
update(users.filter(gh_login.eq(&user.gh_login)))
132133
.set(email.eq(user_email))
133134
.execute(&*conn)?;
@@ -146,8 +147,9 @@ pub fn update_user(req: &mut dyn Request) -> CargoResult<Response> {
146147
.get_result::<String>(&*conn)
147148
.map_err(|_| human("Error in creating token"))?;
148149

149-
crate::email::send_user_confirm_email(user_email, &user.gh_login, &token)
150-
.map_err(|_| bad_request("Email could not be sent"))
150+
crate::email::send_user_confirm_email(user_email, &user.gh_login, &token);
151+
152+
Ok(())
151153
})?;
152154

153155
#[derive(Serialize)]
@@ -199,7 +201,7 @@ pub fn regenerate_token_and_send(req: &mut dyn Request) -> CargoResult<Response>
199201
.get_result::<Email>(&*conn)
200202
.map_err(|_| bad_request("Email could not be found"))?;
201203

202-
email::send_user_confirm_email(&email.email, &user.gh_login, &email.token)
204+
email::try_send_user_confirm_email(&email.email, &user.gh_login, &email.token)
203205
.map_err(|_| bad_request("Error in sending email"))
204206
})?;
205207

src/email.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,22 @@ fn build_email(
5757
Ok(email.into())
5858
}
5959

60-
pub fn send_user_confirm_email(email: &str, user_name: &str, token: &str) -> CargoResult<()> {
60+
/// Attempts to send a confirmation email. Swallows all errors.
61+
///
62+
/// This function swallows any errors that occur while attempting to send the email. Some users
63+
/// have an invalid email set in their GitHub profile, and we should let them sign in even though
64+
/// we're trying to silently use their invalid address during signup and can't send them an email.
65+
/// Use `try_send_user_confirm_email` when the user is directly trying to set their email.
66+
pub fn send_user_confirm_email(email: &str, user_name: &str, token: &str) {
67+
let _ = try_send_user_confirm_email(email, user_name, token);
68+
}
69+
70+
/// Attempts to send a confirmation email and returns errors.
71+
///
72+
/// For use in cases where we want to fail if an email is bad because the user is directly trying
73+
/// to set their email correctly, as opposed to us silently trying to use the email from their
74+
/// GitHub profile during signup.
75+
pub fn try_send_user_confirm_email(email: &str, user_name: &str, token: &str) -> CargoResult<()> {
6176
// Create a URL with token string as path to send to user
6277
// If user clicks on path, look email/user up in database,
6378
// make sure tokens match

src/models/user.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ impl<'a> NewUser<'a> {
5858
use diesel::insert_into;
5959
use diesel::pg::upsert::excluded;
6060
use diesel::sql_types::Integer;
61-
use diesel::NotFound;
6261

6362
conn.transaction(|| {
6463
let user = insert_into(users)
@@ -96,8 +95,7 @@ impl<'a> NewUser<'a> {
9695
.optional()?;
9796

9897
if let Some(token) = token {
99-
crate::email::send_user_confirm_email(user_email, &user.gh_login, &token)
100-
.map_err(|_| NotFound)?;
98+
crate::email::send_user_confirm_email(user_email, &user.gh_login, &token);
10199
}
102100
}
103101

0 commit comments

Comments
 (0)