Skip to content

Commit 254eb55

Browse files
committed
allow saving device without updating updated_at
1 parent a3fae81 commit 254eb55

File tree

5 files changed

+51
-48
lines changed

5 files changed

+51
-48
lines changed

src/api/core/accounts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ async fn put_device_token(device_id: DeviceId, data: Json<PushToken>, headers: H
14091409
}
14101410

14111411
device.push_token = Some(token);
1412-
if let Err(e) = device.save(&conn).await {
1412+
if let Err(e) = device.save(true, &conn).await {
14131413
err!(format!("An error occurred while trying to save the device push token: {e}"));
14141414
}
14151415

src/api/identity.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use chrono::{NaiveDateTime, Utc};
1+
use chrono::Utc;
22
use num_traits::FromPrimitive;
33
use rocket::{
44
form::{Form, FromForm},
@@ -147,7 +147,7 @@ async fn _refresh_login(data: ConnectData, conn: &DbConn, ip: &ClientIp) -> Json
147147
}
148148
Ok((mut device, auth_tokens)) => {
149149
// Save to update `device.updated_at` to track usage and toggle new status
150-
device.save(conn).await?;
150+
device.save(true, conn).await?;
151151

152152
let result = json!({
153153
"refresh_token": auth_tokens.refresh_token(),
@@ -267,9 +267,10 @@ async fn _sso_login(
267267
}
268268
Some((mut user, sso_user)) => {
269269
let mut device = get_device(&data, conn, &user).await?;
270-
271-
// Save to update `device.updated_at` to track usage and toggle new status
272-
device.save(conn).await?;
270+
if !device.is_new() {
271+
// Update `device.updated_at` only if it's not a new device
272+
device.save(true, conn).await?;
273+
}
273274

274275
let twofactor_token = twofactor_auth(&mut user, &data, &mut device, ip, client_version, conn).await?;
275276

@@ -317,7 +318,7 @@ async fn _sso_login(
317318
auth_user.expires_in,
318319
)?;
319320

320-
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, &now, conn, ip).await
321+
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, conn, ip).await
321322
}
322323

323324
async fn _password_login(
@@ -434,28 +435,29 @@ async fn _password_login(
434435
}
435436

436437
let mut device = get_device(&data, conn, &user).await?;
437-
438-
// Save to update `device.updated_at` to track usage and toggle new status
439-
device.save(conn).await?;
438+
if !device.is_new() {
439+
// Update `device.updated_at` only if it's not a new device
440+
device.save(true, conn).await?;
441+
}
440442

441443
let twofactor_token = twofactor_auth(&mut user, &data, &mut device, ip, client_version, conn).await?;
442444

443445
let auth_tokens = auth::AuthTokens::new(&device, &user, AuthMethod::Password, data.client_id);
444446

445-
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, &now, conn, ip).await
447+
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, conn, ip).await
446448
}
447449

