<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>generators/authenticated/templates/mailer.rb</filename>
    </added>
    <added>
      <filename>generators/authenticated/templates/mailer_test.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -17,7 +17,12 @@ You can pass --skip-migration to skip the user migration.
 
 From here, you will need to add the resource routes in config/routes.rb.  
 
-  map.resources :users, :sessions
+  map.resources :users
+  map.resource  :session
+
+If you're on rails 1.2.3 you may need to specify the controller name for the session singular resource:
+
+  map.resource :session, :controller =&gt; 'sessions'
 
 Also, add an observer to config/environment.rb if you chose the --include-activation option
   config.active_record.observers = :user_observer # or whatever you named your model</diff>
      <filename>README</filename>
    </modified>
    <modified>
      <diff>@@ -54,7 +54,7 @@ class AuthenticatedGenerator &lt; Rails::Generator::NamedBase
                                                       &quot;#{controller_class_name}Helper&quot;
       m.class_collisions model_controller_class_path, &quot;#{model_controller_class_name}Controller&quot;, # Model Controller
                                                       &quot;#{model_controller_class_name}Helper&quot;
-      m.class_collisions class_path,                  &quot;#{class_name}&quot;, &quot;#{class_name}Notifier&quot;, &quot;#{class_name}NotifierTest&quot;, &quot;#{class_name}Observer&quot;
+      m.class_collisions class_path,                  &quot;#{class_name}&quot;, &quot;#{class_name}Mailer&quot;, &quot;#{class_name}MailerTest&quot;, &quot;#{class_name}Observer&quot;
       m.class_collisions [], 'AuthenticatedSystem', 'AuthenticatedTestHelper'
 
       # Controller, helper, views, and test directories.
@@ -63,7 +63,7 @@ class AuthenticatedGenerator &lt; Rails::Generator::NamedBase
       m.directory File.join('app/controllers', model_controller_class_path)
       m.directory File.join('app/helpers', controller_class_path)
       m.directory File.join('app/views', controller_class_path, controller_file_name)
