Class: Socialcast::CommandLine::LDAPConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/socialcast/command_line/ldap_connector.rb

Constant Summary collapse

UNIQUE_IDENTIFIER =
"unique_identifier"
EMAIL =
"email"
PRIMARY_ATTRIBUTES =
[UNIQUE_IDENTIFIER, 'first_name', 'last_name', 'employee_number']
CONTACT_ATTRIBUTES =
[EMAIL, 'location', 'cell_phone', 'office_phone']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_name, config) ⇒ LDAPConnector

Returns a new instance of LDAPConnector.



15
16
17
18
# File 'lib/socialcast/command_line/ldap_connector.rb', line 15

def initialize(connection_name, config)
  @connection_name = connection_name
  @config = config
end

Instance Attribute Details

#attribute_mappingsObject (readonly)

Returns the value of attribute attribute_mappings.



13
14
15
# File 'lib/socialcast/command_line/ldap_connector.rb', line 13

def attribute_mappings
  @attribute_mappings
end

#connection_nameObject (readonly)

Returns the value of attribute connection_name.



13
14
15
# File 'lib/socialcast/command_line/ldap_connector.rb', line 13

def connection_name
  @connection_name
end

Instance Method Details

#connection_configObject



110
111
112
# File 'lib/socialcast/command_line/ldap_connector.rb', line 110

def connection_config
  @config["connections"][connection_name]
end

#each_ldap_entryObject



26
27
28
29
30
31
32
# File 'lib/socialcast/command_line/ldap_connector.rb', line 26

def each_ldap_entry
  search(:return_result => false, :filter => connection_config["filter"], :base => connection_config["basedn"], :attributes => ldap_search_attributes) do |entry|
    if grab(entry, attribute_mappings[EMAIL]).present? || (attribute_mappings.has_key?(UNIQUE_IDENTIFIER) && grab(entry, attribute_mappings[UNIQUE_IDENTIFIER]).present?)
      yield entry
    end
  end
end

#each_user_hashObject



20
21
22
23
24
# File 'lib/socialcast/command_line/ldap_connector.rb', line 20

def each_user_hash
  each_ldap_entry do |entry|
    yield build_user_hash_from_mappings(entry)
  end
end

#fetch_user_hash(identifier, options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/socialcast/command_line/ldap_connector.rb', line 34

def fetch_user_hash(identifier, options)
  options = options.dup
  identifying_field = options.delete(:identifying_field) || UNIQUE_IDENTIFIER

  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[identifying_field]}=#{identifier}")

  search(:base => connection_config['basedn'], :filter => filter, :attributes => ldap_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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/socialcast/command_line/ldap_connector.rb', line 87

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

#ldap_search_attributesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/socialcast/command_line/ldap_connector.rb', line 53

def ldap_search_attributes
  membership_attribute = permission_mappings.fetch 'attribute_name', 'memberof'
  attributes = attribute_mappings.values.map do |mapping_value|
    value = begin
      mapping_value.camelize.constantize
    rescue NameError
      mapping_value
    end

    case value
    when Hash
      dup_mapping_value = value.dup
      dup_mapping_value.delete("value")
      dup_mapping_value.values
    when String
      value
    when Class, Module
      if value.respond_to?(:attributes)
        value.attributes
      else
        mapping_value
      end
    end
  end.flatten
  attributes << membership_attribute
end