Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Commit

Permalink
Add authlogic gem and configure people and person_sessions.
Browse files Browse the repository at this point in the history
This doesn't work.
  • Loading branch information
DavidS committed Jul 27, 2014
1 parent 7af7d70 commit 4975db0
Show file tree
Hide file tree
Showing 20 changed files with 180 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ gem 'spring', group: :development

# required for travis
gem 'rake', group: [:test]

gem 'authlogic', '>= 3.4.0'
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
authlogic (3.4.2)
activerecord (>= 3.2)
activesupport (>= 3.2)
request_store (~> 1.0)
builder (3.2.2)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
Expand Down Expand Up @@ -75,6 +79,7 @@ GEM
rake (10.3.2)
rdoc (4.1.1)
json (~> 1.4)
request_store (1.0.7)
sass (3.2.19)
sass-rails (4.0.3)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -113,6 +118,7 @@ PLATFORMS
ruby

DEPENDENCIES
authlogic (>= 3.4.0)
coffee-rails (~> 4.0.0)
jbuilder (~> 2.0)
jquery-rails
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/person_sessions.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/person_sessions.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the person_sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
41 changes: 41 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,45 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

# handle current user and session for the view
helper_method :current_person_session, :current_person

private
def current_person_session
return @current_person_session if defined?(@current_person_session)
@current_person_session = PersonSession.find
end

def current_person
return @current_person if defined?(@current_person)
@current_person = current_person_session && current_person_session.user
end

def require_person
unless current_person
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to login_path
return false
end
end

def require_no_person
if current_person
store_location
flash[:notice] = "You cannot access this page while logged in to another account"
redirect_to root_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
3 changes: 3 additions & 0 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class PeopleController < ApplicationController
before_filter :require_no_person, :only => [:new, :create]
before_filter :require_person, :only => [:show, :edit, :update]

def new
end

Expand Down
24 changes: 24 additions & 0 deletions app/controllers/person_sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class PersonSessionsController < ApplicationController
before_filter :require_no_person, :only => [:new, :create]
before_filter :require_person, :only => :destroy

def new
@user_session = PersonSession.new
end

def create
@user_session = PersonSession.new(params[:person_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url
else
render :action => :new
end
end

def destroy
current_person_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default new_person_session_url
end
end
2 changes: 2 additions & 0 deletions app/helpers/person_sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PersonSessionsHelper
end
3 changes: 3 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Person < ActiveRecord::Base
acts_as_authentic do |c|
# add config here
end
end
2 changes: 2 additions & 0 deletions app/models/person_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class PersonSession < Authlogic::Session::Base
end
2 changes: 1 addition & 1 deletion config/initializers/filter_parameter_logging.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Be sure to restart your server when you modify this file.

# Configure sensitive parameters which will be filtered from the log file.
Rails.application.config.filter_parameters += [:password]
Rails.application.config.filter_parameters += [:password, :password_confirmation]
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# resources :products

resources :people
resources :user_session

# Example resource route with options:
# resources :products do
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20140727070204_create_user_sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateUserSessions < ActiveRecord::Migration
def change
create_table :user_sessions do |t|
t.string :session_id, :null => false
t.text :data

t.timestamps
end

add_index :user_sessions, :session_id
add_index :user_sessions, :updated_at
end
end
17 changes: 17 additions & 0 deletions db/migrate/20140727071221_add_user_to_people.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddUserToPeople < ActiveRecord::Migration
def change
add_column :people, :login, :string
add_index :people, :login
add_column :people, :crypted_password, :string
add_column :people, :password_salt, :string
add_column :people, :persistence_token, :string
add_index :people, :persistence_token
add_column :people, :login_count, :integer
add_column :people, :last_request_at, :datetime
add_index :people, :last_request_at
add_column :people, :last_login_at, :datetime
add_column :people, :current_login_at, :datetime
add_column :people, :last_login_ip, :string
add_column :people, :current_login_ip, :string
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameUserSessionsToPersonSessions < ActiveRecord::Migration
def change
rename_table :user_sessions, :person_sessions
end
end
26 changes: 25 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,36 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140726092933) do
ActiveRecord::Schema.define(version: 20140727075101) do

create_table "people", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "login"
t.string "crypted_password"
t.string "password_salt"
t.string "persistence_token"
t.integer "login_count"
t.datetime "last_request_at"
t.datetime "last_login_at"
t.datetime "current_login_at"
t.string "last_login_ip"
t.string "current_login_ip"
end

add_index "people", ["last_request_at"], name: "index_people_on_last_request_at"
add_index "people", ["login"], name: "index_people_on_login"
add_index "people", ["persistence_token"], name: "index_people_on_persistence_token"

create_table "person_sessions", force: true do |t|
t.string "session_id", null: false
t.text "data"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "person_sessions", ["session_id"], name: "index_person_sessions_on_session_id"
add_index "person_sessions", ["updated_at"], name: "index_person_sessions_on_updated_at"

end
7 changes: 7 additions & 0 deletions test/controllers/person_sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class PersonSessionsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
11 changes: 11 additions & 0 deletions test/fixtures/person_sessions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
4 changes: 4 additions & 0 deletions test/helpers/person_sessions_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class PersonSessionsHelperTest < ActionView::TestCase
end
7 changes: 7 additions & 0 deletions test/models/person_session_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class PersonSessionTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 4975db0

Please sign in to comment.