Module: Socialcast::LdapIntegration
- Defined in:
- lib/socialcast/ldap_integration.rb,
lib/socialcast/ldap_integration/version.rb
Constant Summary collapse
- NULL_FILTER =
::Net::LDAP::Filter.pres("objectclass")
- VERSION =
"1.1.30"
Class Method Summary collapse
-
.authenticated?(identifier, password, options = {}) ⇒ Boolean
Supported options: [:identifying_field] the field which is being used to look up the user.
- .available? ⇒ Boolean
- .config ⇒ Object
- .config=(config) ⇒ Object
-
.connect(options = {}) ⇒ Object
Connects to the Directory servers specified in the configuration.
- .connect_to(connection_options) ⇒ Object
-
.discover_base_dn(identifying_field, identifier) ⇒ Object
Identifying field will be something like “mail” or “sAMAccountName”.
- .fetch_account_info(identifier, options = {}) ⇒ Object
- .mirror_lazily? ⇒ Boolean
-
.mirroring_field?(field) ⇒ Boolean
Returns true if LDAP will manage the user field with the given name.
- .schedule ⇒ Object
- .scheduled? ⇒ Boolean
-
.search(options = {}) ⇒ Object
Supported options [:filter] A string representing the search filter to use instead of the default search filter.
- .user_present_in_directory?(identifier, options) ⇒ Boolean
- .with_connections(options = {}) {|connections| ... } ⇒ Object
Class Method Details
.authenticated?(identifier, password, options = {}) ⇒ Boolean
Supported options:
- :identifying_field
-
the field which is being used to look up the user. Should probably be ‘company_login’ or ‘email’.
Defaults to 'company_login'
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/socialcast/ldap_integration.rb', line 132 def self.authenticated?(identifier, password, = {}) return false if password.blank? return false if !self.user_present_in_directory?(identifier, ) = .reverse_merge!({:identifying_field => 'company_login'}).delete(:identifying_field) Socialcast::LdapIntegration.connect do |ldap, connection| = {} .reverse_merge! base = connection['base'] host = connection['host'] port = connection['port'] map = connection['map'] if base.blank? base = self.discover_base_dn(map[], identifier) end .reverse_merge!({ :host => host, :port => port, :base => base, :filter => "#{map[]}=#{identifier}", :password => password }) return true if !ldap.bind_as().blank? end return false end |
.available? ⇒ Boolean
18 19 20 21 22 23 24 |
# File 'lib/socialcast/ldap_integration.rb', line 18 def self.available? return false unless self.config return self.config['connections'].all? do |connection| self.connection_fields_present?(connection) && self.required_fields_present?(connection) end end |
.config ⇒ Object
10 11 12 |
# File 'lib/socialcast/ldap_integration.rb', line 10 def self.config @@config end |
.config=(config) ⇒ Object
14 15 16 |
# File 'lib/socialcast/ldap_integration.rb', line 14 def self.config=(config) @@config = config end |
.connect(options = {}) ⇒ Object
Connects to the Directory servers specified in the configuration.
Supported Options:
- :config
-
One or more LDAP connection specifications to connect to, overrides those in LdapIntegration.config
Yields:
-
Connected and authenticated Net::LDAP object
-
Connection settings for the connection
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/socialcast/ldap_integration.rb', line 62 def self.connect(={}) raise ArgumentError.new("connect requires a block, see connect_to") unless block_given? config = [:config] ? [[:config]] : Socialcast::LdapIntegration.config['connections'] config.each do || ldap = connect_to() yield(ldap, ) end nil end |
.connect_to(connection_options) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/socialcast/ldap_integration.rb', line 82 def self.connect_to() searcher_username = ['searcher_username'] searcher_password = ['searcher_password'] base = ['base'] host = ['host'] port = ['port'] ssl = ['ssl'] = {:host => host, :port => port, :base => base} [:encryption] = :simple_tls if ssl ldap = Net::LDAP.new ldap.auth searcher_username, searcher_password return ldap end |
.discover_base_dn(identifying_field, identifier) ⇒ Object
Identifying field will be something like “mail” or “sAMAccountName”
169 170 171 172 173 174 175 176 177 |
# File 'lib/socialcast/ldap_integration.rb', line 169 def self.discover_base_dn(, identifier) sanitized_field_name = .downcase.to_sym Socialcast::LdapIntegration.search do |entry, connection| if entry.respond_to?(sanitized_field_name) && entry.send(sanitized_field_name).first == identifier return entry.dn end end "" end |
.fetch_account_info(identifier, options = {}) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/socialcast/ldap_integration.rb', line 195 def self.fetch_account_info(identifier, = {}) = .delete(:identifying_field) || 'company_login' matched_account = nil self.connect do |ldap, connection| map = connection["map"] base = connection["base"] if base.blank? base = discover_base_dn(map[], identifier) end filter = self.construct_filter_for_search(connection, nil, "#{map[]}=#{identifier}") ldap.search(:base => base, :filter => filter[:filter]) do |entry, connection| matched_account = entry end if matched_account mapped_account_info = HashWithIndifferentAccess.new map.each_pair do |sc_field, ldap_field| mapped_account_info[sc_field] = matched_account[ldap_field].first unless ldap_field.blank? end return mapped_account_info end end nil end |
.mirror_lazily? ⇒ Boolean
30 31 32 33 34 35 |
# File 'lib/socialcast/ldap_integration.rb', line 30 def self.mirror_lazily? # should we really return true here if the connection is unavailable? return true unless self.available? self.config['lazy_mirroring'].to_s == 'true' end |
.mirroring_field?(field) ⇒ Boolean
Returns true if LDAP will manage the user field with the given name
180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/socialcast/ldap_integration.rb', line 180 def self.mirroring_field?(field) string_field = field.to_s if self.available? && (["password", "email"].include?(string_field)) return true else ldap_connections = self.config["connections"] ldap_connections.each do |connection| map = connection["map"] field_populated = map.keys.include?(string_field) && !map[string_field].blank? return true if field_populated end end false end |
.schedule ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/socialcast/ldap_integration.rb', line 37 def self.schedule config = Socialcast::LdapIntegration.config cron_line = nil if config['enableLdapSchedule'] case config['schedule']['checkFreq'] when 'Hourly' cron_line = "0 */#{config['schedule']['hourInterval'].to_i} * * *" when 'Daily' cron_line = "0 #{config['schedule']['timeHour']} * * *" when 'Weekly' cron_line = "0 #{config['schedule']['timeHour']} * * #{config['schedule']['timeDay'].to_i + 1}" end end return cron_line end |
.scheduled? ⇒ Boolean
26 27 28 |
# File 'lib/socialcast/ldap_integration.rb', line 26 def self.scheduled? self.config['enableLdapSchedule'].to_s == 'true' end |
.search(options = {}) ⇒ Object
Supported options
- :filter
-
A string representing the search filter to use instead
of the default search filter. Overrides any :auxiliary_filter
option.
- :auxiliary_filter
-
A string representing a search filter to use
in addition to the default search filter. The two filters
will be combined using a logical AND.
- :limit
-
A number representing the maxmimum number of records to
return, defaults to unlimited.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/socialcast/ldap_integration.rb', line 106 def self.search(={}) .reverse_merge! :connect_options => {} Socialcast::LdapIntegration.connect(.delete(:connect_options) || {}) do |ldap, connection| = self.construct_filter_for_search(connection, .delete(:filter), .delete(:auxiliary_filter)) [:size] = [:limit] if .has_key?(:limit) [:return_result] = false = [] if connection["base"].blank? tree_root = ldap.search_root_dse distinguished_names = Array.wrap(tree_root.namingcontexts) = distinguished_names.map { |dn| .merge(:base => dn ) } end .each do || ldap.search() do |entry| yield(entry, connection) if block_given? end end end end |
.user_present_in_directory?(identifier, options) ⇒ Boolean
162 163 164 165 166 |
# File 'lib/socialcast/ldap_integration.rb', line 162 def self.user_present_in_directory?(identifier, ) account_info = self.fetch_account_info(identifier, .dup) is_user_present = !account_info.nil? is_user_present end |
.with_connections(options = {}) {|connections| ... } ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/socialcast/ldap_integration.rb', line 73 def self.with_connections(={}) connections = [] connect() do |ldap, | connections << [ldap, ] end yield connections nil end |