Skip to content

Commit

Permalink
Supports Auto Registration of users using the auto_register method in…
Browse files Browse the repository at this point in the history
… Session.

Fixes sreg support so it supports any amount of sreg fields.

Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information
pelle authored and binarylogic committed Jun 20, 2009
1 parent 9a95f15 commit 23c5f5d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
15 changes: 10 additions & 5 deletions lib/authlogic_openid/acts_as_authentic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def openid_identifier=(value)
# if their OpenID provider supports it.
def save(perform_validation = true, &block)
return false if perform_validation && block_given? && authenticate_with_openid? && !authenticate_with_openid

return false if new_record? && !openid_complete?
result = super
yield(result) if block_given?
result
Expand All @@ -90,7 +92,7 @@ def authenticate_with_openid
options = {}
options[:required] = self.class.openid_required_fields
options[:optional] = self.class.openid_optional_fields
options[:return_to] = session_class.controller.url_for(:for_model => "1")
options[:return_to] = session_class.controller.url_for(:for_model => "1",:controller=>"users",:action=>"create")

This comment has been minimized.

Copy link
@davidsmalley

davidsmalley Mar 9, 2010

Why is :controller=>"users",:action=>"create" hardcoded?

This comment has been minimized.

Copy link
@herestomwiththeweather

herestomwiththeweather May 12, 2010

ditto. this breaks applications that use people instead of users.


session_class.controller.send(:authenticate_with_open_id, openid_identifier, options) do |result, openid_identifier, registration|
if result.unsuccessful?
Expand All @@ -102,7 +104,6 @@ def authenticate_with_openid

return true
end

return false
end

Expand All @@ -112,9 +113,13 @@ def authenticate_with_openid
# 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?
registration.symbolize_keys!
[self.class.openid_required_fields+self.class.openid_optional_fields].flatten.each do |field|
setter="#{field.to_s}=".to_sym
if respond_to?(setter)
send setter,registration[field]
end
end
end

# This method works in conjunction with map_saved_attributes.
Expand Down
41 changes: 35 additions & 6 deletions lib/authlogic_openid/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def find_by_openid_identifier_method(value = nil)
rw_config(:find_by_openid_identifier_method, value, :find_by_openid_identifier)
end
alias_method :find_by_openid_identifier_method=, :find_by_openid_identifier_method

# Add this in your Session object to Auto Register a new user using openid via sreg
def auto_register(value=true)
auto_register_value(value)
end

def auto_register_value(value=nil)
rw_config(:auto_register,value,false)
end

alias_method :auto_register=,:auto_register
end

module Methods
Expand Down Expand Up @@ -67,24 +78,42 @@ def authenticating_with_openid?
attempted_record.nil? && errors.empty? && (!openid_identifier.blank? || (controller.params[:open_id_complete] && controller.params[:for_session]))
end

def find_by_openid_identifier_method
self.class.find_by_openid_identifier_method
end

def find_by_openid_identifier_method
self.class.find_by_openid_identifier_method
end

def auto_register?
self.class.auto_register_value
end

def validate_by_openid
self.remember_me = controller.params[:remember_me] == "true" if controller.params.key?(:remember_me)
self.attempted_record = klass.send(find_by_openid_identifier_method, openid_identifier)
if !attempted_record
if auto_register?
self.attempted_record = klass.new :openid_identifier=>openid_identifier
attempted_record.save do |result|
if result
true
else
false
end
end
else
errors.add(:openid_identifier, "did not match any users in our database, have you set up your account to use OpenID?")
end
return
end
controller.send(:authenticate_with_open_id, openid_identifier, :return_to => controller.url_for(:for_session => "1", :remember_me => remember_me?)) do |result, openid_identifier|
if result.unsuccessful?
errors.add_to_base(result.message)
return
end

self.attempted_record = klass.send(find_by_openid_identifier_method, openid_identifier)

if !attempted_record
errors.add(:openid_identifier, "did not match any users in our database, have you set up your account to use OpenID?")
return
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/authlogic_openid/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def to_a

MAJOR = 1
MINOR = 0
TINY = 4
TINY = 5

# The current version as a Version instance
CURRENT = new(MAJOR, MINOR, TINY)
Expand Down

3 comments on commit 23c5f5d

@skrat
Copy link

@skrat skrat commented on 23c5f5d Mar 17, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting validation errors, the one about password bugs me since it's not needed while using OpenID:

4 errors prohibited this user from being saved

There were problems with the following fields:

Login is too short (minimum is 3 characters)
Login should use only letters, numbers, spaces, and .-_@ please.
Password is too short (minimum is 4 characters)
Password confirmation is too short (minimum is 4 characters)

@mreinsch
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at my fork (http://github.com/mreinsch/authlogic_openid/), there is an alternative implementation based on patches by gaizka and cpjolicoeur.

Regarding the login, please have a look at the readme, step 5. The password field is already handled by authlogic_openid.

@mreinsch
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at my fork (http://github.com/mreinsch/authlogic_openid), I'm using an implementation based on patches by gaizka and cpjolicoeur. Regarding the login field, have a look at the readme, Step 5. The password field is already taken care of by authlogic_openid.

Please sign in to comment.