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
18
19
20
21
22
23
24
# 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"

  begin
    if can_read_files?(ldap_config_file, ldap_password_file)
      load_config_from_files(ldap_config_file, ldap_password_file)
    else
      load_config_from_env
    end
  rescue => err
    STDERR.puts "Error message: #{err.inspect}"
    abort("Config not specified.  Either provide #{ldap_config_file} and #{ldap_password_file} or environment variables")
  else
    @conn = find_valid_host
  end
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



49
50
51
52
# File 'lib/tapjoy/ldap/base.rb', line 49

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

#add_attribute(distinguished_name, attribute, value) ⇒ Object



54
55
56
57
# File 'lib/tapjoy/ldap/base.rb', line 54

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



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

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

#get_max_id(object_type, role) ⇒ Object

Get highest used ID



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
135
136
137
138
139
140
141
# File 'lib/tapjoy/ldap/base.rb', line 84

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



27
28
29
# File 'lib/tapjoy/ldap/base.rb', line 27

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



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

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

#replace_attribute(distinguished_name, attribute, value) ⇒ Object



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

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

#return_resultObject

Format return codes



77
78
79
80
81
# File 'lib/tapjoy/ldap/base.rb', line 77

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tapjoy/ldap/base.rb', line 32

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