lowercase all emails when comparing

This commit is contained in:
Marius Monnier 2022-07-04 23:02:51 +02:00
parent 620726cc25
commit 69285c47e3
3 changed files with 6 additions and 5 deletions

@ -131,7 +131,7 @@ impl EmailSignup {
} }
fn delete_existings_by_email(conn: &DbConn, email: &str) -> Result<usize> { fn delete_existings_by_email(conn: &DbConn, email: &str) -> Result<usize> {
let existing_signups = email_signups::table.filter(email_signups::email.eq(email)); let existing_signups = email_signups::table.filter(email_signups::email.to_lowercase().eq(email.to_lowercase()));
diesel::delete(existing_signups) diesel::delete(existing_signups)
.execute(&**conn) .execute(&**conn)
.map_err(Error::from) .map_err(Error::from)

@ -24,7 +24,7 @@ impl PasswordResetRequest {
pub fn insert(conn: &Connection, email: &str) -> Result<String> { pub fn insert(conn: &Connection, email: &str) -> Result<String> {
// first, delete other password reset tokens associated with this email: // first, delete other password reset tokens associated with this email:
let existing_requests = let existing_requests =
password_reset_requests::table.filter(password_reset_requests::email.eq(email)); password_reset_requests::table.filter(password_reset_requests::email.to_lowercase().eq(email.to_lowercase()));
diesel::delete(existing_requests).execute(conn)?; diesel::delete(existing_requests).execute(conn)?;
// now, generate a random token, set the expiry date, // now, generate a random token, set the expiry date,

@ -61,7 +61,7 @@ pub struct User {
pub outbox_url: String, pub outbox_url: String,
pub inbox_url: String, pub inbox_url: String,
pub summary: String, pub summary: String,
pub email: Option<String>, pub email : Option<String>,
pub hashed_password: Option<String>, pub hashed_password: Option<String>,
pub instance_id: i32, pub instance_id: i32,
pub creation_date: NaiveDateTime, pub creation_date: NaiveDateTime,
@ -205,15 +205,16 @@ impl User {
/** /**
* TODO: Should create user record with normalized(lowercased) email * TODO: Should create user record with normalized(lowercased) email
DONE: Store email case-sensitive but always compare them after lowering
*/ */
pub fn email_used(conn: &DbConn, email: &str) -> Result<bool> { pub fn email_used(conn: &DbConn, email: &str) -> Result<bool> {
use diesel::dsl::{exists, select}; use diesel::dsl::{exists, select};
let lower_email = email.to_lowercase();
select(exists( select(exists(
users::table users::table
.filter(users::instance_id.eq(Instance::get_local()?.id)) .filter(users::instance_id.eq(Instance::get_local()?.id))
.filter(users::email.eq(email)) .filter(users::email.to_lowercase().eq(lower_email))
.or_filter(users::email.eq(email.to_ascii_lowercase())),
)) ))
.get_result(&**conn) .get_result(&**conn)
.map_err(Error::from) .map_err(Error::from)