lib/adauth/user.rb in adauth-1.1.0 vs lib/adauth/user.rb in adauth-1.2.0

- old
+ new

@@ -12,41 +12,49 @@ :last_name => :sn, :email => :mail, :name => :name } - # Multi values were the method needs to return an array for values. + # Multi values where the method needs to return an array for values. ATTR_MV = { :groups => [ :memberof, Proc.new {|g| g.sub(/.*?CN=(.*?),.*/, '\1')} ], :ous => [ :memberof, - Proc.new {|g| g.sub(/.*?OU=(.*?),.*/, '\1')} ] + Proc.new {|g| g.scan(/OU=.*?,/).map { |e| e.sub!(/OU=/,'').sub(/,/,'') } } ] } # Authenticates a user against Active Directory and returns an instance of self # # Called as: # Adauth::User.authenticate("username", "password") # # Usage would by-pass Adauths group filtering. def self.authenticate(login, pass) return nil if login.empty? or pass.empty? - conn = Net::LDAP.new :host => Adauth.config.server, - :port => Adauth.config.port, - :base => Adauth.config.base, - :auth => { :username => "#{login}@#{Adauth.config.domain}", - :password => pass, - :method => :simple } - if conn.bind and user = conn.search(:filter => Net::LDAP::Filter.eq('sAMAccountName', login)).first + conn = Adauth::Connection.bind(login, pass) + if conn and user = conn.search(:filter => Net::LDAP::Filter.eq('sAMAccountName', login)).first return self.new(user) else return nil end rescue Net::LDAP::LdapError => e return nil end + # Create a Adauth::User object from AD using just the username + # + # Called as: + # Adauth::User.create_from_login(login) + # + # Allows you to create objects for users without using thier password. + def self.create_from_login(login) + conn = Adauth::AdminConnection.bind + user = conn.search(:filter => Net::LDAP::Filter.eq('sAMAccountName', login)).first + obj = self.new(user) + return obj + end + # Returns the full name of the user # # Combines the first_name and last_name attributes to create full_name def full_name self.first_name + ' ' + self.last_name @@ -88,10 +96,12 @@ ATTR_MV.merge(Adauth.config.ad_mv_attrs).each_pair do |k, v| val, block = Array(v) define_method(k) do if @entry.attribute_names.include?(val) if block.is_a?(Proc) - return @entry.send(val).collect(&block) + output = @entry.send(val).collect(&block) + output = output.first if output.first.is_a? Array + return output else return @entry.send(val) end else return [] \ No newline at end of file