Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added authlogic, and skeleton user model

  • Loading branch information...
commit ee439605b0a70b29aabba01afe5bfe99d32dad7c 1 parent e59224c
@mikel authored
Showing with 283 additions and 9 deletions.
  1. +3 −0  .gitmodules
  2. +41 −2 app/controllers/application_controller.rb
  3. +6 −0 app/controllers/home_controller.rb
  4. +24 −0 app/controllers/user_sessions_controller.rb
  5. +2 −0  app/helpers/home_helper.rb
  6. +2 −0  app/helpers/user_sessions_helper.rb
  7. +3 −0  app/models/user.rb
  8. +2 −0  app/models/user_session.rb
  9. +1 −0  app/views/home/index.html.erb
  10. +14 −0 app/views/user_sessions/new.html.erb
  11. 0  app/views/users/_form.html.erb
  12. +2 −0  config/environment.rb
  13. +2 −1  config/environments/development.rb
  14. +7 −1 config/environments/test.rb
  15. +4 −5 config/routes.rb
  16. +30 −0 db/migrate/20090502124658_create_users.rb
  17. +33 −0 db/schema.rb
  18. +34 −0 features/manage_sessions.feature
  19. +3 −0  features/step_definitions/sessions_steps.rb
  20. +3 −0  features/support/paths.rb
  21. +10 −0 spec/controllers/home_controller_spec.rb
  22. +10 −0 spec/controllers/user_sessions_controller_spec.rb
  23. +6 −0 spec/factories.rb
  24. +7 −0 spec/fixtures/users.yml
  25. +11 −0 spec/helpers/home_helper_spec.rb
  26. +11 −0 spec/helpers/user_sessions_helper_spec.rb
  27. +12 −0 spec/models/user_spec.rb
