[Git][noosfero/noosfero][master] ldap: accept multiple fields in login attribute

Victor Costa gitlab at mg.gitlab.com
Thu Dec 3 16:56:41 BRST 2015


Victor Costa pushed to branch master at Noosfero / noosfero


Commits:
b256dff9 by Victor Costa at 2015-12-03T15:55:27Z
ldap: accept multiple fields in login attribute

- - - - -


5 changed files:

- plugins/ldap/Gemfile
- − plugins/ldap/dependencies.rb
- plugins/ldap/lib/ldap_authentication.rb
- plugins/ldap/lib/ldap_plugin.rb
- plugins/ldap/test/unit/ldap_plugin_test.rb


Changes:

=====================================
plugins/ldap/Gemfile
=====================================
--- a/plugins/ldap/Gemfile
+++ b/plugins/ldap/Gemfile
@@ -1,2 +1,2 @@
-gem "net-ldap"
+gem "net-ldap", "~> 0.12.1"
 gem "magic", ">= 0.2.8"


=====================================
plugins/ldap/dependencies.rb deleted
=====================================
--- a/plugins/ldap/dependencies.rb
+++ /dev/null
@@ -1 +0,0 @@
-require 'net/ldap'


=====================================
plugins/ldap/lib/ldap_authentication.rb
=====================================
--- a/plugins/ldap/lib/ldap_authentication.rb
+++ b/plugins/ldap/lib/ldap_authentication.rb
@@ -15,7 +15,6 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
-require 'iconv'
 require 'net/ldap'
 require 'net/ldap/dn'
 require 'magic'
@@ -111,7 +110,14 @@ class LdapAuthentication
     else
       ldap_con = initialize_ldap_con(self.account, self.account_password)
     end
-    login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
+    login_filter = nil
+    (self.attr_login || []).split.each do |attr|
+      if(login_filter.nil?)
+        login_filter = Net::LDAP::Filter.eq( attr, login )
+      else
+        login_filter = login_filter | Net::LDAP::Filter.eq( attr, login )
+      end
+    end
     object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
 
     attrs = {}


=====================================
plugins/ldap/lib/ldap_plugin.rb
=====================================
--- a/plugins/ldap/lib/ldap_plugin.rb
+++ b/plugins/ldap/lib/ldap_plugin.rb
@@ -55,7 +55,7 @@ class LdapPlugin < Noosfero::Plugin
       end
 
       if attrs
-        user.login = login
+        user.login = get_login(attrs, ldap.attr_login, login)
         user.email = get_email(attrs, login)
         user.name =  attrs[:fullname]
         user.password = password
@@ -94,6 +94,11 @@ class LdapPlugin < Noosfero::Plugin
     user
   end
 
+  def get_login(attrs, attr_login, login)
+    user_login = Array.wrap(attrs[attr_login.split.first.to_sym])
+    user_login.empty? ? login : user_login.first
+  end
+
   def get_email(attrs, login)
     return attrs[:mail] unless attrs[:mail].blank?
 


=====================================
plugins/ldap/test/unit/ldap_plugin_test.rb
=====================================
--- a/plugins/ldap/test/unit/ldap_plugin_test.rb
+++ b/plugins/ldap/test/unit/ldap_plugin_test.rb
@@ -14,4 +14,24 @@ class LdapPluginTest < ActiveSupport::TestCase
     refute plugin.allow_password_recovery
   end
 
+  should 'return login when exists a login attribute returned by ldap' do
+    plugin = LdapPlugin.new
+    assert_equal 'test', plugin.get_login({:uid => 'test'}, 'uid', 'test2')
+  end
+
+  should 'return the attribute configured by attr_login when the attribute exists' do
+    plugin = LdapPlugin.new
+    assert_equal 'test', plugin.get_login({:uid => 'test'}, 'uid', 'test2')
+  end
+
+  should 'return login when the ldap attribute does not exists' do
+    plugin = LdapPlugin.new
+    assert_equal 'test2', plugin.get_login({:uid => 'test'}, 'mail', 'test2')
+  end
+
+  should 'use the first word at attr_login as the login key' do
+    plugin = LdapPlugin.new
+    assert_equal 'test', plugin.get_login({:uid => 'test', :mail => 'test at test'}, 'uid mail', 'test2')
+  end
+
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/b256dff9b9bf92c1286fd8d041d09e1f085fdcb3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20151203/13e98b12/attachment-0001.html>


More information about the Noosfero-dev mailing list