Class: Adauth::AdObject

Inherits:
Object
  • Object
show all
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



56
57
58
# File 'lib/adauth/ad_object.rb', line 56

def initialize(ldap_object)
    @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



66
67
68
69
70
# File 'lib/adauth/ad_object.rb', line 66

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



51
52
53
# File 'lib/adauth/ad_object.rb', line 51

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

.allObject

Returns all objects which have the ObjectClass of the inherited class



19
20
21
22
# File 'lib/adauth/ad_object.rb', line 19

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



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

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



27
28
29
30
31
# File 'lib/adauth/ad_object.rb', line 27

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



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/adauth/ad_object.rb', line 89

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

#dn_ousObject

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



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

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



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

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

#handle_field(field) ⇒ Object

Handle the output for the given field



73
74
75
76
77
78
# File 'lib/adauth/ad_object.rb', line 73

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)


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

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



61
62
63
# File 'lib/adauth/ad_object.rb', line 61

def ldap_object
    @ldap_object
end

#membersObject

Returns an array of member objects for this object



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

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



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

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



102
103
104
105
106
107
108
109
110
# File 'lib/adauth/ad_object.rb', line 102

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