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

View File

@ -131,7 +131,7 @@ impl EmailSignup {
}
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)
.execute(&**conn)
.map_err(Error::from)

View File

@ -24,7 +24,7 @@ impl PasswordResetRequest {
pub fn insert(conn: &Connection, email: &str) -> Result<String> {
// first, delete other password reset tokens associated with this email:
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)?;
// now, generate a random token, set the expiry date,

View File

@ -61,7 +61,7 @@ pub struct User {
pub outbox_url: String,
pub inbox_url: String,
pub summary: String,
pub email: Option<String>,
pub email : Option<String>,
pub hashed_password: Option<String>,
pub instance_id: i32,
pub creation_date: NaiveDateTime,
@ -205,15 +205,16 @@ impl User {
/**
* 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> {
use diesel::dsl::{exists, select};
let lower_email = email.to_lowercase();
select(exists(
users::table
.filter(users::instance_id.eq(Instance::get_local()?.id))
.filter(users::email.eq(email))
.or_filter(users::email.eq(email.to_ascii_lowercase())),
.filter(users::email.to_lowercase().eq(lower_email))
))
.get_result(&**conn)
.map_err(Error::from)