Skip to content
Browse files

* Added required_fields and optional_fields config options for Authlo…

…gicOpenid::ActsAsAuthentic.

* Added map_openid_registration, attributes_to_save, and map_saved_attributes methods to customize how attributes are set for AuthlogicOpenid::ActsAsAuthentic.
  • Loading branch information...
1 parent bb54f09 commit 5dab3d0424b730dae835e3f0910c8dd1a2c46c64 @binarylogic binarylogic committed
Showing with 67 additions and 13 deletions.
  1. +4 −2 CHANGELOG.rdoc
  2. +63 −11 lib/authlogic_openid/acts_as_authentic.rb
View
6 CHANGELOG.rdoc
@@ -1,7 +1,9 @@
== 1.0.3
-* Added find_by_openid_identifier config option for the session.
-* Set the openid_identifier by the one passed back by the provider.
+* Added find_by_openid_identifier config option for AuthlogicOpenid::Session.
+* Set the openid_identifier by the one passed back by the provider in AuthlogicOpenid::ActsAsAuthentic.
+* Added required_fields and optional_fields config options for AuthlogicOpenid::ActsAsAuthentic.
+* Added map_openid_registration, attributes_to_save, and map_saved_attributes methods to customize how attributes are set for AuthlogicOpenid::ActsAsAuthentic.
== 1.0.2 released 2009-3-30
View
74 lib/authlogic_openid/acts_as_authentic.rb
@@ -8,10 +8,35 @@ module ActsAsAuthentic
# OpenID is being used.
def self.included(klass)
klass.class_eval do
+ extend Config
add_acts_as_authentic_module(Methods, :prepend)
end
end
+ module Config
+ # Some OpenID providers support a lightweight profile exchange protocol, for those that do, you can require
+ # certain fields. This is convenient for new registrations, as it will basically fill out the fields in the
+ # form for them, so they don't have to re-type information already stored with their OpenID account.
+ #
+ # For more info and what fields you can use see: http://openid.net/specs/openid-simple-registration-extension-1_0.html
+ #
+ # * <tt>Default:</tt> []
+ # * <tt>Accepts:</tt> Array of symbols
+ def required_fields(value = nil)
+ config(:required_fields, value, [])
+ end
+ alias_method :required_fields=, :required_fields
+
+ # Same as required_fields, but optional instead.
+ #
+ # * <tt>Default:</tt> []
+ # * <tt>Accepts:</tt> Array of symbols
+ def optional_fields(value = nil)
+ config(:optional_fields, value, [])
+ end
+ alias_method :optional_fields=, :optional_fields
+ end
+
module Methods
# Set up some simple validations
def self.included(klass)
@@ -57,21 +82,15 @@ def authenticate_with_openid
@openid_error = nil
if !openid_complete?
- attrs_to_persist = attributes.delete_if do |k, v|
- [:password, crypted_password_field, password_salt_field, :persistence_token, :perishable_token, :single_access_token, :login_count,
- :failed_login_count, :last_request_at, :current_login_at, :last_login_at, :current_login_ip, :last_login_ip, :created_at,
- :updated_at, :lock_version].include?(k.to_sym)
- end
- attrs_to_persist.merge!(:password => password, :password_confirmation => password_confirmation)
- session_class.controller.session[:openid_attributes] = attrs_to_persist
+ session_class.controller.session[:openid_attributes] = attributes_to_save
else
- self.attributes = session_class.controller.session[:openid_attributes]
+ map_saved_attributes(session_class.controller.session[:openid_attributes])
session_class.controller.session[:openid_attributes] = nil
end
options = {}
- options[:required_field] = [self.class.login_field, self.class.email_field].compact
- options[:optional_fields] = [:fullname]
+ options[:required] = self.class.required_fields
+ options[:optional] = self.class.optional_fields
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|
@@ -88,12 +107,45 @@ def authenticate_with_openid
return false
end
- def map_openid_registration(registration)
+ # Override this method to map the OpenID registration fields with fields in your model. See the required_fields and
+ # optional_fields configuration options to enable this feature.
+ #
+ # Basically you will get a hash of values passed as a single argument. Then just map them as you see fit. Check out
+ # the source of this method for an example.
+ def map_openid_registration(registration) # :doc:
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.last_name ||= registration[:fullname].split(" ").last if respond_to?(:last_name) && !registration[:last_name].blank?
end
+ # This method works in conjunction with map_saved_attributes.
+ #
+ # Let's say a user fills out a registration form, provides an OpenID and submits the form. They are then redirected to their
+ # OpenID provider. All is good and they are redirected back. All of those fields they spent time filling out are forgetten
+ # and they have to retype them all. To avoid this, AuthlogicOpenid saves all of these attributes in the session and then
+ # attempts to restore them. See the source for what attributes it saves. If you need to block more attributes, or save
+ # more just override this method and do whatever you want.
+ def attributes_to_save # :doc:
+ attrs_to_save = attributes.clone.delete_if do |k, v|
+ [:password, crypted_password_field, password_salt_field, :persistence_token, :perishable_token, :single_access_token, :login_count,
+ :failed_login_count, :last_request_at, :current_login_at, :last_login_at, :current_login_ip, :last_login_ip, :created_at,
+ :updated_at, :lock_version].include?(k.to_sym)
+ end
+ attrs_to_save.merge!(:password => password, :password_confirmation => password_confirmation)
+ end
+
+ # This method works in conjunction with attributes_to_save. See that method for a description of the why these methods exist.
+ #
+ # If the default behavior of this method is not sufficient for you because you have attr_protected or attr_accessible then
+ # override this method and set them individually. Maybe something like this would be good:
+ #
+ # attrs.each do |key, value|
+ # send("#{key}=", value)
+ # end
+ def map_saved_attributes(attrs) # :doc:
+ self.attributes = attrs
+ end
+
def validate_openid
errors.add(:openid_identifier, "had the following error: #{@openid_error}") if @openid_error
end

0 comments on commit 5dab3d0

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