-      m.directory File.join('app/views', class_path, &quot;#{file_name}_notifier&quot;)
+      m.directory File.join('app/views', class_path, &quot;#{file_name}_mailer&quot;) if options[:include_activation]
       m.directory File.join('test/functional', controller_class_path)
       m.directory File.join('app/controllers', model_controller_class_path)
       m.directory File.join('app/helpers', model_controller_class_path)
@@ -77,7 +77,7 @@ class AuthenticatedGenerator &lt; Rails::Generator::NamedBase
                             &quot;#{file_name}.rb&quot;)
 
       if options[:include_activation]
-        %w( notifier observer ).each do |model_type|
+        %w( mailer observer ).each do |model_type|
           m.template &quot;#{model_type}.rb&quot;, File.join('app/models',
                                                class_path,
                                                &quot;#{file_name}_#{model_type}.rb&quot;)
@@ -126,7 +126,7 @@ class AuthenticatedGenerator &lt; Rails::Generator::NamedBase
                             &quot;#{file_name}_test.rb&quot;)
 
       if options[:include_activation]
-        m.template 'notifier_test.rb', File.join('test/unit', class_path, &quot;#{file_name}_notifier_test.rb&quot;)
+        m.template 'mailer_test.rb', File.join('test/unit', class_path, &quot;#{file_name}_mailer_test.rb&quot;)
       end
 
       m.template 'fixtures.yml',
@@ -141,7 +141,7 @@ class AuthenticatedGenerator &lt; Rails::Generator::NamedBase
         # Mailer templates
         %w( activation signup_notification ).each do |action|
           m.template &quot;#{action}.rhtml&quot;,
-                     File.join('app/views', &quot;#{file_name}_notifier&quot;, &quot;#{action}.rhtml&quot;)
+                     File.join('app/views', &quot;#{file_name}_mailer&quot;, &quot;#{action}.rhtml&quot;)
         end
       end
 
@@ -161,9 +161,14 @@ class AuthenticatedGenerator &lt; Rails::Generator::NamedBase
         puts &quot;Don't forget to:&quot;
         puts
         puts &quot;  - add restful routes in config/routes.rb&quot;
-        puts &quot;    map.resources :#{model_controller_file_name}, :#{controller_file_name}&quot;
-        puts &quot;    map.activate '/activate/:activation_code', :controller =&gt; '#{model_controller_file_name}', :action =&gt; 'activate'&quot;
+        puts &quot;    map.resources :#{model_controller_file_name}&quot;
+        puts &quot;    map.resource  :#{controller_singular_name.singularize}&quot;
+        puts
+        puts &quot; Rails 1.2.3 may need a :controller option for the singular resource:&quot;
+        puts &quot;  - map.resource :#{controller_singular_name.singularize}, :controller =&gt; '#{controller_file_name}'&quot;
+        puts
         if options[:include_activation]
+          puts &quot;    map.activate '/activate/:activation_code', :controller =&gt; '#{model_controller_file_name}', :action =&gt; 'activate'&quot;
           puts
           puts &quot;  - add an observer to config/environment.rb&quot;
           puts &quot;    config.active_record.observers = :#{file_name}_observer&quot;</diff>
      <filename>generators/authenticated/authenticated_generator.rb</filename>
    </modified>
    <modified>
      <diff>@@ -6,9 +6,10 @@ module AuthenticatedSystem
       current_&lt;%= file_name %&gt; != :false
     end
     
-    # Accesses the current &lt;%= file_name %&gt; from the session.
+    # Accesses the current &lt;%= file_name %&gt; from the session.  Set it to :false if login fails
+    # so that future calls do not hit the database.
     def current_&lt;%= file_name %&gt;
-      @current_&lt;%= file_name %&gt; ||= (session[:&lt;%= file_name %&gt;] &amp;&amp; &lt;%= class_name %&gt;.find_by_id(session[:&lt;%= file_name %&gt;])) || :false
+      @current_user ||= (login_from_session || login_from_basic_auth || login_from_cookie || :false)
     end
     
     # Store the given &lt;%= file_name %&gt; in the session.
@@ -17,7 +18,7 @@ module AuthenticatedSystem
       @current_&lt;%= file_name %&gt; = new_&lt;%= file_name %&gt;
     end
     
-    # Check if the &lt;%= file_name %&gt; is authorized.
+    # Check if the &lt;%= file_name %&gt; is authorized
     #
     # Override this method in your controllers if you want to restrict access
     # to only a few actions or if you want to check if the &lt;%= file_name %&gt;
@@ -26,11 +27,11 @@ module AuthenticatedSystem
     # Example:
     #
     #  # only allow nonbobs
-    #  def authorize?
+    #  def authorized?
     #    current_&lt;%= file_name %&gt;.login != &quot;bob&quot;
     #  end
     def authorized?
-      true
+      logged_in?
     end
 
     # Filter method to enforce a login requirement.
@@ -48,11 +49,9 @@ module AuthenticatedSystem
     #   skip_before_filter :login_required
     #
     def login_required
-      username, passwd = get_auth_data
-      self.current_&lt;%= file_name %&gt; ||= &lt;%= class_name %&gt;.authenticate(username, passwd) || :false if username &amp;&amp; passwd
-      logged_in? &amp;&amp; authorized? ? true : access_denied
+      authorized? ? true : access_denied
     end
-    
+
     # Redirect as appropriate when an access request fails.
     #
     # The default action is to redirect to the login screen.
@@ -65,7 +64,7 @@ module AuthenticatedSystem
       respond_to do |accepts|
         accepts.html do
           store_location
-          redirect_to :controller =&gt; '&lt;%= controller_file_name %&gt;', :action =&gt; 'new'
+          redirect_to :controller =&gt; '/&lt;%= controller_file_name %&gt;', :action =&gt; 'login'
         end
         accepts.xml do
           headers[&quot;Status&quot;]           = &quot;Unauthorized&quot;
@@ -96,16 +95,24 @@ module AuthenticatedSystem
       base.send :helper_method, :current_&lt;%= file_name %&gt;, :logged_in?
     end
 
-    # When called with before_filter :login_from_cookie will check for an :auth_token
-    # cookie and log the user back in if apropriate
-    def login_from_cookie
-      return unless cookies[:auth_token] &amp;&amp; !logged_in?
-      user = &lt;%= class_name %&gt;.find_by_remember_token(cookies[:auth_token])
-      if user &amp;&amp; user.remember_token?
-        user.remember_me
-        self.current_&lt;%= file_name %&gt; = user
-        cookies[:auth_token] = { :value =&gt; self.current_&lt;%= file_name %&gt;.remember_token , :expires =&gt; self.current_&lt;%= file_name %&gt;.remember_token_expires_at }
-        flash[:notice] = &quot;Logged in successfully&quot;
+    # Called from #current_user.  First attempt to login by the user id stored in the session.
+    def login_from_session
+      self.current_&lt;%= file_name %&gt; = &lt;%= class_name %&gt;.find_by_id(session[:&lt;%= file_name %&gt;]) if session[:&lt;%= file_name %&gt;]
+    end
+
+    # Called from #current_user.  Now, attempt to login by basic authentication information.
+    def login_from_basic_auth
+      username, passwd = get_auth_data
+      self.current_&lt;%= file_name %&gt; = &lt;%= class_name %&gt;.authenticate(username, passwd) if username &amp;&amp; passwd
+    end
+
+    # Called from #current_user.  Finaly, attempt to login by an expiring token in the cookie.
+    def login_from_cookie      
+      &lt;%= file_name %&gt; = cookies[:auth_token] &amp;&amp; &lt;%= class_name %&gt;.find_by_remember_token(cookies[:auth_token])
+      if &lt;%= file_name %&gt; &amp;&amp; &lt;%= file_name %&gt;.remember_token?
+        &lt;%= file_name %&gt;.remember_me
+        cookies[:auth_token] = { :value =&gt; &lt;%= file_name %&gt;.remember_token, :expires =&gt; &lt;%= file_name %&gt;.remember_token_expires_at }
+        self.current_&lt;%= file_name %&gt; = &lt;%= file_name %&gt;
       end
     end
 </diff>
      <filename>generators/authenticated/templates/authenticated_system.rb</filename>
    </modified>
    <modified>
      <diff>@@ -7,4 +7,20 @@ module AuthenticatedTestHelper
   def authorize_as(user)
     @request.env[&quot;HTTP_AUTHORIZATION&quot;] = user ? &quot;Basic #{Base64.encode64(&quot;#{users(user).login}:test&quot;)}&quot; : nil
   end
+
+  # taken from edge rails / rails 2.0.  Only needed on Rails 1.2.3
+  def assert_difference(expressions, difference = 1, message = nil, &amp;block)
+    expression_evaluations = [expressions].flatten.collect{|expression| lambda { eval(expression, block.binding) } } 
+    
+    original_values = expression_evaluations.inject([]) { |memo, expression| memo &lt;&lt; expression.call }
+    yield
+    expression_evaluations.each_with_index do |expression, i|
+      assert_equal original_values[i] + difference, expression.call, message
+    end
+  end
+
+  # taken from edge rails / rails 2.0.  Only needed on Rails 1.2.3
+  def assert_no_difference(expressions, message = nil, &amp;block)
+    assert_difference expressions, 0, message, &amp;block
+  end
 end
\ No newline at end of file</diff>
      <filename>generators/authenticated/templates/authenticated_test_helper.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,8 +2,6 @@
 class &lt;%= controller_class_name %&gt;Controller &lt; ApplicationController
   # Be sure to include AuthenticationSystem in Application Controller instead
   include AuthenticatedSystem
-  # If you want &quot;remember me&quot; functionality, add this before_filter to Application Controller
-  before_filter :login_from_cookie
 
   # render new.rhtml
   def new</diff>
      <filename>generators/authenticated/templates/controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,4 @@
-&lt;%% form_tag &lt;%= controller_plural_name %&gt;_path do -%&gt;
+&lt;%% form_tag &lt;%= controller_singular_name.singularize %&gt;_path do -%&gt;
 &lt;p&gt;&lt;label for=&quot;login&quot;&gt;Login&lt;/label&gt;&lt;br/&gt;
 &lt;%%= text_field_tag 'login' %&gt;&lt;/p&gt;
 </diff>
      <filename>generators/authenticated/templates/login.rhtml</filename>
    </modified>
    <modified>
      <diff>@@ -12,7 +12,7 @@ class &lt;%= class_name %&gt; &lt; ActiveRecord::Base
   validates_length_of       :email,    :within =&gt; 3..100
   validates_uniqueness_of   :login, :email, :case_sensitive =&gt; false
   before_save :encrypt_password
-  &lt;% if options[:include_activation] %&gt; before_create :make_activation_code &lt;% end %&gt;
+  &lt;% if options[:include_activation] %&gt;before_create :make_activation_code &lt;% end %&gt;
   &lt;% if options[:include_activation] %&gt;
   # Activates the user in the database.
   def activate
@@ -22,13 +22,15 @@ class &lt;%= class_name %&gt; &lt; ActiveRecord::Base
   end
 
   def activated?
+    # the existence of an activation code means they have not activated yet
     activation_code.nil?
   end
 
   # Returns true if the user has just been activated.
   def recently_activated?
     @activated
-  end &lt;% end %&gt;
+  end
+&lt;% end %&gt;
   # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
   def self.authenticate(login, password)
     u = &lt;% if options[:include_activation] %&gt;find :first, :conditions =&gt; ['login = ? and activated_at IS NOT NULL', login]&lt;% else %&gt;find_by_login(login)&lt;% end %&gt; # need to get the salt
@@ -85,7 +87,6 @@ class &lt;%= class_name %&gt; &lt; ActiveRecord::Base
     def password_required?
       crypted_password.blank? || !password.blank?
     end
-
     &lt;% if options[:include_activation] %&gt;
     def make_activation_code
       self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )</diff>
      <filename>generators/authenticated/templates/model.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,8 +1,6 @@
 class &lt;%= model_controller_class_name %&gt;Controller &lt; ApplicationController
   # Be sure to include AuthenticationSystem in Application Controller instead
   include AuthenticatedSystem
-  # If you want &quot;remember me&quot; functionality, add this before_filter to Application Controller
-  before_filter :login_from_cookie
 
   # render new.rhtml
   def new</diff>
      <filename>generators/authenticated/templates/model_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,11 +1,11 @@
 class &lt;%= class_name %&gt;Observer &lt; ActiveRecord::Observer
   def after_create(&lt;%= file_name %&gt;)
-    &lt;%= class_name %&gt;Notifier.deliver_signup_notification(&lt;%= file_name %&gt;)
+    &lt;%= class_name %&gt;Mailer.deliver_signup_notification(&lt;%= file_name %&gt;)
   end
 
   def after_save(&lt;%= file_name %&gt;)
   &lt;% if options[:include_activation] %&gt;
-    &lt;%= class_name %&gt;Notifier.deliver_activation(&lt;%= file_name %&gt;) if &lt;%= file_name %&gt;.recently_activated?
+    &lt;%= class_name %&gt;Mailer.deliver_activation(&lt;%= file_name %&gt;) if &lt;%= file_name %&gt;.recently_activated?
   &lt;% end %&gt;
   end
 end</diff>
      <filename>generators/authenticated/templates/observer.rb</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>generators/authenticated/templates/notifier.rb</filename>
    </removed>
    <removed>
      <filename>generators/authenticated/templates/notifier_test.rb</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>eaf17ee3274bd2ace65f3a585718095f0a23ed17</id>
    </parent>
  </parents>
  <author>
    <name>technoweenie</name>
    <email>technoweenie@567b1171-46fb-0310-a4c9-b4bef9110e78</email>
  </author>
  <url>http://github.com/technoweenie/restful-authentication/commit/f1c88f3de29f72d3a090cc67f7f54d6283e7932f</url>
  <id>f1c88f3de29f72d3a090cc67f7f54d6283e7932f</id>
  <committed-date>2007-08-13T06:45:48-07:00</committed-date>
  <authored-date>2007-08-13T06:45:48-07:00</authored-date>
  <message>restructure login code so basic authentication works.  rename notifiers to mailers.  use singleton resources for Sessions

git-svn-id: http://svn.techno-weenie.net/projects/plugins/restful_authentication@2943 567b1171-46fb-0310-a4c9-b4bef9110e78</message>
  <tree>ad9f08270f8488c9e122f719f9e0d52014f7a16c</tree>
  <committer>
    <name>technoweenie</name>
    <email>technoweenie@567b1171-46fb-0310-a4c9-b4bef9110e78</email>
  </committer>
</commit>
