Module: ADMapper::User::InstanceMethods

Defined in:
lib/admapper/user.rb

Instance Method Summary collapse

Instance Method Details

#ad_mapObject

Default mapping of user object to active directory. You will most likely want to implement this in your own class instead of using this very basic default. Simply map your model (keys of the hash) to ActiveDirectory (values of the hash)

def ad_map
  {
    :username => :samaccountname,
    :full_name => :displayname
  }
end


103
104
105
# File 'lib/admapper/user.rb', line 103

def ad_map
  {:username => :samaccountname}
end

#find_in_ad(options = {:key => :username}) ⇒ Object

finds a user in active directory using the internal key. Defaults to username



75
76
77
78
79
80
81
# File 'lib/admapper/user.rb', line 75

def find_in_ad(options = {:key => :username})
  username = send(options[:key])
  ad_user = self.class.ad_query_by_username(username)
  return nil if ad_user.nil?
  self.map_user_from_ad(ad_user)
  true
end

#groupsObject

Returns the groups for a user.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/admapper/user.rb', line 17

def groups
  groups = []
  
   search_filter = Net::LDAP::Filter.eq("sAMAccountName", self.ad_user.samaccountname.first)
      ad_connection = ADMapper::Connection.current_connection
      ad_connection.search(:base => ADMapper::Connection.treebase,
                             :filter => search_filter) do |entry|

                               entry.each do |attribute, values|      
                               if attribute.to_s.match(/memberof/)
                                    values.each do |value|
                                      a = value.split(',')
                                      md = a[0].match(/CN=(.+)/)

                                      groups << md[1]
                                    end
                                  end
                               end
   end
   
   groups.collect do |g|
     self.class.group_class.find_in_ad_by_name(g)
   end.compact
   
   
end

#map_user_from_ad(ad_user) ⇒ Object

maps the ad_user’s attributes to your class’ attributes . Implement the ad_map method in your own class to control how fields map.



85
86
87
88
89
90
# File 'lib/admapper/user.rb', line 85

def map_user_from_ad(ad_user)
   self.ad_map.each do |user_object_field, ad_object_field|           
     self.send("#{user_object_field}=", ad_user.send(ad_object_field).to_s)
  end
  self.ad_user = ad_user
end

#member_of?(group) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/admapper/user.rb', line 46

def member_of?(group)
  group_member = false
  
  search_filter = Net::LDAP::Filter.eq("sAMAccountName", self.ad_user.samaccountname.first)
     ad_connection = ADMapper::Connection.current_connection
     ad_connection.search(:base => ADMapper::Connection.treebase,
                            :filter => search_filter) do |entry|
                              
                              entry.each do |attribute, values|      
                              if attribute.to_s.match(/memberof/)
                                   values.each do |value|
                                     a = value.split(',')
                                     md = a[0].match(/CN=(.+)/)

                                     # user is a member of the right group
                                     if md[1] == group
                                       group_member = true
                                     end
                                   end
                                 end
                              end
  end
  group_member
  
end