connection.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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. return if session_id.blank?
  13. self.current_user = find_verified_user
  14. self.sid = session_id
  15. end
  16. private
  17. def find_verified_user
  18. private_id = Rack::Session::SessionId.new(session_id).private_id
  19. session = ActiveRecord::SessionStore::Session.find_by(session_id: private_id)
  20. return if !session
  21. User.find_by(id: session.data['user_id'])
  22. end
  23. def session_id
  24. @session_id ||= cookies[Zammad::Application::Initializer::SessionStore::SESSION_KEY]
  25. end
  26. end
  27. end