Commit ce49e588d9f664e896b4596850675a34eeae3457

Authored by Victor Costa
1 parent 0c7ed580

ldap: refactor authentication method

Showing 1 changed file with 26 additions and 40 deletions   Show diff stats
plugins/ldap/lib/ldap_plugin.rb
... ... @@ -44,51 +44,37 @@ class LdapPlugin < Noosfero::Plugin
44 44 password = context.params[:user][:password]
45 45 ldap = LdapAuthentication.new(context.environment.ldap_plugin_attributes)
46 46  
47   - user = User.find_or_initialize_by_login(login)
48   -
49   - if user.new_record?
50   - # user is not yet registered, try to authenticate
51   - begin
52   - attrs = ldap.authenticate(login, password)
53   - rescue Net::LDAP::LdapError => e
54   - puts "LDAP is not configured correctly"
55   - end
  47 + # try to authenticate
  48 + begin
  49 + attrs = ldap.authenticate(login, password)
  50 + rescue Net::LDAP::LdapError => e
  51 + puts "LDAP is not configured correctly"
  52 + end
  53 + return nil if attrs.nil?
56 54  
57   - if attrs
58   - user.login = get_login(attrs, ldap.attr_login, login)
59   - user.email = get_email(attrs, login)
60   - user.name = attrs[:fullname]
61   - user.password = password
62   - user.password_confirmation = password
63   - user.person_data = plugins.pipeline(:ldap_plugin_set_profile_data, attrs, context.params).last[:profile_data]
64   - user.activated_at = Time.now.utc
65   - user.activation_code = nil
66   -
67   - ldap = LdapAuthentication.new(context.environment.ldap_plugin_attributes)
68   - begin
69   - if user.save
70   - user.activate
71   - plugins.dispatch(:ldap_plugin_update_user, user, attrs)
72   - else
73   - user = nil
74   - end
75   - rescue
76   - #User not saved
77   - end
78   - else
79   - user = nil
80   - end
  55 + user_login = get_login(attrs, ldap.attr_login, login)
  56 + user = User.find_or_initialize_by_login(user_login)
  57 + return nil if !user.new_record? && !user.activated?
81 58  
82   - else
83   - return nil if !user.activated?
  59 + user.login = user_login
  60 + user.email = get_email(attrs, login)
  61 + user.name = attrs[:fullname]
  62 + user.password = password
  63 + user.password_confirmation = password
  64 + user.person_data = plugins.pipeline(:ldap_plugin_set_profile_data, attrs, context.params).last[:profile_data]
  65 + user.activated_at = Time.now.utc
  66 + user.activation_code = nil
84 67  
85   - begin
86   - # user is defined as nil if ldap authentication failed
87   - user = nil if ldap.authenticate(login, password).nil?
88   - rescue Net::LDAP::LdapError => e
  68 + ldap = LdapAuthentication.new(context.environment.ldap_plugin_attributes)
  69 + begin
  70 + if user.save
  71 + user.activate
  72 + plugins.dispatch(:ldap_plugin_update_user, user, attrs)
  73 + else
89 74 user = nil
90   - puts "LDAP is not configured correctly"
91 75 end
  76 + rescue
  77 + #User not saved
92 78 end
93 79  
94 80 user
... ...