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



58
59
60
61
# File 'lib/adauth/ad_object.rb', line 58

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

Over ride method missing to see if the object has a field by that name



69
70
71
72
73
# File 'lib/adauth/ad_object.rb', line 69

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



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

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

.allObject

Returns all objects which have the ObjectClass of the inherited class



21
22
23
24
# File 'lib/adauth/ad_object.rb', line 21

def self.all
    Adauth.logger.info(self.class.inspect) { "Searching for all objects matching filter \"#{self::ObjectFilter}\"" }
    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



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/adauth/ad_object.rb', line 38

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

.where(field, value) ⇒ Object

Returns all the objects which match the supplied query

Uses ObjectFilter to restrict to the current object



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

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}" }
    filter(add_object_filter(search_filter))
end

Instance Method Details

#cn_groups_nestedObject

The same as cn_groups, but with the parent groups included



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/adauth/ad_object.rb', line 92

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

#deleteObject

Delete the object



159
160
161
# File 'lib/adauth/ad_object.rb', line 159

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)



116
117
118
119
120
121
122
123
124
# File 'lib/adauth/ad_object.rb', line 116

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



84
85
86
87
88
89
# File 'lib/adauth/ad_object.rb', line 84

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

#handle_field(field) ⇒ Object

Handle the output for the given field



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

def handle_field(field)
  case field
    when Symbol then return return_symbol_value(field)
    when Array then return  @ldap_object.send(field.first).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)

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
# File 'lib/adauth/ad_object.rb', line 149

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



64
65
66
# File 'lib/adauth/ad_object.rb', line 64

def ldap_object
    @ldap_object
end

#membersObject

Returns an array of member objects for this object



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

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



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

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



105
106
107
108
109
110
111
112
113
# File 'lib/adauth/ad_object.rb', line 105

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