Skip to content

Commit 8a73da2

Browse files
manuthecodergithub-actions[bot]garyhtou
authored
[User Session] Clear metadata on older sessions (#10751)
### Closes #10744 This pull request introduces a new background job to clear outdated user session data and schedules it to run daily. The job ensures sensitive session information is removed for sessions older than one year. Below are the most important changes: ### Addition of a new job: * [`app/jobs/user/clear_old_user_sessions_job.rb`](diffhunk://#diff-19bd9b2978ef88bf9ae1db00fbb346141d92e5d85c4cb816b8949d814f0c4792R1-R21): Added a new job `ClearOldUserSessionsJob` to the `User` namespace. This job iterates through user sessions older than one year and clears sensitive data such as `device_info`, `os_info`, `timezone`, `ip`, and location details (`latitude` and `longitude`). ### Scheduling the job: * [`config/schedule.yml`](diffhunk://#diff-18c4a92c666266e8bead5b6ddb034501022877cdf773c0d337499ed0a7f6d341R175-R179): Scheduled the new `ClearOldUserSessionsJob` to run daily at 3:00 AM with a low priority queue. [Internal discussion](https://hackclub.slack.com/archives/C047Y01MHJQ/p1750259883680629) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Gary Tou <[email protected]>
1 parent f0bdbdb commit 8a73da2

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
class UserSession
4+
class ClearOldUserSessionsJob < ApplicationJob
5+
queue_as :low
6+
7+
def perform
8+
UserSession.expired.where("created_at < ?", 1.year.ago).find_each(&:clear_metadata!)
9+
end
10+
11+
end
12+
13+
end

app/models/user_session.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ def expired?
8181
expiration_at <= Time.now
8282
end
8383

84+
def clear_metadata!
85+
update!(
86+
device_info: nil,
87+
latitude: nil,
88+
longitude: nil,
89+
)
90+
end
91+
8492
private
8593

8694
def user_is_unlocked

config/schedule.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ update_teenager_column_job:
172172
cron: "0 7 * * *" # run every 1 day
173173
class: "User::UpdateTeenagerColumnJob"
174174

175+
clear_old_user_sessions:
176+
cron: "0 3 * * *" # every day at 3:00 AM
177+
class: "UserSession::ClearOldUserSessionsJob"
178+
queue: low
179+
175180
card_grant_expiration_job:
176181
cron: "0 7 * * *" # run every 1 day
177182
class: "CardGrant::ExpirationJob"

0 commit comments

Comments
 (0)