Class: Socialcast::CommandLine::LDAPConnector
- Inherits:
-
Object
- Object
- Socialcast::CommandLine::LDAPConnector
- Defined in:
- lib/socialcast/command_line/ldap_connector.rb
Constant Summary collapse
- UNIQUE_IDENTIFIER_ATTRIBUTE =
"unique_identifier"
- EMAIL_ATTRIBUTE =
"email"
- MANAGER_ATTRIBUTE =
"manager"
- PRIMARY_ATTRIBUTES =
[UNIQUE_IDENTIFIER_ATTRIBUTE, 'first_name', 'last_name', 'employee_number']
- CONTACT_ATTRIBUTES =
[EMAIL_ATTRIBUTE, 'location', 'cell_phone', 'office_phone']
- PROFILE_PHOTO_ATTRIBUTE =
'profile_photo'
Instance Attribute Summary collapse
-
#attribute_mappings ⇒ Object
readonly
Returns the value of attribute attribute_mappings.
-
#connection_name ⇒ Object
readonly
Returns the value of attribute connection_name.
Class Method Summary collapse
- .attribute_mappings_for(connection_name, config) ⇒ Object
- .with_connector(connection_name, config) ⇒ Object
Instance Method Summary collapse
- #each_photo_hash ⇒ Object
- #each_user_hash ⇒ Object
- #fetch_user_hash(identifier, options) ⇒ Object
-
#grab(entry, attribute) ⇒ Object
grab a single value of an attribute abstracts away ldap multivalue attributes.
-
#initialize(connection_name, config, ldap) ⇒ LDAPConnector
constructor
A new instance of LDAPConnector.
Constructor Details
#initialize(connection_name, config, ldap) ⇒ LDAPConnector
Returns a new instance of LDAPConnector.
33 34 35 36 37 38 39 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 33 def initialize(connection_name, config, ldap) @connection_name = connection_name @config = config @ldap = ldap @group_unique_identifiers = fetch_group_unique_identifiers @dn_to_email_hash = fetch_dn_to_email_hash end |
Instance Attribute Details
#attribute_mappings ⇒ Object (readonly)
Returns the value of attribute attribute_mappings.
15 16 17 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 15 def attribute_mappings @attribute_mappings end |
#connection_name ⇒ Object (readonly)
Returns the value of attribute connection_name.
15 16 17 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 15 def connection_name @connection_name end |
Class Method Details
.attribute_mappings_for(connection_name, config) ⇒ Object
29 30 31 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 29 def self.attribute_mappings_for(connection_name, config) config['connections'][connection_name]['mappings'] || config.fetch('mappings', {}) end |
.with_connector(connection_name, config) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 17 def self.with_connector(connection_name, config) connection_config = config["connections"][connection_name] ldap = Net::LDAP.new(:host => connection_config["host"], :port => connection_config["port"], :base => connection_config["basedn"]).tap do |ldap_instance| ldap_instance.encryption connection_config['encryption'].to_sym if connection_config['encryption'] ldap_instance.auth connection_config["username"], connection_config["password"] end ldap.open do |connected_ldap_instance| yield new(connection_name, config, connected_ldap_instance) end end |
Instance Method Details
#each_photo_hash ⇒ Object
47 48 49 50 51 52 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 47 def each_photo_hash each_ldap_entry(ldap_photo_search_attributes) do |entry| photo_hash = build_photo_hash_from_mappings(entry) yield photo_hash if photo_hash.present? end end |
#each_user_hash ⇒ Object
41 42 43 44 45 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 41 def each_user_hash each_ldap_entry(ldap_user_search_attributes) do |entry| yield build_user_hash_from_mappings(entry) end end |
#fetch_user_hash(identifier, options) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 54 def fetch_user_hash(identifier, ) = .dup = .delete(:identifying_field) || UNIQUE_IDENTIFIER_ATTRIBUTE filter = if connection_config['filter'].present? Net::LDAP::Filter.construct(connection_config['filter']) else Net::LDAP::Filter.pres("objectclass") end filter = filter & Net::LDAP::Filter.construct("#{attribute_mappings[]}=#{identifier}") search(:base => connection_config['basedn'], :filter => filter, :attributes => ldap_user_search_attributes, :size => 1) do |entry| return build_user_hash_from_mappings(entry) end nil end |
#grab(entry, attribute) ⇒ Object
grab a single value of an attribute abstracts away ldap multivalue attributes
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/socialcast/command_line/ldap_connector.rb', line 79 def grab(entry, attribute) const_attribute = begin attribute.camelize.constantize rescue NameError attribute end case const_attribute when Hash dup_attribute = const_attribute.dup value = dup_attribute.delete("value") sprintf value, Hash[dup_attribute.map { |k, v| [k, grab(entry, v)] }].symbolize_keys when String normalize_ldap_value(entry, attribute) when Class, Module if const_attribute.respond_to?(:run) const_attribute.run(entry) else normalize_ldap_value(entry, attribute) end end end |