2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
class Sessions::Event::Base
|
|
|
|
|
|
|
|
def initialize(params)
|
2017-10-01 12:25:52 +00:00
|
|
|
params.each do |key, value|
|
2015-12-09 13:09:37 +00:00
|
|
|
instance_variable_set "@#{key}", value
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
|
|
|
@is_web_socket = false
|
|
|
|
return if !@clients[@client_id]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
@is_web_socket = true
|
2018-11-02 17:42:57 +00:00
|
|
|
|
|
|
|
return if !self.class.instance_variable_get(:@database_connection)
|
|
|
|
|
2018-12-03 11:34:56 +00:00
|
|
|
if ActiveRecord::Base.connected?
|
|
|
|
@reused_connection = true
|
|
|
|
else
|
|
|
|
@reused_connection = false
|
|
|
|
ActiveRecord::Base.establish_connection
|
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
end
|
|
|
|
|
2018-11-17 06:34:45 +00:00
|
|
|
def self.inherited(subclass)
|
2020-09-30 09:07:01 +00:00
|
|
|
super
|
2018-11-17 06:34:45 +00:00
|
|
|
subclass.instance_variable_set(:@database_connection, @database_connection)
|
|
|
|
end
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
def websocket_send(recipient_client_id, data)
|
2020-11-05 16:31:00 +00:00
|
|
|
msg = if data.instance_of?(Array)
|
2016-01-15 17:22:57 +00:00
|
|
|
data.to_json
|
2020-11-05 16:31:00 +00:00
|
|
|
else
|
|
|
|
"[#{data.to_json}]"
|
2016-01-15 17:22:57 +00:00
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
if @clients[recipient_client_id]
|
|
|
|
log 'debug', "ws send #{msg}", recipient_client_id
|
|
|
|
@clients[recipient_client_id][:websocket].send(msg)
|
|
|
|
else
|
|
|
|
log 'debug', "fs send #{msg}", recipient_client_id
|
|
|
|
Sessions.send(recipient_client_id, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
def valid_session?
|
|
|
|
if !@session
|
|
|
|
error = {
|
|
|
|
event: 'error',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-03-03 01:51:24 +00:00
|
|
|
state: 'no_session',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if !@session['id']
|
|
|
|
error = {
|
|
|
|
event: 'error',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-03-03 01:51:24 +00:00
|
|
|
state: 'no_session_user_id',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-12-18 03:36:56 +00:00
|
|
|
def current_user_id
|
2016-05-25 07:19:45 +00:00
|
|
|
if !@session
|
|
|
|
error = {
|
2019-11-21 07:47:05 +00:00
|
|
|
event: "#{@event}_error",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-05-25 07:19:45 +00:00
|
|
|
state: 'no_session',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
2017-12-18 03:36:56 +00:00
|
|
|
if @session['id'].blank?
|
2016-05-25 07:19:45 +00:00
|
|
|
error = {
|
2019-11-21 07:47:05 +00:00
|
|
|
event: "#{@event}_error",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-05-25 07:19:45 +00:00
|
|
|
state: 'no_session_user_id',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
2017-12-18 03:36:56 +00:00
|
|
|
@session['id']
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
user_id = current_user_id
|
|
|
|
return if !user_id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-12-18 03:36:56 +00:00
|
|
|
user = User.find_by(id: user_id)
|
2016-05-25 07:19:45 +00:00
|
|
|
if !user
|
|
|
|
error = {
|
|
|
|
event: "#{event}_error",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-05-25 07:19:45 +00:00
|
|
|
state: 'no_such_user',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
2017-12-18 03:36:56 +00:00
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2020-02-13 08:27:36 +00:00
|
|
|
def remote_ip
|
|
|
|
@headers&.fetch('X-Forwarded-For', nil).presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def origin
|
|
|
|
@headers&.fetch('Origin', nil).presence
|
|
|
|
end
|
|
|
|
|
2017-12-18 03:36:56 +00:00
|
|
|
def permission_check(key, event)
|
|
|
|
user = current_user
|
|
|
|
return if !user
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
if !user.permissions?(key)
|
2016-05-25 07:19:45 +00:00
|
|
|
error = {
|
|
|
|
event: "#{event}_error",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-05-25 07:19:45 +00:00
|
|
|
state: 'no_permission',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
def log(level, data, client_id = nil)
|
2020-09-30 09:07:01 +00:00
|
|
|
return if !@options[:v] && level == 'debug'
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
if !client_id
|
|
|
|
client_id = @client_id
|
|
|
|
end
|
|
|
|
# rubocop:disable Rails/Output
|
|
|
|
puts "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
|
2021-07-16 13:44:10 +00:00
|
|
|
# puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
|
2015-12-09 13:09:37 +00:00
|
|
|
# rubocop:enable Rails/Output
|
2021-07-16 13:44:10 +00:00
|
|
|
# Rails.logger.info "#{Time.now.utc.iso8601}:client(#{client_id}) #{data}"
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.database_connection_required
|
|
|
|
@database_connection = true
|
2015-12-09 13:09:37 +00:00
|
|
|
end
|
|
|
|
|
2018-11-02 17:42:57 +00:00
|
|
|
def destroy
|
|
|
|
return if !@is_web_socket
|
|
|
|
return if !self.class.instance_variable_get(:@database_connection)
|
2018-12-03 11:34:56 +00:00
|
|
|
return if @reused_connection
|
2018-11-02 17:42:57 +00:00
|
|
|
|
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
|
|
|
end
|