Class: Tapjoy::LDAP::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tapjoy/ldap/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Instantiate class



8
9
10
11
12
13
14
15
16
17
# File 'lib/tapjoy/ldap/base.rb', line 8

def initialize
  ldap_config_file = "#{ldap_config_directory}/ldap_info.yaml"
  ldap_password_file = "#{ldap_config_directory}/ldap.secret"
  @ldap_info    = YAML.load_file(ldap_config_file)
  @hosts        = @ldap_info['servers']
  @basedn       = @ldap_info['basedn']
  @conn         = find_valid_host(ldap_password_file)
  @service_ou   = @ldap_info['service_ou']
  @email_domain = @ldap_info['email_domain']
end

Instance Attribute Details

#basednObject (readonly)

Returns the value of attribute basedn.



5
6
7
# File 'lib/tapjoy/ldap/base.rb', line 5

def basedn
  @basedn
end

#connObject (readonly)

Returns the value of attribute conn.



5
6
7
# File 'lib/tapjoy/ldap/base.rb', line 5

def conn
  @conn
end

#groupObject (readonly)

Returns the value of attribute group.



5
6
7
# File 'lib/tapjoy/ldap/base.rb', line 5

def group
  @group
end

#hostsObject (readonly)

Returns the value of attribute hosts.



5
6
7
# File 'lib/tapjoy/ldap/base.rb', line 5

def hosts
  @hosts
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/tapjoy/ldap/base.rb', line 5

def key
  @key
end

#service_ouObject (readonly)

Returns the value of attribute service_ou.



5
6
7
# File 'lib/tapjoy/ldap/base.rb', line 5

def service_ou
  @service_ou
end

Instance Method Details

#add(distinguished_name, attributes) ⇒ Object

Add objects to LDAP



42
43
44
45
# File 'lib/tapjoy/ldap/base.rb', line 42

def add(distinguished_name, attributes)
  @conn.add(:dn => distinguished_name, :attributes => attributes)
  return return_result
end

#add_attribute(distinguished_name, attribute, value) ⇒ Object



47
48
49
50
# File 'lib/tapjoy/ldap/base.rb', line 47

def add_attribute(distinguished_name, attribute, value)
  @conn.add_attribute(distinguished_name, attribute, value)
  return return_result
end

#delete(distinguished_name) ⇒ Object

Delete objects from LDAP



64
65
66
67
# File 'lib/tapjoy/ldap/base.rb', line 64

def delete(distinguished_name)
  @conn.delete(:dn => distinguished_name)
  return return_result
end

#get_max_id(object_type, role) ⇒ Object

Get highest used ID



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/tapjoy/ldap/base.rb', line 77

def get_max_id(object_type, role)
  case object_type
  when 'user'
    objectclass = 'person'
    ldap_attr   = 'uidNumber'
  when 'group'
    objectclass = 'posixGroup'
    ldap_attr   = 'gidNumber'
  else
    abort('Unknown object type')
  end

  minID, maxID = set_id_boundary(role)

  # LDAP Filters
  oc_filter   = Net::LDAP::Filter.eq('objectclass', objectclass)
  attr_filter = Net::LDAP::Filter.eq(ldap_attr, '*')
  filter      = Net::LDAP::Filter.join(oc_filter, attr_filter)

  highid = minID - 1  #subtract 1, so we can add 1 later

  id_list = search([ldap_attr], filter)
  id_list.each do |item|

    # parse attribute associated with object
    # users => uidnumber
    # groups => gidnumber
    if object_type == 'user'
      id = item.uidnumber[0].to_i
    elsif object_type == 'group'
      id = item.gidnumber[0].to_i
    else
      abort('Unknown object')
    end

    # Now that we have the appropriate attribute
    # let's find the first useable id.
    # I *really* hate the pattern I use here, but
    # can't think of a better one atm.
    if id > highid
      highid = id
    end
    if maxID.nil?
      next
    else
      if id > maxID
        highid = maxID
      end
    end
  end

  if !highid.nil?
    id = highid + 1
    return id.to_s
  else
    abort("Unable to find highest #{ldap_attr}")
  end
end

#ldap_config_directoryObject

Set LDAP Config Directory



20
21
22
# File 'lib/tapjoy/ldap/base.rb', line 20

def ldap_config_directory
  return "#{ENV['LDAP_CONFIG_DIR'] ? ENV['LDAP_CONFIG_DIR'] : ENV['HOME'] + '/.ldap'}"
end

#modify(distinguished_name, operations) ⇒ Object

Modify objects in LDAP



58
59
60
61
# File 'lib/tapjoy/ldap/base.rb', line 58

def modify(distinguished_name, operations)
  @conn.modify(:dn => distinguished_name, :operations => operations)
  return return_result
end

#replace_attribute(distinguished_name, attribute, value) ⇒ Object



52
53
54
55
# File 'lib/tapjoy/ldap/base.rb', line 52

def replace_attribute(distinguished_name, attribute, value)
  @conn.replace_attribute(distinguished_name, attribute, value)
  return_result
end

#return_resultObject

Format return codes



70
71
72
73
74
# File 'lib/tapjoy/ldap/base.rb', line 70

def return_result
  msg1 = "Return Code: #{ @conn.get_operation_result.code }\n"
  msg2 = "Message: #{ @conn.get_operation_result.message }"
  return msg1 + msg2
end

#search(attributes = ['*'], filter = Net::LDAP::Filter.eq('objectclass','*')) ⇒ Object

Search the LDAP directory



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tapjoy/ldap/base.rb', line 25

def search(attributes = ['*'],
           filter = Net::LDAP::Filter.eq('objectclass','*'))
  @entries = []
  if @conn
    @conn.search :base => @basedn,
                 :filter => filter,
                 :attributes => attributes do |entry|
      @entries.push(entry)
    end
  else
    abort('Could not connect to any LDAP servers')
  end

  return @entries
end