diff --git a/lib/dhcp.rb b/lib/dhcp.rb index db32800..89e27cc 100644 --- a/lib/dhcp.rb +++ b/lib/dhcp.rb @@ -5,7 +5,7 @@ # The second level class is vendor specific Currently we support Microsoft Servers # via a web based gateway to the netsh.exe program and the InfoBlox DHCP server, which # is accessed directly -# The DHCP entries are cached in a Memcache store and are share among all users of the +# The DHCP entries are cached in a Memcache store and are shared among all users of the # system module DHCP # There has been a problem with the gateway transport or the data diff --git a/lib/netdb_manager.rb b/lib/netdb_manager.rb index 747ef94..42ee439 100644 --- a/lib/netdb_manager.rb +++ b/lib/netdb_manager.rb @@ -1,5 +1,5 @@ -# Patch the host with a transactional update facility that we use to hook our update code -require 'netdb_manager/active_record_ext' +# Patch the ActionController with a alias_method_chain that loads our modifications to the models +require 'netdb_manager/action_controller_ext' ActionController::Base.prepend_view_path(File.join(File.dirname(__FILE__), '..', 'app', 'views')) diff --git a/lib/netdb_manager/action_controller_ext.rb b/lib/netdb_manager/action_controller_ext.rb new file mode 100644 index 0000000..cb5d5f5 --- /dev/null +++ b/lib/netdb_manager/action_controller_ext.rb @@ -0,0 +1,20 @@ +module NetdbManager + module ActionControllerExtensions + def self.included(base) #:nodoc: + base.send :include, InstanceMethods + base.class_eval do + alias_method_chain :process, :netdb_support + end + end + + module InstanceMethods + def process_with_netdb_support *args + require_dependency 'netdb_manager/host_ext' + require_dependency 'netdb_manager/user_ext' + require_dependency 'netdb_manager/application_controller_ext' + process_without_netdb_support *args + end + end + end +end +ActionController::Base.send :include, NetdbManager::ActionControllerExtensions diff --git a/lib/netdb_manager/active_record_ext.rb b/lib/netdb_manager/active_record_ext.rb deleted file mode 100644 index 8889d83..0000000 --- a/lib/netdb_manager/active_record_ext.rb +++ /dev/null @@ -1,20 +0,0 @@ -module NetdbManager - module ActiveRecordExtensions - def self.included(base) #:nodoc: - base.send :include, InstanceMethods - base.class_eval { alias_method_chain :before_save, :netdb_support } - end - - module InstanceMethods - def before_save_with_netdb_support - if self.class.to_s == "Host" - require_dependency 'netdb_manager/host_ext' - elsif self.class.to_s == "User" - require_dependency 'netdb_manager/user_ext' - end - before_save_without_netdb_support - end - end - end -end -ActiveRecord::Callbacks.send :include, NetdbManager::ActiveRecordExtensions diff --git a/lib/netdb_manager/application_controller_ext.rb b/lib/netdb_manager/application_controller_ext.rb new file mode 100644 index 0000000..3ae645e --- /dev/null +++ b/lib/netdb_manager/application_controller_ext.rb @@ -0,0 +1,46 @@ +module NetdbManager + module ApplicationControllerExtensions + NET_TTL = 7200 + + def self.included(base) #:nodoc: + base.send :include, InstanceMethods + base.class_eval do + before_filter :initialise_caches + end + end + + module InstanceMethods + def initialise_caches + return true unless @user + return true if SETTINGS[:unattended] and SETTINGS[:unattended] == false + + @dhcp = Rails.cache.fetch(:dhcp, :expires_in => NET_TTL){ + DHCP::Dhcp.new(session) + }.dup # For some reason the object is frozen in this implementation of the cache! + raise RuntimeException, "Unable to create DHCP memcache storage" unless @dhcp + + @user_cache = Rails.cache.fetch("user_cache", :expires_in => NET_TTL){ + {} + }.dup # For some reason the object is frozen in this implementation of the cache! + raise RuntimeException, "Unable to create password cache storage" unless @pass + + # The DHCP instance needs access to the session as some of its DHCPServer implementations need to know about the user + per_user_data = @user_cache[login] + @dhcp.personalise(per_user_data) + true + end + def save_network_data + return true if RAILS_ENV == "test" + if @dhcp + dhcpServer = @dhcp.serverFor subnet.number + if new_record? + setDHCP dhcpServer + end + else + true # No netdb management unless we use memcache + end + end + end + end +end +ApplicaitonController::Base.send :include, NetdbManager::ApplicationControllerExtensions diff --git a/lib/netdb_manager/host_ext.rb b/lib/netdb_manager/host_ext.rb index 8ea494c..6f63a7c 100644 --- a/lib/netdb_manager/host_ext.rb +++ b/lib/netdb_manager/host_ext.rb @@ -21,23 +21,13 @@ def self.included(base) base.extend ClassMethods base.send :include, InstanceMethods - base.send :after_save, :transactional_update + base.class_eval do + after_save :transactional_update + end true end module InstanceMethods - #after_save :transactional_update - def save_network_data - return true if RAILS_ENV == "test" - if @dhcp - dhcpServer = @dhcp.serverFor subnet.number - if new_record? - setDHCP dhcpServer - end - else - true # No netdb management unless we use memcache - end - end def delDHCP dhcpServer status = log_status("Delete a DHCP reservation for #{name}/#{ip}", dhcpServer){ dhcpServer.delReservation self @@ -76,21 +66,6 @@ def log_status message, server, &block end end - def initialise_network_cache - return true unless @user - return true if SETTINGS[:unattended] and SETTINGS[:unattended] == false - - @dhcp = Rails.cache.fetch(:dhcp, :expires_in => NET_TTL){ - DHCP::Dhcp.new(session) - }.dup # For some reason the object is frozen in this implementation of the cache! - raise RuntimeException, "Unable to create DHCP memcache storage" unless @dhcp - - # The DHCP instance needs access to the session as some of its DHCPServer implementations need to know about the user - per_user_dhcp_data = session[:dhcp_data] ||= {:user => @user.login} - @dhcp.personalise(per_user_dhcp_data) - true - end - def transactional_update puts "performing transactional update" Rails.logger.debug "performing transactional update" @@ -107,10 +82,6 @@ def transactional_update end module ClassMethods - def reload_network_data - Rails.cache.clear - head :created - end end end end diff --git a/lib/netdb_manager/user_ext.rb b/lib/netdb_manager/user_ext.rb index 1cfe0cf..ee15c4c 100644 --- a/lib/netdb_manager/user_ext.rb +++ b/lib/netdb_manager/user_ext.rb @@ -1,9 +1,14 @@ module NetdbManager module UserExtensions + NET_TTL = 7200 def self.included(base) #:nodoc: - base.class_eval { alias_method_chain :try_to_login, :networking_support } base.extend ClassMethods base.send :include, InstanceMethods + base.class_eval do + class << self + alias_method_chain :try_to_login, :netdb_support + end + end end module InstanceMethods @@ -11,29 +16,21 @@ module InstanceMethods module ClassMethods - def try_to_login_with_networking_support(login, password) - if user = try_to_login_without_network_support - User.initialise_network_support + def try_to_login_with_netdb_support(login, password) + if user = self.try_to_login_without_netdb_support(login, password) + User.capture_user_data user end user end - def initialise_network_cache - return true unless @user - return true if SETTINGS[:unattended] and SETTINGS[:unattended] == false - - @dhcp = Rails.cache.fetch(:dhcp, :expires_in => NET_TTL){ - DHCP::Dhcp.new(session) - }.dup # For some reason the object is frozen in this implementation of the cache! - raise RuntimeException, "Unable to create DHCP memcache storage" unless @dhcp - - # The DHCP instance needs access to the session as some of its DHCPServer implementations need to know about the user - per_user_dhcp_data = session[:dhcp_data] ||= {:user => @user.login} - @dhcp.personalise(per_user_dhcp_data) + def capture_user_data user + if @user_cache + @user_cache[user.login] = {:password => user.password, :rejected => {}} + Rails.cache.write "user_cache", @user_cache, :expires_in => NET_TTL + end true end end end end -# And yes we need to put this in ActiveRecord and NOT User User.send :include, NetdbManager::UserExtensions