448450
async fn authenticated_response(
449451
user: &User,
450452
device: &mut Device,
451453
auth_tokens: auth::AuthTokens,
452454
twofactor_token: Option<String>,
453-
now: &NaiveDateTime,
454455
conn: &DbConn,
455456
ip: &ClientIp,
456457
) -> JsonResult {
457458
if CONFIG.mail_enabled() && device.is_new() {
458-
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), now, device).await {
459+
let now = Utc::now().naive_utc();
460+
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &now, device).await {
459461
error!("Error sending new device email: {e:#?}");
460462

461463
if CONFIG.require_device_email() {
@@ -475,7 +477,7 @@ async fn authenticated_response(
475477
}
476478

477479
// Save to update `device.updated_at` to track usage and toggle new status
478-
device.save(conn).await?;
480+
device.save(true, conn).await?;
479481

480482
let master_password_policy = master_password_policy(user, conn).await;
481483

@@ -592,7 +594,7 @@ async fn _user_api_key_login(
592594
let access_claims = auth::LoginJwtClaims::default(&device, &user, &AuthMethod::UserApiKey, data.client_id);
593595

594596
// Save to update `device.updated_at` to track usage and toggle new status
595-
device.save(conn).await?;
597+
device.save(true, conn).await?;
596598

597599
info!("User {} logged in successfully via API key. IP: {}", user.email, ip.ip);
598600

@@ -655,7 +657,12 @@ async fn get_device(data: &ConnectData, conn: &DbConn, user: &User) -> ApiResult
655657
// Find device or create new
656658
match Device::find_by_uuid_and_user(&device_id, &user.uuid, conn).await {
657659
Some(device) => Ok(device),
658-
None => Device::new(device_id, user.uuid.clone(), device_name, device_type, conn).await,
660+
None => {
661+
let mut device = Device::new(device_id, user.uuid.clone(), device_name, device_type);
662+
// save device without updating `device.updated_at`
663+
device.save(false, conn).await?;
664+
Ok(device)
665+
}
659666
}
660667
}
661668

src/api/push.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub async fn register_push_device(device: &mut Device, conn: &DbConn) -> EmptyRe
128128
err!(format!("An error occurred while proceeding registration of a device: {e}"));
129129
}
130130

131-
if let Err(e) = device.save(conn).await {
131+
if let Err(e) = device.save(true, conn).await {
132132
err!(format!("An error occurred while trying to save the (registered) device push uuid: {e}"));
133133
}
134134

src/auth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ pub async fn refresh_tokens(
12231223
};
12241224

12251225
// Save to update `updated_at`.
1226-
device.save(conn).await?;
1226+
device.save(true, conn).await?;
12271227

12281228
let user = match User::find_by_uuid(&device.user_uuid, conn).await {
12291229
None => err!("Impossible to find user"),

src/db/models/device.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ pub struct Device {
3535

3636
/// Local methods
3737
impl Device {
38+
pub fn new(uuid: DeviceId, user_uuid: UserId, name: String, atype: i32) -> Self {
39+
let now = Utc::now().naive_utc();
40+
41+
Self {
42+
uuid,
43+
created_at: now,
44+
updated_at: now,
45+
46+
user_uuid,
47+
name,
48+
atype,
49+
50+
push_uuid: Some(PushId(get_uuid())),
51+
push_token: None,
52+
refresh_token: crypto::encode_random_bytes::<64>(&BASE64URL),
53+
twofactor_remember: None,
54+
}
55+
}
56+
3857
pub fn to_json(&self) -> Value {
3958
json!({
4059
"id": self.uuid,
@@ -110,46 +129,29 @@ impl DeviceWithAuthRequest {
110129
}
111130
use crate::db::DbConn;
112131

113-
use crate::api::{ApiResult, EmptyResult};
132+
use crate::api::EmptyResult;
114133
use crate::error::MapResult;
115134

116135
/// Database methods
117136
impl Device {
118-
pub async fn new(uuid: DeviceId, user_uuid: UserId, name: String, atype: i32, conn: &DbConn) -> ApiResult<Device> {
119-
let now = Utc::now().naive_utc();
120-
121-
let device = Self {
122-
uuid,
123-
created_at: now,
124-
updated_at: now,
125-
126-
user_uuid,
127-
name,
128-
atype,
129-
130-
push_uuid: Some(PushId(get_uuid())),
131-
push_token: None,
132-
refresh_token: crypto::encode_random_bytes::<64>(&BASE64URL),
133-
twofactor_remember: None,
134-
};
135-
136-
device.inner_save(conn).await.map(|()| device)
137-
}
137+
pub async fn save(&mut self, update_time: bool, conn: &DbConn) -> EmptyResult {
138+
if update_time {
139+
self.updated_at = Utc::now().naive_utc();
140+
}
138141

139-
async fn inner_save(&self, conn: &DbConn) -> EmptyResult {
140142
db_run! { conn:
141143
sqlite, mysql {
142144
crate::util::retry(||
143145
diesel::replace_into(devices::table)
144-
.values(self)
146+
.values(&*self)
145147
.execute(conn),
146148
10,
147149
).map_res("Error saving device")
148150
}
149151
postgresql {
150152
crate::util::retry(||
151153
diesel::insert_into(devices::table)
152-
.values(self)
154+
.values(&*self)
153155
.on_conflict((devices::uuid, devices::user_uuid))
154156
.do_update()
155157
.set(self)
@@ -160,12 +162,6 @@ impl Device {
160162
}
161163
}
162164

163-
// Should only be called after user has passed authentication
164-
pub async fn save(&mut self, conn: &DbConn) -> EmptyResult {
165-
self.updated_at = Utc::now().naive_utc();
166-
self.inner_save(conn).await
167-
}
168-
169165
pub async fn delete_all_by_user(user_uuid: &UserId, conn: &DbConn) -> EmptyResult {
170166
db_run! { conn: {
171167
diesel::delete(devices::table.filter(devices::user_uuid.eq(user_uuid)))

0 commit comments

Comments
 (0)