From ed27e8f8fadd920f9709d5790f7a80600ae985ad Mon Sep 17 00:00:00 2001 From: juandebravo Date: Tue, 29 Nov 2011 19:09:13 -0500 Subject: [PATCH 1/8] deleted unrequired file --- lib/adhearsion/ldap.rb | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 lib/adhearsion/ldap.rb diff --git a/lib/adhearsion/ldap.rb b/lib/adhearsion/ldap.rb deleted file mode 100644 index 29664c5..0000000 --- a/lib/adhearsion/ldap.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Adhearsion - module Ldap - class << self - end - end -end From ee320ca6ac95d1dd1620a6bc5da5769c0076328a Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Wed, 30 Nov 2011 14:29:24 -0500 Subject: [PATCH 2/8] Rename to adhearsion-ldap --- README.md | 12 +-- ahn-ldap.gemspec => adhearsion-ldap.gemspec | 8 +- lib/adhearsion-ldap.rb | 1 + lib/{ahn_ldap.rb => adhearsion/ldap.rb} | 6 +- lib/adhearsion/ldap/plugin.rb | 37 ++++++++ lib/adhearsion/ldap/plugin/service.rb | 66 ++++++++++++++ lib/adhearsion/ldap/version.rb | 5 ++ lib/ahn_ldap/plugin.rb | 36 -------- lib/ahn_ldap/plugin/service.rb | 64 ------------- lib/ahn_ldap/version.rb | 3 - .../ldap}/plugin/service_spec.rb | 20 ++--- spec/adhearsion/ldap/plugin_spec.rb | 89 +++++++++++++++++++ .../ldap_spec.rb} | 8 +- spec/ahn_ldap/plugin_spec.rb | 89 ------------------- spec/spec_helper.rb | 20 ++--- 15 files changed, 235 insertions(+), 229 deletions(-) rename ahn-ldap.gemspec => adhearsion-ldap.gemspec (85%) create mode 100644 lib/adhearsion-ldap.rb rename lib/{ahn_ldap.rb => adhearsion/ldap.rb} (73%) create mode 100644 lib/adhearsion/ldap/plugin.rb create mode 100644 lib/adhearsion/ldap/plugin/service.rb create mode 100644 lib/adhearsion/ldap/version.rb delete mode 100644 lib/ahn_ldap/plugin.rb delete mode 100644 lib/ahn_ldap/plugin/service.rb delete mode 100644 lib/ahn_ldap/version.rb rename spec/{ahn_ldap => adhearsion/ldap}/plugin/service_spec.rb (60%) create mode 100644 spec/adhearsion/ldap/plugin_spec.rb rename spec/{ahn_ldap_spec.rb => adhearsion/ldap_spec.rb} (61%) delete mode 100644 spec/ahn_ldap/plugin_spec.rb diff --git a/README.md b/README.md index 121a80e..c8c6f6b 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,12 @@ In your Adhearsion app configuration file, add the following values: ```ruby Adhearsion.config do |config| - config.ahn_ldap.host = "valid-host" - config.ahn_ldap.port = "valid-port".to_i # 389 by default - config.ahn_ldap.base = "valid-ldap-base" - config.ahn_ldap.bind_dn = "valid-ldap-binding" - config.ahn_ldap.allallow_anonymous = true # false - config.ahn_ldap.try_sasl = true # false + config.adhearsion_ldap.host = "valid-host" + config.adhearsion_ldap.port = "valid-port".to_i # 389 by default + config.adhearsion_ldap.base = "valid-ldap-base" + config.adhearsion_ldap.bind_dn = "valid-ldap-binding" + config.adhearsion_ldap.allallow_anonymous = true # false + config.adhearsion_ldap.try_sasl = true # false end ``` diff --git a/ahn-ldap.gemspec b/adhearsion-ldap.gemspec similarity index 85% rename from ahn-ldap.gemspec rename to adhearsion-ldap.gemspec index ef91d99..ca78577 100644 --- a/ahn-ldap.gemspec +++ b/adhearsion-ldap.gemspec @@ -1,17 +1,17 @@ # -*- encoding: utf-8 -*- $:.push File.expand_path("../lib", __FILE__) -require "ahn_ldap/version" +require "adhearsion/ldap/version" Gem::Specification.new do |s| - s.name = "ahn-ldap" - s.version = AhnLDAP::VERSION + s.name = "adhearsion-ldap" + s.version = Adhearsion::LDAP::VERSION s.authors = ["Taylor Carpenter", "Juan de Bravo", "Ben Langfeld"] s.email = ["tcarpenter@mojolingo.com", "juandebravo@gmail.com", "ben@langfeld.me"] s.homepage = "http://adhearsion.com" s.summary = %q{LDAP configuration for Adhearsion} s.description = %q{An Adhearsion Plugin providing LDAP configurability} - s.rubyforge_project = "ahn-ldap" + s.rubyforge_project = "adhearsion-ldap" s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") diff --git a/lib/adhearsion-ldap.rb b/lib/adhearsion-ldap.rb new file mode 100644 index 0000000..525d854 --- /dev/null +++ b/lib/adhearsion-ldap.rb @@ -0,0 +1 @@ +require 'adhearsion/ldap' diff --git a/lib/ahn_ldap.rb b/lib/adhearsion/ldap.rb similarity index 73% rename from lib/ahn_ldap.rb rename to lib/adhearsion/ldap.rb index 48d1356..e68251d 100644 --- a/lib/ahn_ldap.rb +++ b/lib/adhearsion/ldap.rb @@ -9,9 +9,9 @@ abort "" end -require "ahn_ldap/version" -require "ahn_ldap/plugin" +require "adhearsion/ldap/version" +require "adhearsion/ldap/plugin" -module AhnLDAP +module Adhearsion::LDAP end diff --git a/lib/adhearsion/ldap/plugin.rb b/lib/adhearsion/ldap/plugin.rb new file mode 100644 index 0000000..0ca362b --- /dev/null +++ b/lib/adhearsion/ldap/plugin.rb @@ -0,0 +1,37 @@ +module Adhearsion + module LDAP + + ## + # Adhearsion Plugin that defines the LDAP configuration options + # and includes a hook to start the LDAP service in Adhearsion initialization process + class Plugin < Adhearsion::Plugin + extend ActiveSupport::Autoload + + autoload :Service + + # Default configuration for LDAP connection. + # Configure an LDAP connection using ActiveLdap. See ActiveLdap::Base.establish_connect + # for further information about the appropriate settings here. + config :adhearsion_ldap do + host nil , :desc => "LDAP server host" + port 389 , :desc => "LDAP server port" + base "" , :desc => <<-__ + LDAP tree that must be used in the connection + __ + bind_dn "" , :desc => <<-__ + Specific domain name that identifies the user + __ + password "" , :desc => "Password credentials" + allow_anonymous false, :desc => "valid values: true | false (default)" + try_sasl false, :desc => "valid values: true | false (default)" + end + + # Include the LDAP service in plugins initialization process + init :adhearsion_ldap do + Service.start + end + + end + + end +end diff --git a/lib/adhearsion/ldap/plugin/service.rb b/lib/adhearsion/ldap/plugin/service.rb new file mode 100644 index 0000000..e37c2dc --- /dev/null +++ b/lib/adhearsion/ldap/plugin/service.rb @@ -0,0 +1,66 @@ +module Adhearsion + module LDAP + class Plugin + class Service + + class << self + + ## + # Start LDAP connection + def start + raise "Must supply a host argument to the LDAP configuration" if (config.host.nil? || config.host.empty?) + raise "Must supply a valid port to the LDAP configuration" unless config.port.is_a? Integer + + require_models + establish_connection config.host, + config.port, + config.base, + config.bind_dn, + config.password, + config.allow_anonymous, + config.try_sasl + end + + # TODO: It appears that ActiveLdap does not have a connection validation + # or reconnection routine. + #def create_call_hook_for_connection_cleanup + # Events.register_callback([:asterisk, :before_call]) do + # ActiveLdap::Base.verify_active_connections! + # end + #end + + ## + # Release LDAP connection + def stop + ActiveLdap::Base.remove_connection + end + + private + + def require_models + Adhearsion.config.files_from_setting("paths", "models").each do |model| + load model + end + end + + ## + # Open LDAP connection + def establish_connection host, port, base, bind_dn, password, allow_anonymous, try_sasl + ActiveLdap::Base.setup_connection :host => host, + :port => port, + :base => base, + :logger => logger, + :bind_dn => bind_dn, + :password => password, + :allow_anonymous => allow_anonymous, + :try_sasl => try_sasl + end + + def config + @config ||= Adhearsion.config[:adhearsion_ldap] + end + end + end # Service + end # Plugin + end # LDAP +end # Adhearsion diff --git a/lib/adhearsion/ldap/version.rb b/lib/adhearsion/ldap/version.rb new file mode 100644 index 0000000..d5c0423 --- /dev/null +++ b/lib/adhearsion/ldap/version.rb @@ -0,0 +1,5 @@ +module Adhearsion + module LDAP + VERSION = "0.1.0" + end +end diff --git a/lib/ahn_ldap/plugin.rb b/lib/ahn_ldap/plugin.rb deleted file mode 100644 index c660758..0000000 --- a/lib/ahn_ldap/plugin.rb +++ /dev/null @@ -1,36 +0,0 @@ - -module AhnLDAP - - ## - # Adhearsion Plugin that defines the LDAP configuration options - # and includes a hook to start the LDAP service in Adhearsion initialization process - class Plugin < Adhearsion::Plugin - extend ActiveSupport::Autoload - - autoload :Service - - # Default configuration for LDAP connection. - # Configure an LDAP connection using ActiveLdap. See ActiveLdap::Base.establish_connect - # for further information about the appropriate settings here. - config :ahn_ldap do - host nil , :desc => "LDAP server host" - port 389 , :desc => "LDAP server port" - base "" , :desc => <<-__ - LDAP tree that must be used in the connection - __ - bind_dn "" , :desc => <<-__ - Specific domain name that identifies the user - __ - password "" , :desc => "Password credentials" - allow_anonymous false, :desc => "valid values: true | false (default)" - try_sasl false, :desc => "valid values: true | false (default)" - end - - # Include the LDAP service in plugins initialization process - init :ahn_ldap do - Service.start - end - - end - -end \ No newline at end of file diff --git a/lib/ahn_ldap/plugin/service.rb b/lib/ahn_ldap/plugin/service.rb deleted file mode 100644 index abfd0c6..0000000 --- a/lib/ahn_ldap/plugin/service.rb +++ /dev/null @@ -1,64 +0,0 @@ -module AhnLDAP - class Plugin - class Service - - class << self - - ## - # Start LDAP connection - def start - raise "Must supply a host argument to the LDAP configuration" if (config.host.nil? || config.host.empty?) - raise "Must supply a valid port to the LDAP configuration" unless config.port.is_a? Integer - - require_models - establish_connection config.host, - config.port, - config.base, - config.bind_dn, - config.password, - config.allow_anonymous, - config.try_sasl - end - - # TODO: It appears that ActiveLdap does not have a connection validation - # or reconnection routine. - #def create_call_hook_for_connection_cleanup - # Events.register_callback([:asterisk, :before_call]) do - # ActiveLdap::Base.verify_active_connections! - # end - #end - - ## - # Release LDAP connection - def stop - ActiveLdap::Base.remove_connection - end - - private - - def require_models - Adhearsion.config.files_from_setting("paths", "models").each do |model| - load model - end - end - - ## - # Open LDAP connection - def establish_connection host, port, base, bind_dn, password, allow_anonymous, try_sasl - ActiveLdap::Base.setup_connection :host => host, - :port => port, - :base => base, - :logger => logger, - :bind_dn => bind_dn, - :password => password, - :allow_anonymous => allow_anonymous, - :try_sasl => try_sasl - end - - def config - @config ||= Adhearsion.config[:ahn_ldap] - end - end - end # Service - end # Plugin -end # AhnLDAP \ No newline at end of file diff --git a/lib/ahn_ldap/version.rb b/lib/ahn_ldap/version.rb deleted file mode 100644 index 5676716..0000000 --- a/lib/ahn_ldap/version.rb +++ /dev/null @@ -1,3 +0,0 @@ -module AhnLDAP - VERSION = "0.1.0" -end diff --git a/spec/ahn_ldap/plugin/service_spec.rb b/spec/adhearsion/ldap/plugin/service_spec.rb similarity index 60% rename from spec/ahn_ldap/plugin/service_spec.rb rename to spec/adhearsion/ldap/plugin/service_spec.rb index 361afe3..2023094 100644 --- a/spec/ahn_ldap/plugin/service_spec.rb +++ b/spec/adhearsion/ldap/plugin/service_spec.rb @@ -1,34 +1,34 @@ require 'spec_helper' -describe AhnLDAP::Plugin::Service do +describe Adhearsion::LDAP::Plugin::Service do describe "while initializing" do after do - reset_ahn_ldap_config + reset_adhearsion_ldap_config end - + it "should raise an exception when no host has been configured" do lambda {Adhearsion::Plugin.load}.should raise_error "Must supply a host argument to the LDAP configuration" end it "should raise an exception when an invalid port has been configured" do - Adhearsion.config[:ahn_ldap].host = "localhost" - Adhearsion.config[:ahn_ldap].port = "389" + Adhearsion.config[:adhearsion_ldap].host = "localhost" + Adhearsion.config[:adhearsion_ldap].port = "389" lambda {Adhearsion::Plugin.load}.should raise_error "Must supply a valid port to the LDAP configuration" end end - describe "when starting the LDAP connection" do + describe "when starting the LDAP connection" do before do - Adhearsion.config[:ahn_ldap].host = "localhost" + Adhearsion.config[:adhearsion_ldap].host = "localhost" end after do - reset_ahn_ldap_config + reset_adhearsion_ldap_config end - + it "should call Connection.start method with the valid parameters" do - AhnLDAP::Plugin::Service.should_receive(:establish_connection).with("localhost", 389, "", "", "", false, false).and_return true + Adhearsion::LDAP::Plugin::Service.should_receive(:establish_connection).with("localhost", 389, "", "", "", false, false).and_return true Adhearsion.config.should_receive(:files_from_setting).with("paths", "models").and_return [] Adhearsion::Plugin.load end diff --git a/spec/adhearsion/ldap/plugin_spec.rb b/spec/adhearsion/ldap/plugin_spec.rb new file mode 100644 index 0000000..5ca98e2 --- /dev/null +++ b/spec/adhearsion/ldap/plugin_spec.rb @@ -0,0 +1,89 @@ +require 'spec_helper' + +describe Adhearsion::LDAP::Plugin do + + describe "while accessing the plugin configuration" do + + it "should retrieve a valid configuration instance" do + Adhearsion.config.adhearsion_ldap.should be_instance_of Loquacious::Configuration + end + + it "should configure properly the host" do + Adhearsion.config[:adhearsion_ldap].host.should be_nil + end + + it "should configure properly the port" do + Adhearsion.config[:adhearsion_ldap].port.should == 389 + end + + it "should configure properly the base" do + Adhearsion.config[:adhearsion_ldap].base.should be_empty + end + + it "should configure properly the bind_dn" do + Adhearsion.config[:adhearsion_ldap].bind_dn.should be_empty + end + + it "should configure properly the password" do + Adhearsion.config[:adhearsion_ldap].password.should be_empty + end + + it "should configure properly the anonymous property" do + Adhearsion.config[:adhearsion_ldap].allow_anonymous.should be_false + end + + it "should configure properly the try_sasl property" do + Adhearsion.config[:adhearsion_ldap].try_sasl.should be_false + end + + end + + describe "while configuring a specific config value" do + after do + reset_adhearsion_ldap_config + end + + it "ovewrites properly the host value" do + Adhearsion.config[:adhearsion_ldap].host = "localhost" + Adhearsion.config[:adhearsion_ldap].host.should == "localhost" + end + + it "ovewrites properly the port value" do + Adhearsion.config[:adhearsion_ldap].port = 489 + Adhearsion.config[:adhearsion_ldap].port.should == 489 + end + + it "ovewrites properly the base value" do + Adhearsion.config[:adhearsion_ldap].base = "ou = value" + Adhearsion.config[:adhearsion_ldap].base.should == "ou = value" + end + + it "ovewrites properly the bind_dn value" do + Adhearsion.config[:adhearsion_ldap].bind_dn = "uid = foo@bar" + Adhearsion.config[:adhearsion_ldap].bind_dn.should == "uid = foo@bar" + end + + it "ovewrites properly the password value" do + Adhearsion.config[:adhearsion_ldap].password = "valid-credentials" + Adhearsion.config[:adhearsion_ldap].password.should == "valid-credentials" + end + + it "ovewrites properly the allow_anonymous value" do + Adhearsion.config[:adhearsion_ldap].allow_anonymous = true + Adhearsion.config[:adhearsion_ldap].allow_anonymous.should be_true + end + + it "ovewrites properly the try_sasl value" do + Adhearsion.config[:adhearsion_ldap].try_sasl = true + Adhearsion.config[:adhearsion_ldap].try_sasl.should be_true + end + + end + + describe "while loading plugins" do + it "should load the init block and start the service" do + Adhearsion::LDAP::Plugin::Service.should_receive(:start).once.and_return true + Adhearsion::Plugin.load + end + end +end \ No newline at end of file diff --git a/spec/ahn_ldap_spec.rb b/spec/adhearsion/ldap_spec.rb similarity index 61% rename from spec/ahn_ldap_spec.rb rename to spec/adhearsion/ldap_spec.rb index 520b212..3a7040e 100644 --- a/spec/ahn_ldap_spec.rb +++ b/spec/adhearsion/ldap_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe AhnLDAP do - - subject { AhnLDAP } - +describe Adhearsion::LDAP do + + subject { Adhearsion::LDAP } + it "should be a module" do subject.should be_kind_of Module end diff --git a/spec/ahn_ldap/plugin_spec.rb b/spec/ahn_ldap/plugin_spec.rb deleted file mode 100644 index 0bcad7c..0000000 --- a/spec/ahn_ldap/plugin_spec.rb +++ /dev/null @@ -1,89 +0,0 @@ -require 'spec_helper' - -describe AhnLDAP::Plugin do - - describe "while accessing the plugin configuration" do - - it "should retrieve a valid configuration instance" do - Adhearsion.config.ahn_ldap.should be_instance_of Loquacious::Configuration - end - - it "should configure properly the host" do - Adhearsion.config[:ahn_ldap].host.should be_nil - end - - it "should configure properly the port" do - Adhearsion.config[:ahn_ldap].port.should == 389 - end - - it "should configure properly the base" do - Adhearsion.config[:ahn_ldap].base.should be_empty - end - - it "should configure properly the bind_dn" do - Adhearsion.config[:ahn_ldap].bind_dn.should be_empty - end - - it "should configure properly the password" do - Adhearsion.config[:ahn_ldap].password.should be_empty - end - - it "should configure properly the anonymous property" do - Adhearsion.config[:ahn_ldap].allow_anonymous.should be_false - end - - it "should configure properly the try_sasl property" do - Adhearsion.config[:ahn_ldap].try_sasl.should be_false - end - - end - - describe "while configuring a specific config value" do - after do - reset_ahn_ldap_config - end - - it "ovewrites properly the host value" do - Adhearsion.config[:ahn_ldap].host = "localhost" - Adhearsion.config[:ahn_ldap].host.should == "localhost" - end - - it "ovewrites properly the port value" do - Adhearsion.config[:ahn_ldap].port = 489 - Adhearsion.config[:ahn_ldap].port.should == 489 - end - - it "ovewrites properly the base value" do - Adhearsion.config[:ahn_ldap].base = "ou = value" - Adhearsion.config[:ahn_ldap].base.should == "ou = value" - end - - it "ovewrites properly the bind_dn value" do - Adhearsion.config[:ahn_ldap].bind_dn = "uid = foo@bar" - Adhearsion.config[:ahn_ldap].bind_dn.should == "uid = foo@bar" - end - - it "ovewrites properly the password value" do - Adhearsion.config[:ahn_ldap].password = "valid-credentials" - Adhearsion.config[:ahn_ldap].password.should == "valid-credentials" - end - - it "ovewrites properly the allow_anonymous value" do - Adhearsion.config[:ahn_ldap].allow_anonymous = true - Adhearsion.config[:ahn_ldap].allow_anonymous.should be_true - end - - it "ovewrites properly the try_sasl value" do - Adhearsion.config[:ahn_ldap].try_sasl = true - Adhearsion.config[:ahn_ldap].try_sasl.should be_true - end - - end - - describe "while loading plugins" do - it "should load the init block and start the service" do - AhnLDAP::Plugin::Service.should_receive(:start).once.and_return true - Adhearsion::Plugin.load - end - end -end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 673ad27..597f1ef 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,11 +1,11 @@ -require 'ahn_ldap' +require 'adhearsion/ldap' -def reset_ahn_ldap_config - Adhearsion.config[:ahn_ldap].host = nil - Adhearsion.config[:ahn_ldap].port = 389 - Adhearsion.config[:ahn_ldap].base = "" - Adhearsion.config[:ahn_ldap].bind_dn = "" - Adhearsion.config[:ahn_ldap].password = "" - Adhearsion.config[:ahn_ldap].allow_anonymous = false - Adhearsion.config[:ahn_ldap].try_sasl = false -end \ No newline at end of file +def reset_adhearsion_ldap_config + Adhearsion.config[:adhearsion_ldap].host = nil + Adhearsion.config[:adhearsion_ldap].port = 389 + Adhearsion.config[:adhearsion_ldap].base = "" + Adhearsion.config[:adhearsion_ldap].bind_dn = "" + Adhearsion.config[:adhearsion_ldap].password = "" + Adhearsion.config[:adhearsion_ldap].allow_anonymous = false + Adhearsion.config[:adhearsion_ldap].try_sasl = false +end From d7974d7d3c31717e0f74c8008e04e1de923d520f Mon Sep 17 00:00:00 2001 From: Ben Klang Date: Fri, 2 Dec 2011 13:25:25 -0500 Subject: [PATCH 3/8] [BUG] Fix model loading and init --- lib/adhearsion/ldap/plugin.rb | 3 ++- lib/adhearsion/ldap/plugin/service.rb | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/adhearsion/ldap/plugin.rb b/lib/adhearsion/ldap/plugin.rb index 0ca362b..a75869a 100644 --- a/lib/adhearsion/ldap/plugin.rb +++ b/lib/adhearsion/ldap/plugin.rb @@ -24,11 +24,12 @@ class Plugin < Adhearsion::Plugin password "" , :desc => "Password credentials" allow_anonymous false, :desc => "valid values: true | false (default)" try_sasl false, :desc => "valid values: true | false (default)" + models "app/ldap_models", :desc => "directory containing ActiveLdap models" end # Include the LDAP service in plugins initialization process init :adhearsion_ldap do - Service.start + Service.new.start end end diff --git a/lib/adhearsion/ldap/plugin/service.rb b/lib/adhearsion/ldap/plugin/service.rb index e37c2dc..2518fd1 100644 --- a/lib/adhearsion/ldap/plugin/service.rb +++ b/lib/adhearsion/ldap/plugin/service.rb @@ -37,11 +37,12 @@ def stop private - def require_models - Adhearsion.config.files_from_setting("paths", "models").each do |model| - load model - end + def require_models + path = File.join(Adhearsion.config.platform.root, Adhearsion.config.adhearsion_ldap.models) + Dir.open(Adhearsion.config.adhearsion_ldap.models).each do |model| + require File.join(path, model) if model =~ /\.rb$/ end + end ## # Open LDAP connection From 46d056dcf11f9b669a977bbf121998014e5d8b65 Mon Sep 17 00:00:00 2001 From: Ben Klang Date: Fri, 2 Dec 2011 13:25:42 -0500 Subject: [PATCH 4/8] [CS] Whitespace --- lib/adhearsion/ldap/plugin.rb | 14 ++--- lib/adhearsion/ldap/plugin/service.rb | 85 +++++++++++++-------------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/lib/adhearsion/ldap/plugin.rb b/lib/adhearsion/ldap/plugin.rb index a75869a..aa998be 100644 --- a/lib/adhearsion/ldap/plugin.rb +++ b/lib/adhearsion/ldap/plugin.rb @@ -13,17 +13,17 @@ class Plugin < Adhearsion::Plugin # Configure an LDAP connection using ActiveLdap. See ActiveLdap::Base.establish_connect # for further information about the appropriate settings here. config :adhearsion_ldap do - host nil , :desc => "LDAP server host" - port 389 , :desc => "LDAP server port" - base "" , :desc => <<-__ + host nil, :desc => "LDAP server host" + port 389, :desc => "LDAP server port" + base "", :desc => <<-__ LDAP tree that must be used in the connection __ - bind_dn "" , :desc => <<-__ + bind_dn "", :desc => <<-__ Specific domain name that identifies the user __ - password "" , :desc => "Password credentials" - allow_anonymous false, :desc => "valid values: true | false (default)" - try_sasl false, :desc => "valid values: true | false (default)" + password "", :desc => "Password credentials" + allow_anonymous false, :desc => "valid values: true | false (default)" + try_sasl false, :desc => "valid values: true | false (default)" models "app/ldap_models", :desc => "directory containing ActiveLdap models" end diff --git a/lib/adhearsion/ldap/plugin/service.rb b/lib/adhearsion/ldap/plugin/service.rb index 2518fd1..402e4ed 100644 --- a/lib/adhearsion/ldap/plugin/service.rb +++ b/lib/adhearsion/ldap/plugin/service.rb @@ -3,39 +3,39 @@ module LDAP class Plugin class Service - class << self + ## + # Start LDAP connection + def start + raise "Must supply a host argument to the LDAP configuration" if (config.host.nil? || config.host.empty?) + raise "Must supply a valid port to the LDAP configuration" unless config.port.is_a? Integer - ## - # Start LDAP connection - def start - raise "Must supply a host argument to the LDAP configuration" if (config.host.nil? || config.host.empty?) - raise "Must supply a valid port to the LDAP configuration" unless config.port.is_a? Integer + require_models + establish_connection config.host, + config.port, + config.base, + config.bind_dn, + config.password, + config.allow_anonymous, + config.try_sasl - require_models - establish_connection config.host, - config.port, - config.base, - config.bind_dn, - config.password, - config.allow_anonymous, - config.try_sasl - end + Events.register_callback(:shutdown) { stop } + end - # TODO: It appears that ActiveLdap does not have a connection validation - # or reconnection routine. - #def create_call_hook_for_connection_cleanup - # Events.register_callback([:asterisk, :before_call]) do - # ActiveLdap::Base.verify_active_connections! - # end - #end + # TODO: It appears that ActiveLdap does not have a connection validation + # or reconnection routine. + #def create_call_hook_for_connection_cleanup + # Events.register_callback([:asterisk, :before_call]) do + # ActiveLdap::Base.verify_active_connections! + # end + #end - ## - # Release LDAP connection - def stop - ActiveLdap::Base.remove_connection - end + ## + # Release LDAP connection + def stop + ActiveLdap::Base.remove_connection + end - private + private def require_models path = File.join(Adhearsion.config.platform.root, Adhearsion.config.adhearsion_ldap.models) @@ -44,22 +44,21 @@ def require_models end end - ## - # Open LDAP connection - def establish_connection host, port, base, bind_dn, password, allow_anonymous, try_sasl - ActiveLdap::Base.setup_connection :host => host, - :port => port, - :base => base, - :logger => logger, - :bind_dn => bind_dn, - :password => password, - :allow_anonymous => allow_anonymous, - :try_sasl => try_sasl - end + ## + # Open LDAP connection + def establish_connection host, port, base, bind_dn, password, allow_anonymous, try_sasl + ActiveLdap::Base.setup_connection :host => host, + :port => port, + :base => base, + :logger => logger, + :bind_dn => bind_dn, + :password => password, + :allow_anonymous => allow_anonymous, + :try_sasl => try_sasl + end - def config - @config ||= Adhearsion.config[:adhearsion_ldap] - end + def config + @config ||= Adhearsion.config[:adhearsion_ldap] end end # Service end # Plugin From abdbcc5a4506925154f2073ddf806278bf528a8d Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Wed, 18 Jan 2012 10:54:54 +0000 Subject: [PATCH 5/8] [UPDATE] Adhearsion dependency --- Gemfile | 2 -- adhearsion-ldap.gemspec | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 0b58ac9..7eef02d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,4 @@ source "http://rubygems.org" -gem 'adhearsion', :git => 'git://github.com/adhearsion/adhearsion.git', :branch => :develop - # Specify your gem's dependencies in ahn-ldap.gemspec gemspec \ No newline at end of file diff --git a/adhearsion-ldap.gemspec b/adhearsion-ldap.gemspec index ca78577..699f141 100644 --- a/adhearsion-ldap.gemspec +++ b/adhearsion-ldap.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - # s.add_runtime_dependency %q, [">= 2.0.0"] + s.add_runtime_dependency %q, [">= 2.0.0.alpha1"] s.add_runtime_dependency %q, [">= 3.0.10"] s.add_runtime_dependency "activeldap" From 9c4eef16e364fc35342235a83e12285cb9133e9c Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Wed, 18 Jan 2012 10:56:14 +0000 Subject: [PATCH 6/8] [BUGFIX] Adhearsion now loads plugins differently --- spec/adhearsion/ldap/plugin/service_spec.rb | 6 +++--- spec/adhearsion/ldap/plugin_spec.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/adhearsion/ldap/plugin/service_spec.rb b/spec/adhearsion/ldap/plugin/service_spec.rb index 2023094..f2b3092 100644 --- a/spec/adhearsion/ldap/plugin/service_spec.rb +++ b/spec/adhearsion/ldap/plugin/service_spec.rb @@ -8,13 +8,13 @@ end it "should raise an exception when no host has been configured" do - lambda {Adhearsion::Plugin.load}.should raise_error "Must supply a host argument to the LDAP configuration" + lambda { Adhearsion::Plugin.init_plugins }.should raise_error "Must supply a host argument to the LDAP configuration" end it "should raise an exception when an invalid port has been configured" do Adhearsion.config[:adhearsion_ldap].host = "localhost" Adhearsion.config[:adhearsion_ldap].port = "389" - lambda {Adhearsion::Plugin.load}.should raise_error "Must supply a valid port to the LDAP configuration" + lambda { Adhearsion::Plugin.init_plugins }.should raise_error "Must supply a valid port to the LDAP configuration" end end @@ -30,7 +30,7 @@ it "should call Connection.start method with the valid parameters" do Adhearsion::LDAP::Plugin::Service.should_receive(:establish_connection).with("localhost", 389, "", "", "", false, false).and_return true Adhearsion.config.should_receive(:files_from_setting).with("paths", "models").and_return [] - Adhearsion::Plugin.load + Adhearsion::Plugin.init_plugins end end diff --git a/spec/adhearsion/ldap/plugin_spec.rb b/spec/adhearsion/ldap/plugin_spec.rb index 5ca98e2..7805654 100644 --- a/spec/adhearsion/ldap/plugin_spec.rb +++ b/spec/adhearsion/ldap/plugin_spec.rb @@ -83,7 +83,7 @@ describe "while loading plugins" do it "should load the init block and start the service" do Adhearsion::LDAP::Plugin::Service.should_receive(:start).once.and_return true - Adhearsion::Plugin.load + Adhearsion::Plugin.init_plugins end end end \ No newline at end of file From c3294a12993d5bdac7196075ba0f4dad6ed161cc Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Wed, 18 Jan 2012 11:03:08 +0000 Subject: [PATCH 7/8] [BUGFIX] Fix a failing test --- lib/adhearsion/ldap/plugin.rb | 2 +- lib/adhearsion/ldap/plugin/service.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/adhearsion/ldap/plugin.rb b/lib/adhearsion/ldap/plugin.rb index aa998be..366b68e 100644 --- a/lib/adhearsion/ldap/plugin.rb +++ b/lib/adhearsion/ldap/plugin.rb @@ -29,7 +29,7 @@ class Plugin < Adhearsion::Plugin # Include the LDAP service in plugins initialization process init :adhearsion_ldap do - Service.new.start + Service.start end end diff --git a/lib/adhearsion/ldap/plugin/service.rb b/lib/adhearsion/ldap/plugin/service.rb index 402e4ed..badb3c9 100644 --- a/lib/adhearsion/ldap/plugin/service.rb +++ b/lib/adhearsion/ldap/plugin/service.rb @@ -3,6 +3,12 @@ module LDAP class Plugin class Service + class << self + def start + new.start + end + end + ## # Start LDAP connection def start From 85d43fed0261e6241a8dff887954a79c2638fdfd Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Wed, 18 Jan 2012 11:08:16 +0000 Subject: [PATCH 8/8] [BUGFIX] Fix another failing test --- spec/adhearsion/ldap/plugin/service_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/adhearsion/ldap/plugin/service_spec.rb b/spec/adhearsion/ldap/plugin/service_spec.rb index f2b3092..2fd5643 100644 --- a/spec/adhearsion/ldap/plugin/service_spec.rb +++ b/spec/adhearsion/ldap/plugin/service_spec.rb @@ -21,6 +21,7 @@ describe "when starting the LDAP connection" do before do Adhearsion.config[:adhearsion_ldap].host = "localhost" + Adhearsion::LDAP::Plugin::Service.any_instance.should_receive(:require_models).and_return true end after do @@ -28,8 +29,7 @@ end it "should call Connection.start method with the valid parameters" do - Adhearsion::LDAP::Plugin::Service.should_receive(:establish_connection).with("localhost", 389, "", "", "", false, false).and_return true - Adhearsion.config.should_receive(:files_from_setting).with("paths", "models").and_return [] + Adhearsion::LDAP::Plugin::Service.any_instance.should_receive(:establish_connection).with("localhost", 389, "", "", "", false, false).and_return true Adhearsion::Plugin.init_plugins end