Class: Adauth::AdObject

Inherits:
Object
  • Object
show all
Includes:
Expects
Defined in:
lib/adauth/ad_object.rb

Overview

Active Directory Interface Object

Objects inherit from this class.

Provides all the common functions for Active Directory.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ldap_object) ⇒ AdObject

Creates a new instance of the object and sets @ldap_object to the passed Net::LDAP entity


82
83
84
85
# File 'lib/adauth/ad_object.rb', line 82

def initialize(ldap_object)
    expects ldap_object, Net::LDAP::Entry
    @ldap_object = ldap_object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object


30
31
32
33
34
# File 'lib/adauth/ad_object.rb', line 30

def method_missing(method, *args)
  field = self.class::Fields[method]
  return handle_field(field) if field
  return super
end

Class Method Details

.add_object_filter(filter) ⇒ Object

Adds the object filter to the passed filter


77
78
79
# File 'lib/adauth/ad_object.rb', line 77

def self.add_object_filter(filter)
  filter & self::ObjectFilter
end

.allObject

Returns all objects which have the ObjectClass of the inherited class


45
46
47
48
# File 'lib/adauth/ad_object.rb', line 45

def self.all
    Adauth.logger.info(self.class.inspect) { "Searching for all objects matching filter \"#{self::ObjectFilter}\"" }
    Adauth::SearchResults.new(self.filter(self::ObjectFilter))
end

.filter(filter) ⇒ Object

Returns all LDAP objects that match the given filter

Use with add_object_filter to make sure that you only get objects that match the object you are querying though


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/adauth/ad_object.rb', line 62

def self.filter(filter)
  results = []

  result = Adauth.connection.search(:filter => filter)

  raise 'Search returned NIL' if result == nil

  result.each do |entry|
    results << self.new(entry)
  end

  results
end

.method_missing(method, *args) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/adauth/ad_object.rb', line 22

def self.method_missing(method, *args)
  return super unless method =~ /^find_by_/
  method_field = method.to_s.split("_").last
  field = self::Fields[method_field.to_sym]
  return super unless field
  self.where(field, args.first)
end

.reverse_field(search) ⇒ Object


36
37
38
39
40
41
42
# File 'lib/adauth/ad_object.rb', line 36

def self.reverse_field(search)
  hash = {}
  self::Fields.each do |k, v|
    hash[v] = k
  end
  return hash[search]
end

.where(field, value) ⇒ Object

Returns all the objects which match the supplied query

Uses ObjectFilter to restrict to the current object


53
54
55
56
57
# File 'lib/adauth/ad_object.rb', line 53

def self.where(field, value)
    search_filter = Net::LDAP::Filter.eq(field, value)
    Adauth.logger.info(self.class.inspect) { "Searching for all \"#{self::ObjectFilter}\" where #{field} = #{value}" }
    Adauth::SearchResults.new(filter(add_object_filter(search_filter)))
end

Instance Method Details

#cn_groups_nestedObject

The same as cn_groups, but with the parent groups included


109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/adauth/ad_object.rb', line 109

def cn_groups_nested
  @cn_groups_nested = cn_groups
  cn_groups.each do |group|
    ado = Adauth::AdObjects::Group.where('name', group).first
    if ado
      groups = convert_to_objects ado.cn_groups
      groups.each do |g|
        @cn_groups_nested.push g if !(@cn_groups_nested.include?(g))
      end
    end
  end
  return @cn_groups_nested
end

#deleteObject

Delete the object


178
179
180
# File 'lib/adauth/ad_object.rb', line 178

def delete
  Adauth.connection.delete(dn: @ldap_object.dn)
end

#dn_ousObject

CSV Version of the ous list (can't be pulled over from AD)


135
136
137
138
139
140
141
142
143
# File 'lib/adauth/ad_object.rb', line 135

def dn_ous
    unless @dn_ous
        @dn_ous = []
        @ldap_object.dn.split(/,/).each do |entry|
            @dn_ous.push entry.gsub(/OU=/, '').gsub(/CN=/,'') if entry =~ /OU=/ or entry == "CN=Users"
        end
    end
    @dn_ous
end

#groupsObject

Returns all the groups the object is a member of


101
102
103
104
105
106
# File 'lib/adauth/ad_object.rb', line 101

def groups
    unless @groups
        @groups = convert_to_objects(cn_groups)
    end
    @groups
end

#handle_field(field) ⇒ Object

Handle the output for the given field


93
94
95
96
97
98
# File 'lib/adauth/ad_object.rb', line 93

def handle_field(field)
  case field
    when Symbol then return return_symbol_value(field)
    when Array then return  @ldap_object.try(field.first).try(:collect, &field.last)
  end
end

#is_a_member?(parent) ⇒ Boolean

Checks to see if the object is a member of a given parent (though DN)


168
169
170
171
172
173
174
175
# File 'lib/adauth/ad_object.rb', line 168

def is_a_member?(parent)
  my_split_dn = @ldap_object.dn.split(",")
  parent_split_dn = parent.ldap_object.dn.split(",")
  if (my_split_dn.count - 1) == parent_split_dn.count
    return true if my_split_dn[1] == parent_split_dn[0]
  end
  return false
end

#ldap_objectObject

Allows direct access to @ldap_object


88
89
90
# File 'lib/adauth/ad_object.rb', line 88

def ldap_object
    @ldap_object
end

#membersObject

Returns an array of member objects for this object


155
156
157
158
159
160
161
162
163
164
165
# File 'lib/adauth/ad_object.rb', line 155

def members
    unless @members
        @members = []
        [Adauth::AdObjects::Computer, Adauth::AdObjects::OU, Adauth::AdObjects::User, Adauth::AdObjects::Group].each do |object|
            object.all.each do |entity|
                @members.push entity if entity.is_a_member?(self)
            end
        end
    end
    @members
end

#modify(operations) ⇒ Object

Runs a modify action on the current object, takes an aray of operations


146
147
148
149
150
151
152
# File 'lib/adauth/ad_object.rb', line 146

def modify(operations)
  Adauth.logger.info(self.class.inspect) { "Attempting modify operation" }
  unless Adauth.connection.modify :dn => @ldap_object.dn, :operations => operations
    Adauth.logger.fatal(self.class.inspect) { "Modify Operation Failed! Code: #{Adauth.connection.get_operation_result.code} Message: #{Adauth.connection.get_operation_result.message}" }
    raise 'Modify Operation Failed (see log for details)'
  end
end

#ousObject

Returns all the ous the object is in


124
125
126
127
128
129
130
131
132
# File 'lib/adauth/ad_object.rb', line 124

def ous
    unless @ous
        @ous = []
        @ldap_object.dn.split(/,/).each do |entry|
            @ous.push Adauth::AdObjects::OU.where('name', entry.gsub(/OU=/, '')).first if entry =~ /OU=/
        end
    end
    @ous
end