Module: Devise::AdObject

Extended by:
ActiveSupport::Concern
Included in:
Models::AdGroup, Models::AdUser
Defined in:
lib/devise_active_directory_authenticatable/models/ad_object.rb

Overview

Basic functions and shared methods for AD objects in ActiveRecord

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

Logger =
DeviseActiveDirectoryAuthenticatable::Logger

Instance Method Summary collapse

Instance Method Details

#ad_objObject



16
17
18
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 16

def ad_obj
  @ad_obj ||= klass.find_activedirectory_objs(:objectguid => self.objectguid).first
end

#attr_mapObject



8
9
10
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 8

def attr_map
  @attr_map ||= ::Devise.ad_attr_mapping[klass.devise_model_name.to_sym]
end

#copy_from_activedirectory!(params = {}) ⇒ Object

Update the attributes of the current object from the AD Defaults to current user if no parameters given



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 35

def copy_from_activedirectory! params = {}
  #Allows us to change what ad object is bound to this user
  @ad_obj = klass.find_activedirectory_objs(params).first unless params.empty?

  unless @ad_obj.nil?
    attr_map.each do |local_attr, active_directory_attr|
      self[local_attr] = @ad_obj[active_directory_attr]
    end

    update_memberships
  end
end

#klassObject



12
13
14
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 12

def klass
  self.class
end

#update_childrenObject



57
58
59
60
61
62
63
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 57

def update_children
  #Set the members
  if ad_obj[:member].is_a? Array
    update_membership(ad_obj[:member], klass.member_users) if defined? klass.member_users
    update_membership(ad_obj[:member], klass.member_groups) if defined? klass.member_groups
  end
end

#update_membership(ad_objs, params) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 72

def update_membership ad_objs, params
  # Create the objects of the right type, then sets them
  klass = params[:class]
  field = params[:field]

  ad_objs = klass.find_from_activedirectory(:object => ad_objs)
  self.send "#{field}=", ad_objs
end

#update_membershipsObject

Updates the members and memberofs stored in the database and by update, I mean overwrite TODO find a way to update, or at least not molest non-AD associations



52
53
54
55
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 52

def update_memberships
  update_children
  update_parents
end

#update_parentsObject



65
66
67
68
69
70
# File 'lib/devise_active_directory_authenticatable/models/ad_object.rb', line 65

def update_parents
  # MemberOf Relationship
  if ad_obj[:memberof].is_a? Array and defined? klass.memberof
    update_membership(ad_obj[:memberof], klass.memberof)
  end
end