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

Commit 4975db0

Browse files
committed
Add authlogic gem and configure people and person_sessions.
This doesn't work.
1 parent 7af7d70 commit 4975db0

20 files changed

+180
-2
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ gem 'spring', group: :development
4040

4141
# required for travis
4242
gem 'rake', group: [:test]
43+
44+
gem 'authlogic', '>= 3.4.0'

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ GEM
2828
thread_safe (~> 0.1)
2929
tzinfo (~> 1.1)
3030
arel (5.0.1.20140414130214)
31+
authlogic (3.4.2)
32+
activerecord (>= 3.2)
33+
activesupport (>= 3.2)
34+
request_store (~> 1.0)
3135
builder (3.2.2)
3236
coffee-rails (4.0.1)
3337
coffee-script (>= 2.2.0)
@@ -75,6 +79,7 @@ GEM
7579
rake (10.3.2)
7680
rdoc (4.1.1)
7781
json (~> 1.4)
82+
request_store (1.0.7)
7883
sass (3.2.19)
7984
sass-rails (4.0.3)
8085
railties (>= 4.0.0, < 5.0)
@@ -113,6 +118,7 @@ PLATFORMS
113118
ruby
114119

115120
DEPENDENCIES
121+
authlogic (>= 3.4.0)
116122
coffee-rails (~> 4.0.0)
117123
jbuilder (~> 2.0)
118124
jquery-rails
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the person_sessions controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

app/controllers/application_controller.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,45 @@ class ApplicationController < ActionController::Base
22
# Prevent CSRF attacks by raising an exception.
33
# For APIs, you may want to use :null_session instead.
44
protect_from_forgery with: :exception
5+
6+
# handle current user and session for the view
7+
helper_method :current_person_session, :current_person
8+
9+
private
10+
def current_person_session
11+
return @current_person_session if defined?(@current_person_session)
12+
@current_person_session = PersonSession.find
13+
end
14+
15+
def current_person
16+
return @current_person if defined?(@current_person)
17+
@current_person = current_person_session && current_person_session.user
18+
end
19+
20+
def require_person
21+
unless current_person
22+
store_location
23+
flash[:notice] = "You must be logged in to access this page"
24+
redirect_to login_path
25+
return false
26+
end
27+
end
28+
29+
def require_no_person
30+
if current_person
31+
store_location
32+
flash[:notice] = "You cannot access this page while logged in to another account"
33+
redirect_to root_url
34+
return false
35+
end
36+
end
37+
38+
def store_location
39+
session[:return_to] = request.request_uri
40+
end
41+
42+
def redirect_back_or_default(default)
43+
redirect_to(session[:return_to] || default)
44+
session[:return_to] = nil
45+
end
546
end

app/controllers/people_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
class PeopleController < ApplicationController
2+
before_filter :require_no_person, :only => [:new, :create]
3+
before_filter :require_person, :only => [:show, :edit, :update]
4+
25
def new
36
end
47

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class PersonSessionsController < ApplicationController
2+
before_filter :require_no_person, :only => [:new, :create]
3+
before_filter :require_person, :only => :destroy
4+
5+
def new
6+
@user_session = PersonSession.new
7+
end
8+
9+
def create
10+
@user_session = PersonSession.new(params[:person_session])
11+
if @user_session.save
12+
flash[:notice] = "Login successful!"
13+
redirect_back_or_default account_url
14+
else
15+
render :action => :new
16+
end
17+
end
18+
19+
def destroy
20+
current_person_session.destroy
21+
flash[:notice] = "Logout successful!"
22+
redirect_back_or_default new_person_session_url
23+
end
24+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module PersonSessionsHelper
2+
end

app/models/person.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
class Person < ActiveRecord::Base
2+
acts_as_authentic do |c|
3+
# add config here
4+
end
25
end

app/models/person_session.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class PersonSession < Authlogic::Session::Base
2+
end

0 commit comments

Comments
 (0)