Module: Devise::AdObject::ClassMethods

Defined in:
lib/devise_active_directory_authenticatable/models/ad_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#member_groupsObject

Returns the value of attribute member_groups.



83
84
85
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 83

def member_groups
  @member_groups
end

#member_usersObject

Returns the value of attribute member_users.



83
84
85
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 83

def member_users
  @member_users
end

#memberofObject

Returns the value of attribute memberof.



83
84
85
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 83

def memberof
  @memberof
end

Instance Method Details

#activedirectory_class_nameObject



118
119
120
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 118

def activedirectory_class_name
  @ad_class ||= activedirectory_class.name[/.*::(.*)/, 1]
end

#activedirectory_connectObject

Attempts to connect with the activedirectory based on the configuration options



204
205
206
207
208
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 204

def activedirectory_connect
  ActiveDirectory::Base.enable_cache if ::Devise.ad_caching
  ActiveDirectory::Base.setup(::Devise.ad_settings)
  raise DeviseActiveDirectoryAuthenticatable::ActiveDirectoryException, "Invalid Username or Password" unless ActiveDirectory::Base.connected?
end

#connected_to_activedirectory?Boolean

Checks to see if a conection with AD has been established

Returns:

  • (Boolean)


190
191
192
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 190

def connected_to_activedirectory?
  ActiveDirectory::Base.connected?
end

#devise_ad_member_groups(field, params = {}) ⇒ Object



106
107
108
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 106

def devise_ad_member_groups field, params = {}
  @member_groups = set_devise_ad_options field, params
end

#devise_ad_member_users(field, params = {}) ⇒ Object



110
111
112
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 110

def devise_ad_member_users field, params = {}
  @member_users = set_devise_ad_options field, params
end

#devise_ad_memberof(field, params = {}) ⇒ Object



102
103
104
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 102

def devise_ad_memberof field, params = {}
  @memberof = set_devise_ad_options field, params
end

#devise_model_nameObject



114
115
116
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 114

def devise_model_name
  @devise_model ||= devise_model.name[/.*::(.*)/, 1]
end

#find_activedirectory_objs(local_params = {}) ⇒ Object

Search based on GUID, DN or Username primarily



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 124

def find_activedirectory_objs local_params = {}
  #Sometimes we're provide the objects
  if local_params.key? :object
    return [local_params[:object]] unless local_params[:object].kind_of? Array
    return local_params[:object]
  end

  #Reverse mappings for user
  ad_params = local_attrs_to_ad(local_params)

  activedirectory_class.find(:all, ad_params)
end

#find_from_activedirectory(local_params = {}) ⇒ Object



137
138
139
140
141
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 137

def find_from_activedirectory local_params = {}
  ad_objs = find_activedirectory_objs local_params
  guids = ad_objs.collect { |obj| obj[:objectguid] }
  scoped.where(:objectguid => guids)
end

#find_or_create_from_activedirectory(local_params = {}) ⇒ Object

Does a search using AD terms and either finds the corresponding object in the database, or creates it TODO change attributes to not be statically mapped to objectguid



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 147

def find_or_create_from_activedirectory local_params = {}
  ad_objs = find_activedirectory_objs local_params
  local_objs = []

  #Grab all of the objects in one query by GUID for efficiency
  guids = ad_objs.collect { |obj| obj[:objectguid] }
  db_objs_by_guid = {}

  #Make a hash map to do quick lookups
  scoped.where(:objectguid => guids).each do |db_obj|
    db_objs_by_guid[db_obj.objectguid] = db_obj
  end

  ad_objs.each do |ad_obj|
    guid = ad_obj[:objectguid]
    obj = db_objs_by_guid[guid] || new
    obj.copy_from_activedirectory!(:object => ad_obj) if obj.new_record?

    local_objs << obj
  end

  local_objs
end

#login_withObject



85
86
87
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 85

def 
  ::Devise.authentication_keys.first
end

#set_activedirectory_credentials(params = {}) ⇒ Object

Sets the username and password for the connection params {:username => ‘joe.user’, :password => ‘top_secret’ }



197
198
199
200
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 197

def set_activedirectory_credentials(params = {})
  #Used for username and password only
  ::Devise.ad_settings[:auth].merge! params
end

#set_devise_ad_options(field, params = {}) ⇒ Object



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

def set_devise_ad_options field, params = {}
  ret = {}
  ret[:field] = field.to_s
  ret[:class_name] = (params[:class_name] || field).to_s.classify
  ret[:class] = Kernel.const_get(ret[:class_name])

  unless ret[:class].include? AdObject
    raise "#{ret[:class_name]} does not include any of the Devise Active Directory modules.  Please consult the documentation." 
  end

  return ret
end

#sync_allObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 171

def sync_all
  return false unless connected_to_activedirectory?

  db_objs = find_or_create_from_activedirectory

  ActiveRecord::Base.transaction do
    #Save the new ones
    db_objs.each { |obj| obj.save if obj.new_record? }

    #Then update the memberships
    #If we're updating all of them, then updating just the parents will do
    db_objs.each do |obj| 
      obj.update_parents
    end
  end
end