Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6318

Show
Ignore:
Timestamp:
03/04/07 20:59:06 (2 years ago)
Author:
david
Message:

Stop relying on root_url being defined, we can just grab the current url instead [DHH] Cleaned up examples

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/open_id_authentication/lib/open_id_authentication.rb

    r6252 r6318  
    11module OpenIdAuthentication 
     2  OPEN_ID_AUTHENTICATION_DIR = RAILS_ROOT + "/tmp/openids" 
     3 
    24  protected 
    35    # OpenIDs are expected to begin with http:// or https:// 
     
    2224        yield :missing, identity_url, nil 
    2325      when OpenID::SUCCESS 
    24         open_id_response.add_extension_arg('sreg','required', [fields[:required]].flatten * ',') if fields[:required] 
    25         open_id_response.add_extension_arg('sreg','optional', [fields[:optional]].flatten * ',') if fields[:optional] 
    26         redirect_to(open_id_response.redirect_url( 
    27           root_url, open_id_response.return_to( 
    28             "#{request.protocol + request.host_with_port + request.request_uri}?open_id_complete=1" 
    29           ) 
    30         )) 
     26        add_simple_registration_fields(open_id_response, fields) 
     27        redirect_to(open_id_redirect_url(open_id_response)) 
    3128      end 
    3229    end 
     
    4744 
    4845    def open_id_consumer 
    49       OpenID::Consumer.new(session, OpenID::FilesystemStore.new(RAILS_ROOT + "/tmp/openids")) 
     46      OpenID::Consumer.new(session, OpenID::FilesystemStore.new(OPEN_ID_AUTHENTICATION_DIR)) 
     47    end 
     48 
     49 
     50    def add_simple_registration_fields(open_id_response, fields) 
     51      open_id_response.add_extension_arg('sreg', 'required', [ fields[:required] ].flatten * ',') if fields[:required] 
     52      open_id_response.add_extension_arg('sreg', 'optional', [ fields[:optional] ].flatten * ',') if fields[:optional] 
     53    end 
     54     
     55    def open_id_redirect_url(open_id_response) 
     56      open_id_response.redirect_url( 
     57        request.protocol + request.host, 
     58        open_id_response.return_to("#{request.url}?open_id_complete=1") 
     59      )      
    5060    end 
    5161end 
  • plugins/open_id_authentication/README

    r6252 r6318  
    1919This particular plugin also relies on the fact that the authentication action allows for both POST and GET operations. 
    2020If you're using RESTful authentication, you'll need to explicitly allow for this in your routes.rb.  
     21 
     22This plugin relies on Rails Edge revision 6317 or newer. 
    2123 
    2224 
     
    4749    protected 
    4850      def password_authentication(name, password) 
    49         if @current_user = @account.users.find_by_name_and_password(params[:name], params[:password]) 
     51        if @current_user = @account.users.authenticate(params[:name], params[:password]) 
    5052          successful_login 
    5153        else 
     
    8486        redirect_to(new_session_url) 
    8587      end 
    86        
    87       # Set #root_url if your root url has a different named route. 
    88       # 
    89       #   map.home '', :controller => ..., :action => ... 
    90       # 
    91       # Otherwise, name the route 'root' and leave this method out. 
    92       def root_url 
    93         home_url 
    94       end 
    9588  end 
     89 
    9690 
    9791Simple Registration OpenID Extension 
     
    10599        # Pass optional :required and :optional keys to specify what sreg fields you want. 
    106100        # Be sure to yield registration, a third argument in the #authenticate_with_open_id block. 
    107         authenticate_with_open_id(identity_url, :required => [:nickname, :email], :optional => :fullname) do |status, identity_url, registration| 
     101        authenticate_with_open_id(identity_url,  
     102            :required => [ :nickname, :email ], 
     103            :optional => :fullname) do |status, identity_url, registration| 
    108104          case status 
    109105          when :missing 
     
    115111          when :successful 
    116112            if @current_user = @account.users.find_by_identity_url(identity_url) 
    117               # registration is a hash containing the valid sreg keys given above 
    118               # use this to map them to fields of your user model 
    119               {'login=' => 'nickname', 'email=' => 'email', 'display_name=' => 'fullname'}.each do |attr, reg| 
    120                 current_user.send(attr, registration[reg]) unless registration[reg].blank? 
     113              assign_registration_attributes!(registration) 
     114 
     115              if current_user.save 
     116                successful_login 
     117              else 
     118                failed_login "Your OpenID profile registration failed: " + 
     119                  @current_user.errors.full_messages.to_sentence 
    121120              end 
    122               unless current_user.save 
    123                 flash[:error] = "Error saving the fields from your OpenID profile: #{current_user.errors.full_messages.to_sentence}" 
    124               end 
    125               successful_login 
    126121            else 
    127122              failed_login "Sorry, no user by that identity URL exists" 
     
    130125        end 
    131126      end 
     127       
     128      # registration is a hash containing the valid sreg keys given above 
     129      # use this to map them to fields of your user model 
     130      def assign_registration_attributes!(registration) 
     131        model_to_registration_mapping.each do |model_attribute, registration_attribute| 
     132          unless registration[registration_attribute].blank? 
     133            @current_user.send("#{model_attribute}=", registration[registration_attribute]) 
     134          end 
     135        end 
     136      end 
     137 
     138      def model_to_registration_mapping 
     139        { :login => 'nickname', :email => 'email', :display_name => 'fullname' } 
     140      end 
     141 
    132142 
    133143Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license