Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned / improved code

  • Loading branch information...
commit 83cb915ff89929ad491021b385e462a1dbee6fe7 1 parent 1bead0e
@binarylogic authored
Showing with 32 additions and 14 deletions.
  1. +32 −14 lib/authlogic_openid/acts_as_authentic.rb
View
46 lib/authlogic_openid/acts_as_authentic.rb
@@ -3,6 +3,9 @@ module ActsAsAuthentic
def self.included(klass)
klass.class_eval do
add_acts_as_authentic_module(Methods)
+ validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_openid?)
+ validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_openid?)
+ validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_openid?)
end
end
@@ -11,15 +14,17 @@ def self.included(klass)
klass.class_eval do
validates_uniqueness_of :openid_identifier, :scope => validations_scope, :if => :using_openid?
validate :validate_openid
- [:validates_length_of_login_field_options, :validates_format_of_login_field_options, :validates_length_of_password_field_options,
- :validates_confirmation_of_password_field_options, :validates_length_of_password_confirmation_field_options].each do |method|
- send(method, send(method).merge(:unless => :using_openid?))
- end
end
end
+ def openid_identifier=(value)
+ write_attribute(:openid_identifier, value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value))
+ reset_persistence_token if openid_identifier_changed?
+ rescue OpenIdAuthentication::InvalidOpenId => e
+ @openid_error = e.message
+ end
+
def save(*args, &block)
- normalize_openid_identifier if using_openid?
if !authenticate_with_openid? || (authenticate_with_openid? && authenticate_with_openid)
result = super
yield(result) if block_given?
@@ -30,13 +35,6 @@ def save(*args, &block)
end
private
- def normalize_openid_identifier
- begin
- self.openid_identifier = OpenIdAuthentication.normalize_identifier(openid_identifier)
- rescue Exception
- end
- end
-
def authenticate_with_openid
@openid_error = nil
@@ -53,14 +51,30 @@ def authenticate_with_openid
session_class.controller.session[:openid_attributes] = nil
end
- session_class.controller.send(:authenticate_with_open_id, openid_identifier, :return_to => session_class.controller.url_for(:for_model => "1")) do |result, openid_identifier|
- @openid_error = result.message if result.unsuccessful?
+ options = {}
+ options[:required_field] = [self.class.login_field, self.class.email_field].compact
+ options[:optional_fields] = [:fullname]
+ options[:return_to] = session_class.controller.url_for(:for_model => "1")
+
+ session_class.controller.send(:authenticate_with_open_id, openid_identifier, options) do |result, openid_identifier, registration|
+ if result.unsuccessful?
+ @openid_error = result.message
+ else
+ map_openid_fields(registration)
+ end
+
return true
end
return false
end
+ def map_openid_fields(registration)
+ self.name = registration[:fullname] if respond_to?(:name) && !registration[:fullname].blank?
+ self.first_name = registration[:fullname].split(" ").first if respond_to?(:first_name) && !registration[:fullname].blank?
+ self.first_name = registration[:fullname].split(" ").last if respond_to?(:last_name) && !registration[:last_name].blank?
+ end
+
def validate_openid
errors.add(:openid_identifier, "had the following error: #{@openid_error}") if @openid_error
end
@@ -76,6 +90,10 @@ def openid_complete?
def authenticate_with_openid?
session_class.activated? && ((using_openid? && openid_identifier_changed?) || openid_complete?)
end
+
+ def validate_password_with_openid?
+ !using_openid? && require_password?
+ end
end
end
end

0 comments on commit 83cb915

Please sign in to comment.
Something went wrong with that request. Please try again.