Sign out the account's other sessions when its password changes

Changing the password is how someone shuts out whoever else is signed
in to their account, but every session opened with the old password
stayed valid. Now the others end and their live connections close, as
on sign-out. The session the password was changed from stays signed in.

Changing the password outside a request, as from a console, ends every
session.

From #371. 1.5.2 has no record snapshot or response cache, so the test
that a cached session lookup doesn't outlive the password change is
left out; the rest applies as it is.

(cherry picked from commit c78c3fd7ef)
This commit is contained in:
Jeremy Daer committed 2026-10-19 10:28:55 -07:00
1 parent 9474e70f5a
commit c78f03fda7
4 files changed
+77

No files matched your search

+22
View File
@@ -42,6 +42,28 @@ class UserTest < ActiveSupport::TestCase
end
end
test "changing the password outside a request signs out every session" do
users(:david).sessions.start!(user_agent: "Other device", ip_address: "127.0.0.1")
assert_changes -> { users(:david).sessions.count }, to: 0 do
users(:david).update!(password: "a new secret123456")
end
end
test "changing the password keeps the session it was changed from" do
Current.session = users(:david).sessions.start!(user_agent: "This device", ip_address: "127.0.0.1")
users(:david).update!(password: "a new secret123456")
assert_equal [ Current.session ], users(:david).sessions.reload
end
test "changing other details keeps every session" do
assert_no_changes -> { users(:david).sessions.count } do
users(:david).update!(name: "Dave", email_address: "dave@37signals.com")
end
end
private
def create_new_user
User.create!(name: "User", email_address: "user@example.com", password: "secret123456")