View
3  .gitmodules
@@ -0,0 +1,3 @@
+[submodule "vendor/rails"]
+ path = vendor/rails
+ url = git://github.com/rails/rails.git
View
43 app/controllers/application_controller.rb
@@ -5,6 +5,45 @@ class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
- # Scrub sensitive parameters from your log
- # filter_parameter_logging :password
+ helper_method :current_user_session, :current_user
+ filter_parameter_logging :password, :password_confirmation
+
+ private
+ def current_user_session
+ return @current_user_session if defined?(@current_user_session)
+ @current_user_session = UserSession.find
+ end
+
+ def current_user
+ return @current_user if defined?(@current_user)
+ @current_user = current_user_session && current_user_session.record
+ end
+
+ def require_user
+ unless current_user
+ store_location
+ flash[:notice] = "You must be logged in to access this page"
+ redirect_to new_user_session_url
+ return false
+ end
+ end
+
+ def require_no_user
+ if current_user
+ store_location
+ flash[:notice] = "You must be logged out to access this page"
+ redirect_to account_url
+ return false
+ end
+ end
+
+ def store_location
+ session[:return_to] = request.request_uri
+ end
+
+ def redirect_back_or_default(default)
+ redirect_to(session[:return_to] || default)
+ session[:return_to] = nil
+ end
end
+
View
6 app/controllers/home_controller.rb
@@ -0,0 +1,6 @@
+class HomeController < ApplicationController
+
+ def index
+ end
+
+end
View
24 app/controllers/user_sessions_controller.rb
@@ -0,0 +1,24 @@
+class UserSessionsController < ApplicationController
+ before_filter :require_no_user, :only => [:new, :create]
+ before_filter :require_user, :only => :destroy
+
+ def new
+ @user_session = UserSession.new
+ end
+
+ def create
+ @user_session = UserSession.new(params[:user_session])
+ if @user_session.save
+ flash[:notice] = "Login successful!"
+ redirect_back_or_default root_url
+ else
+ render :action => :new
+ end
+ end
+
+ def destroy
+ current_user_session.destroy
+ flash[:notice] = "Logout successful!"
+ redirect_back_or_default new_user_session_url
+ end
+end
View
2  app/helpers/home_helper.rb
@@ -0,0 +1,2 @@
+module HomeHelper
+end
View
2  app/helpers/user_sessions_helper.rb
@@ -0,0 +1,2 @@
+module UserSessionsHelper
+end
View
3  app/models/user.rb
@@ -0,0 +1,3 @@
+class User < ActiveRecord::Base
+ acts_as_authentic
+end
View
2  app/models/user_session.rb
@@ -0,0 +1,2 @@
+class UserSession < Authlogic::Session::Base
+end
View
1  app/views/home/index.html.erb
@@ -0,0 +1 @@
+<%= flash[:notice] %>
View
14 app/views/user_sessions/new.html.erb
@@ -0,0 +1,14 @@
+<h1>Login</h1>
+
+<% form_for @user_session do |f| %>
+ <%= f.error_messages %>
+ <%= f.label :login %><br />
+ <%= f.text_field :login %><br />
+ <br />
+ <%= f.label :password %><br />
+ <%= f.password_field :password %><br />
+ <br />
+ <%= f.check_box :remember_me %><%= f.label :remember_me %><br />
+ <br />
+ <%= f.submit "Login" %>
+<% end %>
View
0  app/views/users/_form.html.erb
No changes.
View
2  config/environment.rb
@@ -19,6 +19,8 @@
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
# config.gem "sqlite3-ruby", :lib => "sqlite3"
# config.gem "aws-s3", :lib => "aws/s3"
+
+ config.gem "authlogic", :version => '>=2.0.11'
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
View
3  config/environments/development.rb
@@ -14,4 +14,5 @@
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
-config.action_mailer.raise_delivery_errors = false
+config.action_mailer.raise_delivery_errors = false
+
View
8 config/environments/test.rb
@@ -25,4 +25,10 @@
# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
-# config.active_record.schema_format = :sql
+# config.active_record.schema_format = :sql
+
+config.gem "rspec", :lib => false, :version => ">=1.2.2"
+config.gem "rspec-rails", :lib => false, :version => ">=1.2.2"
+config.gem "webrat", :lib => false, :version => ">=0.4.3"
+config.gem "cucumber", :lib => false, :version => ">=0.2.2"
+config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"
View
9 config/routes.rb
@@ -30,14 +30,13 @@
# admin.resources :products
# end
- # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
- # map.root :controller => "welcome"
-
# See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing the them or commenting them out if you're using named routes and resources.
- map.connect ':controller/:action/:id'
- map.connect ':controller/:action/:id.:format'
+
+ map.resources :user_sessions
+ map.root :controller => 'home', :action => :index
+
end
View
30 db/migrate/20090502124658_create_users.rb
@@ -0,0 +1,30 @@
+class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table :users do |t|
+
+ # Basic fields needed for AuthLogic
+ t.string :login, :null => false
+ t.string :email, :null => false
+ t.string :crypted_password, :null => false
+ t.string :password_salt, :null => false
+ t.string :persistence_token, :null => false
+ t.string :single_access_token, :null => false
+ t.string :perishable_token, :null => false
+
+ # Optional Authlogic fields, but useful for us.
+ t.integer :login_count, :null => false, :default => 0
+ t.integer :failed_login_count, :null => false, :default => 0
+ t.datetime :last_request_at
+ t.datetime :current_login_at
+ t.datetime :last_login_at
+ t.string :current_login_ip
+ t.string :last_login_ip
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :users
+ end
+end
View
33 db/schema.rb
@@ -0,0 +1,33 @@
+# This file is auto-generated from the current state of the database. Instead of editing this file,
+# please use the migrations feature of Active Record to incrementally modify your database, and
+# then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your database schema. If you need
+# to create the application database on another system, you should be using db:schema:load, not running
+# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20090502124658) do
+
+ create_table "users", :force => true do |t|
+ t.string "login", :null => false
+ t.string "email", :null => false
+ t.string "crypted_password", :null => false
+ t.string "password_salt", :null => false
+ t.string "persistence_token", :null => false
+ t.string "single_access_token", :null => false
+ t.string "perishable_token", :null => false
+ t.integer "login_count", :default => 0, :null => false
+ t.integer "failed_login_count", :default => 0, :null => false
+ t.datetime "last_request_at"
+ t.datetime "current_login_at"
+ t.datetime "last_login_at"
+ t.string "current_login_ip"
+ t.string "last_login_ip"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
34 features/manage_sessions.feature
@@ -0,0 +1,34 @@
+Feature: Manage sessions
+ In order to limit access to authorized users
+ an administrator
+ wants to only allow authenticated users access
+
+ Scenario: Unsuccessful login
+ Given I am on the new user sessions page
+ When I press "Login"
+ Then I should see "You did not provide any details for authentication"
+
+ Scenario: Non existent user
+ Given I am on the new user sessions page
+ When I fill in "login" with "Bob"
+ And I fill in "password" with "PassWord"
+ And I press "Login"
+ Then I should see "Login does not exist"
+
+ Scenario: Incorrect password
+ Given there is a user with username "Bob" and password "PassWord"
+ And I am on the new user sessions page
+ When I fill in "login" with "Bob"
+ And I fill in "password" with "guess"
+ And I press "Login"
+ Then I should see "Password is not valid"
+
+ Scenario: Successful login
+ Given there is a user with username "Bob" and password "PassWord"
+ And I am on the new user sessions page
+ When I fill in "login" with "Bob"
+ And I fill in "password" with "PassWord"
+ And I press "Login"
+ Then I should see "Login successful!"
+ Then I should be on the homepage
+
View
3  features/step_definitions/sessions_steps.rb
@@ -0,0 +1,3 @@
+Given /^there is a user with username "([^\"]*)" and password "([^\"]*)"$/ do |login, password|
+ Factory(:user, :login => login, :password => password, :password_confirmation => password)
+end
View
3  features/support/paths.rb
@@ -16,6 +16,9 @@ def path_to(page_name)
when /the homepage/
root_path
+
+ when /the new user sessions page/
+ new_user_session_path
# Add more page name => path mappings here
View
10 spec/controllers/home_controller_spec.rb
@@ -0,0 +1,10 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe HomeController do
+
+ #Delete this example and add some real ones
+ it "should use HomeController" do
+ controller.should be_an_instance_of(HomeController)
+ end
+
+end
View
10 spec/controllers/user_sessions_controller_spec.rb
@@ -0,0 +1,10 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe UserSessionsController do
+
+ #Delete this example and add some real ones
+ it "should use UserSessionsController" do
+ controller.should be_an_instance_of(UserSessionsController)
+ end
+
+end
View
6 spec/factories.rb
@@ -0,0 +1,6 @@
+Factory.define :user do |f|
+ f.login 'bob'
+ f.password 'PassWord'
+ f.password_confirmation 'PassWord'
+ f.email 'bob@someplace.com'
+end
View
7 spec/fixtures/users.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+# column: value
+#
+# two:
+# column: value
View
11 spec/helpers/home_helper_spec.rb
@@ -0,0 +1,11 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe HomeHelper do
+
+ #Delete this example and add some real ones or delete this file
+ it "should be included in the object returned by #helper" do
+ included_modules = (class << helper; self; end).send :included_modules
+ included_modules.should include(HomeHelper)
+ end
+
+end
View
11 spec/helpers/user_sessions_helper_spec.rb
@@ -0,0 +1,11 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe UserSessionsHelper do
+
+ #Delete this example and add some real ones or delete this file
+ it "should be included in the object returned by #helper" do
+ included_modules = (class << helper; self; end).send :included_modules
+ included_modules.should include(UserSessionsHelper)
+ end
+
+end
View
12 spec/models/user_spec.rb
@@ -0,0 +1,12 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe User do
+ before(:each) do
+ @valid_attributes = {
+ }
+ end
+
+ it "should create a new instance given valid attributes" do
+ User.create!(@valid_attributes)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.