Skip to content

Commit ecdcb03

Browse files
authored
Authenticate the action cable connection too (rails#53444)
* Authenticate the action cable connection too * Skip generating the connection class if we dont have action cable * Not using assigned instance * Remove the right variable!
1 parent ac98964 commit ecdcb03

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

railties/lib/rails/generators/rails/authentication/authentication_generator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def create_authentication_files
1919
template "app/controllers/concerns/authentication.rb"
2020
template "app/controllers/passwords_controller.rb"
2121

22+
template "app/channels/application_cable/connection.rb" unless options.skip_action_cable?
23+
2224
template "app/mailers/passwords_mailer.rb"
2325

2426
template "app/views/passwords_mailer/reset.html.erb"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module ApplicationCable
2+
class Connection < ActionCable::Connection::Base
3+
identified_by :current_user
4+
5+
def connect
6+
set_current_user || reject_unauthorized_connection
7+
end
8+
9+
private
10+
def set_current_user
11+
if session = Session.find_by(id: cookies.signed[:session_id])
12+
self.current_user = session.user
13+
end
14+
end
15+
end
16+
end
17+

railties/test/generators/authentication_generator_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def test_authentication_generator
3030
assert_file "app/controllers/sessions_controller.rb"
3131
assert_file "app/controllers/concerns/authentication.rb"
3232
assert_file "app/views/sessions/new.html.erb"
33+
assert_file "app/channels/application_cable/connection.rb"
3334

3435
assert_file "app/controllers/application_controller.rb" do |content|
3536
class_line, includes_line = content.lines.first(2)
@@ -106,6 +107,14 @@ def test_model_test_is_skipped_if_test_framework_is_given
106107
assert_no_file "test/models/user_test.rb"
107108
end
108109

110+
def test_connection_class_skipped_without_action_cable
111+
generator([destination_root], skip_action_cable: true)
112+
113+
run_generator_instance
114+
115+
assert_no_file "app/channels/application_cable/connection.rb"
116+
end
117+
109118
private
110119
def run_generator_instance
111120
commands = []

0 commit comments

Comments
 (0)