Add EppSession for custom session management

This commit is contained in:
Martin Lensment 2014-06-27 13:39:16 +03:00
parent 3a57126e69
commit e9f23ba348
9 changed files with 92 additions and 2 deletions

View file

@ -3,6 +3,7 @@ module Epp::Common
included do
protect_from_forgery with: :null_session
helper_method :current_epp_user
end
def proxy
@ -12,4 +13,12 @@ module Epp::Common
def parsed_frame
Nokogiri::XML(params[:frame]).remove_namespaces!
end
def epp_session
EppSession.find_or_initialize_by(session_id: cookies['session'])
end
def current_epp_user
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
end
end

View file

@ -11,6 +11,7 @@ class Epp::SessionsController < ApplicationController
@epp_user = EppUser.find_by(login_params)
if @epp_user.try(:active)
epp_session[:epp_user_id] = @epp_user.id
render 'login_success'
else
response.headers['X-EPP-Returncode'] = '2200'
@ -19,6 +20,7 @@ class Epp::SessionsController < ApplicationController
end
def logout
epp_session[:epp_user_id] = nil
response.headers['X-EPP-Returncode'] = '1500'
render 'logout'
end

View file

@ -8,7 +8,7 @@ module Epp::DomainsHelper
cp = command_params
{
name: cp[:name],
registrar: nil, #well come from current_epp_user
registrar_id: current_epp_user.registrar.try(:id),
registered_at: Time.now,
valid_from: Date.today,
valid_to: Date.today + cp[:period].to_i.years,

31
app/models/epp_session.rb Normal file
View file

@ -0,0 +1,31 @@
class EppSession < ActiveRecord::Base
before_save :marshal_data!
def data
@data ||= self.class.unmarshal(read_attribute(:data)) || {}
end
def [](key)
data[key.to_sym]
end
def []=(key, value)
data[key.to_sym] = value
save!
end
def marshal_data!
self.data = self.class.marshal(data)
end
class << self
def marshal(data)
::Base64.encode64(Marshal.dump(data)) if data
end
def unmarshal(data)
return data unless data.is_a? String
Marshal.load(::Base64.decode64(data)) if data
end
end
end