[Git][noosfero/noosfero][master] ldap: refactor authentication method

Victor Costa gitlab at mg.gitlab.com
Fri Dec 4 14:21:05 BRST 2015


Victor Costa pushed to branch master at Noosfero / noosfero


Commits:
ce49e588 by Victor Costa at 2015-12-04T13:15:00Z
ldap: refactor authentication method

- - - - -


1 changed file:

- plugins/ldap/lib/ldap_plugin.rb


Changes:

=====================================
plugins/ldap/lib/ldap_plugin.rb
=====================================
--- a/plugins/ldap/lib/ldap_plugin.rb
+++ b/plugins/ldap/lib/ldap_plugin.rb
@@ -44,51 +44,37 @@ class LdapPlugin < Noosfero::Plugin
     password = context.params[:user][:password]
     ldap = LdapAuthentication.new(context.environment.ldap_plugin_attributes)
 
-    user = User.find_or_initialize_by_login(login)
-
-    if user.new_record?
-      # user is not yet registered, try to authenticate
-      begin
-        attrs = ldap.authenticate(login, password)
-      rescue Net::LDAP::LdapError => e
-        puts "LDAP is not configured correctly"
-      end
+    # try to authenticate
+    begin
+      attrs = ldap.authenticate(login, password)
+    rescue Net::LDAP::LdapError => e
+      puts "LDAP is not configured correctly"
+    end
+    return nil if attrs.nil?
 
-      if attrs
-        user.login = get_login(attrs, ldap.attr_login, login)
-        user.email = get_email(attrs, login)
-        user.name =  attrs[:fullname]
-        user.password = password
-        user.password_confirmation = password
-        user.person_data = plugins.pipeline(:ldap_plugin_set_profile_data, attrs, context.params).last[:profile_data]
-        user.activated_at = Time.now.utc
-        user.activation_code = nil
-
-        ldap = LdapAuthentication.new(context.environment.ldap_plugin_attributes)
-        begin
-          if user.save
-            user.activate
-            plugins.dispatch(:ldap_plugin_update_user, user, attrs)
-          else
-            user = nil
-          end
-        rescue
-          #User not saved
-        end
-      else
-        user = nil
-      end
+    user_login = get_login(attrs, ldap.attr_login, login)
+    user = User.find_or_initialize_by_login(user_login)
+    return nil if !user.new_record? && !user.activated?
 
-    else
-      return nil if !user.activated?
+    user.login = user_login
+    user.email = get_email(attrs, login)
+    user.name =  attrs[:fullname]
+    user.password = password
+    user.password_confirmation = password
+    user.person_data = plugins.pipeline(:ldap_plugin_set_profile_data, attrs, context.params).last[:profile_data]
+    user.activated_at = Time.now.utc
+    user.activation_code = nil
 
-      begin
-        # user is defined as nil if ldap authentication failed
-        user = nil if ldap.authenticate(login, password).nil?
-      rescue Net::LDAP::LdapError => e
+    ldap = LdapAuthentication.new(context.environment.ldap_plugin_attributes)
+    begin
+      if user.save
+        user.activate
+        plugins.dispatch(:ldap_plugin_update_user, user, attrs)
+      else
         user = nil
-        puts "LDAP is not configured correctly"
       end
+    rescue
+      #User not saved
     end
 
     user



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/ce49e588d9f664e896b4596850675a34eeae3457
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20151204/42b5df3c/attachment.html>


More information about the Noosfero-dev mailing list