Browse Source

use twofactor_incomplete table

pull/6495/head
Stefan Melmuk 1 day ago
parent
commit
c5fe9a134f
No known key found for this signature in database GPG Key ID: 817020C608FE9C09
  1. 2
      src/api/core/two_factor/email.rs
  2. 22
      src/db/models/user.rs

2
src/api/core/two_factor/email.rs

@ -64,7 +64,7 @@ async fn send_email_login(data: Json<SendEmailLoginData>, conn: DbConn) -> Empty
user user
} else { } else {
// SSO login only sends device id, so we get the user by the most recently used device // SSO login only sends device id, so we get the user by the most recently used device
let Some(user) = User::find_by_device(&data.device_identifier, &conn).await else { let Some(user) = User::find_by_device_for_email2fa(&data.device_identifier, &conn).await else {
err!("Username or password is incorrect. Try again.") err!("Username or password is incorrect. Try again.")
}; };

22
src/db/models/user.rs

@ -1,4 +1,4 @@
use crate::db::schema::{devices, invitations, sso_users, users}; use crate::db::schema::{invitations, sso_users, twofactor_incomplete, users};
use chrono::{NaiveDateTime, TimeDelta, Utc}; use chrono::{NaiveDateTime, TimeDelta, Utc};
use derive_more::{AsRef, Deref, Display, From}; use derive_more::{AsRef, Deref, Display, From};
use diesel::prelude::*; use diesel::prelude::*;
@ -386,16 +386,18 @@ impl User {
}} }}
} }
pub async fn find_by_device(device_uuid: &DeviceId, conn: &DbConn) -> Option<Self> { pub async fn find_by_device_for_email2fa(device_uuid: &DeviceId, conn: &DbConn) -> Option<Self> {
db_run! { conn: { if let Some(user_uuid) = db_run! ( conn: {
users::table twofactor_incomplete::table
.inner_join(devices::table.on(devices::user_uuid.eq(users::uuid))) .filter(twofactor_incomplete::device_uuid.eq(device_uuid))
.filter(devices::uuid.eq(device_uuid)) .order_by(twofactor_incomplete::login_time.desc())
.select(users::all_columns) .select(twofactor_incomplete::user_uuid)
.order_by(devices::updated_at.desc()) .first::<UserId>(conn)
.first::<Self>(conn)
.ok() .ok()
}} }) {
return Self::find_by_uuid(&user_uuid, conn).await;
}
None
} }
pub async fn get_all(conn: &DbConn) -> Vec<(Self, Option<SsoUser>)> { pub async fn get_all(conn: &DbConn) -> Vec<(Self, Option<SsoUser>)> {

Loading…
Cancel
Save