connection.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module ApplicationCable
  3. class Connection < ActionCable::Connection::Base
  4. identified_by :current_user
  5. identified_by :sid
  6. # current_user is stored in the context of GraphQL which is persistent
  7. # for the scope of a subscription and cannot be changed from within
  8. # other subscriptions.
  9. # Therefore, on login/logout, a new web socket connection must be made to
  10. # reflect the changes within GraphQL.
  11. def connect
  12. find_verified_user
  13. UserInfo.current_user_id = current_user&.id
  14. end
  15. private
  16. def find_verified_user
  17. session_id = cookies[Zammad::Application::Initializer::SessionStore::SESSION_KEY]
  18. return if session_id.blank?
  19. private_id = Rack::Session::SessionId.new(session_id).private_id
  20. return if private_id.blank?
  21. session = ActiveRecord::SessionStore::Session.find_by(session_id: private_id)
  22. return if session.blank?
  23. self.current_user = User.find_by(id: session.data['user_id'])
  24. self.sid = session_id
  25. end
  26. end
  27. end