base.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sessions::Event::Base
  3. def initialize(params)
  4. params.each do |key, value|
  5. instance_variable_set :"@#{key}", value
  6. end
  7. @is_web_socket = false
  8. return if !@clients[@client_id]
  9. @is_web_socket = true
  10. return if !self.class.instance_variable_get(:@database_connection)
  11. if ActiveRecord::Base.connected?
  12. @reused_connection = true
  13. else
  14. @reused_connection = false
  15. ActiveRecord::Base.establish_connection
  16. end
  17. end
  18. def self.inherited(subclass)
  19. super
  20. subclass.instance_variable_set(:@database_connection, @database_connection)
  21. end
  22. def websocket_send(recipient_client_id, data)
  23. msg = if data.instance_of?(Array)
  24. data.to_json
  25. else
  26. "[#{data.to_json}]"
  27. end
  28. if @clients[recipient_client_id]
  29. log 'debug', "ws send #{msg}", recipient_client_id
  30. @clients[recipient_client_id][:websocket].send(msg)
  31. else
  32. log 'debug', "fs send #{msg}", recipient_client_id
  33. Sessions.send(recipient_client_id, data)
  34. end
  35. end
  36. def valid_session?
  37. if !@session
  38. error = {
  39. event: 'error',
  40. data: {
  41. state: 'no_session',
  42. },
  43. }
  44. Sessions.send(@client_id, error)
  45. return
  46. end
  47. if !@session['id']
  48. error = {
  49. event: 'error',
  50. data: {
  51. state: 'no_session_user_id',
  52. },
  53. }
  54. Sessions.send(@client_id, error)
  55. return
  56. end
  57. true
  58. end
  59. def current_user_id
  60. if !@session
  61. error = {
  62. event: "#{@event}_error",
  63. data: {
  64. state: 'no_session',
  65. },
  66. }
  67. Sessions.send(@client_id, error)
  68. return
  69. end
  70. if @session['id'].blank?
  71. error = {
  72. event: "#{@event}_error",
  73. data: {
  74. state: 'no_session_user_id',
  75. },
  76. }
  77. Sessions.send(@client_id, error)
  78. return
  79. end
  80. @session['id']
  81. end
  82. def current_user
  83. user_id = current_user_id
  84. return if !user_id
  85. user = User.find_by(id: user_id)
  86. if !user
  87. error = {
  88. event: "#{event}_error",
  89. data: {
  90. state: 'no_such_user',
  91. },
  92. }
  93. Sessions.send(@client_id, error)
  94. return
  95. end
  96. user
  97. end
  98. def remote_ip
  99. @headers&.fetch('X-Forwarded-For', nil).presence
  100. end
  101. def origin
  102. @headers&.fetch('Origin', nil).presence
  103. end
  104. def permission_check(key, event)
  105. user = current_user
  106. return if !user
  107. if !user.permissions?(key)
  108. error = {
  109. event: "#{event}_error",
  110. data: {
  111. state: 'no_permission',
  112. },
  113. }
  114. Sessions.send(@client_id, error)
  115. return
  116. end
  117. true
  118. end
  119. def log(level, data, client_id = nil)
  120. return if !@options[:v] && level == 'debug'
  121. if !client_id
  122. client_id = @client_id
  123. end
  124. # rubocop:disable Rails/Output
  125. puts "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
  126. # puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
  127. # rubocop:enable Rails/Output
  128. # Rails.logger.info "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
  129. end
  130. def self.database_connection_required
  131. @database_connection = true
  132. end
  133. def destroy
  134. return if !@is_web_socket
  135. return if !self.class.instance_variable_get(:@database_connection)
  136. return if @reused_connection
  137. ActiveRecord::Base.remove_connection
  138. end
  139. end