class Soar::Registry::Directory::Provider::Ldap
Attributes
Public Class Methods
@param [Hash{String => String, Number}] config @option config [String] host @option config [Number] port @param [String] base ldap tree base eg 'dc=hetzner,dc=co,dc=za' @param [Array<String>] attributes array of attributes to return for queries @param [Array<String>] index @param [Hash{String => String}] credentials @option credentials [String] username @option credentials [String] password
# File lib/soar/registry/directory/provider/ldap.rb, line 24 def initialize(config: , base: , attributes: , index: , credentials: ) adapt_exceptions do @index = index @attributes = attributes @config = { host: config['host'], port: config['port'], base: base, auth: { method: :simple, username: credentials['username'], password: credentials['password'] }, encryption: { method: :simple_tls } } @client = Net::LDAP.new(@config) end end
Public Instance Methods
@param [String] dn @return [Boolean]
# File lib/soar/registry/directory/provider/ldap.rb, line 131 def delete(dn) adapt_exceptions do @client.delete(dn: dn) return true end end
@param [String] primary key of the identity ie. the first index @return [Hash{String => String, Number, Array}] single matching entry @raise [Soar::Registry::Directory::Error::NoEntriesFoundError] if primary key not found @raise [Soar::Registry::Directory::Error::MultipleEntriesFound] if multiple matches found
# File lib/soar/registry/directory/provider/ldap.rb, line 74 def fetch(primary_key) adapt_exceptions do result = @client.search({ attributes: @attributes, filter: Net::LDAP::Filter.eq(@index[0], primary_key), return_result: true }) raise Soar::Registry::Directory::Error::NoEntriesFoundError, "No entries found for #{@index[0]} = #{primary_key}" if result.empty? raise Soar::Registry::Directory::Error::MultipleEntriesFound if result.length > 1 response = {} @attributes.each { |attribute| response[attribute] = result[0][attribute.to_sym][0] } return response end end
@return [Array<String, Number>] a list of indexes
# File lib/soar/registry/directory/provider/ldap.rb, line 121 def index adapt_exceptions do @index end end
@param [Hash{String => String, Number, Array}] entry @return [Boolean] @raise [Soar::Registry::Directory::Error::DuplicateEntryError]
# File lib/soar/registry/directory/provider/ldap.rb, line 50 def put(entry) adapt_exceptions do @index.each { |index| if (entry['attributes'].keys.include?(index)) result = search(index, entry['attributes'][index]) raise Soar::Registry::Directory::Error::DuplicateEntryError if result.length > 0 end } @client.add({ dn: entry['dn'], attributes: entry['attributes'].map { |k, v| [k.to_sym, v] }.to_h }) return true end end
@param [String] key @param [String] value @return [Array<Hash{String => String, Array, Number}>] list of matching entries @raise [ArgumentError] if value or named index is not specified
# File lib/soar/registry/directory/provider/ldap.rb, line 98 def search(key, value) adapt_exceptions do result = [] entries = @client.search({ filter: Net::LDAP::Filter.eq(key, value), attributes: @attributes, return_result: true }) entries.each { |entry| response = {} @attributes.each { |attribute| response[attribute] = entry[attribute.to_sym][0] } result << response } return result end end
Private Instance Methods
# File lib/soar/registry/directory/provider/ldap.rb, line 140 def adapt_exceptions begin yield rescue Net::LDAP::BindingInformationInvalidError, Net::LDAP::NoBindResultError, Net::LDAP::ConnectionRefusedError, Errno::ECONNREFUSED => e raise Soar::Registry::Directory::Error::NetworkingError, e.message end end