projects
/
selectricity
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
eaf661e
)
The user model has been modified so that it stores the user object itself in the...
author
<jlsharps@mit.edu>
<>
Tue, 7 Aug 2007 23:16:04 +0000
(19:16 -0400)
committer
<jlsharps@mit.edu>
<>
Tue, 7 Aug 2007 23:16:04 +0000
(19:16 -0400)
app/controllers/account_controller.rb
patch
|
blob
|
history
app/controllers/site_controller.rb
patch
|
blob
|
history
app/models/user.rb
patch
|
blob
|
history
lib/authenticated_system.rb
patch
|
blob
|
history
lib/authenticated_test_helper.rb
patch
|
blob
|
history
test/functional/account_controller_test.rb
patch
|
blob
|
history
diff --git
a/app/controllers/account_controller.rb
b/app/controllers/account_controller.rb
index c13203b793283f6b34746f7acfb7f11c70345b87..031368b84327d7a1f5d397362686462b0f974859 100644
(file)
--- a/
app/controllers/account_controller.rb
+++ b/
app/controllers/account_controller.rb
@@
-19,7
+19,7
@@
class AccountController < ApplicationController
self.current_user.remember_me
cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
end
self.current_user.remember_me
cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
end
- redirect_back_or_default(:controller => '/
account
', :action => 'index')
+ redirect_back_or_default(:controller => '/
site
', :action => 'index')
flash[:notice] = "Logged in successfully"
end
end
flash[:notice] = "Logged in successfully"
end
end
@@
-29,7
+29,7
@@
class AccountController < ApplicationController
return unless request.post?
@user.save!
self.current_user = @user
return unless request.post?
@user.save!
self.current_user = @user
- redirect_back_or_default(:controller => '/
account
', :action => 'index')
+ redirect_back_or_default(:controller => '/
site
', :action => 'index')
flash[:notice] = "Thanks for signing up!"
rescue ActiveRecord::RecordInvalid
render :action => 'signup'
flash[:notice] = "Thanks for signing up!"
rescue ActiveRecord::RecordInvalid
render :action => 'signup'
@@
-40,6
+40,6
@@
class AccountController < ApplicationController
cookies.delete :auth_token
reset_session
flash[:notice] = "You have been logged out."
cookies.delete :auth_token
reset_session
flash[:notice] = "You have been logged out."
- redirect_back_or_default(:controller => '/
account
', :action => 'index')
+ redirect_back_or_default(:controller => '/
site
', :action => 'index')
end
end
end
end
diff --git
a/app/controllers/site_controller.rb
b/app/controllers/site_controller.rb
index f6b36a774b141fb4cc0c8ddd07084ed65120f6fb..776832218c2b9a611e874a50564f3efa42805696 100644
(file)
--- a/
app/controllers/site_controller.rb
+++ b/
app/controllers/site_controller.rb
@@
-5,10
+5,11
@@
class SiteController < ApplicationController
def index
@quickvotes = QuickVote.find(:all, ["quickvote = 1"]).sort {|a,b| b.enddate <=> a.enddate}[0..1]
# if the person claims they are logged in
def index
@quickvotes = QuickVote.find(:all, ["quickvote = 1"]).sort {|a,b| b.enddate <=> a.enddate}[0..1]
# if the person claims they are logged in
- if session[:user]
-
+
+ if logged_in?
# check to see that we actually have record of them
# check to see that we actually have record of them
- if User.find(:all, ["id = ?", session[:user].id]).length == 1
+ if User.exists?(session[:user])
+ username = User.find(session[:user]).login
# if we have record of them, grab the list of their elections
session[:user] = User.find(session[:user])
@current_elections = session[:user].elections.sort do |a,b|
# if we have record of them, grab the list of their elections
session[:user] = User.find(session[:user])
@current_elections = session[:user].elections.sort do |a,b|
@@
-16,8
+17,9
@@
class SiteController < ApplicationController
end
else
# if we have no record of them, set the session id back to
end
else
# if we have no record of them, set the session id back to
- # nothing and start again
+
# nothing and start again
session[:user] = nil
session[:user] = nil
+
end
end
end
end
end
end
diff --git
a/app/models/user.rb
b/app/models/user.rb
index 64fd71c544af5ff93c858f871b763ded4842b644..78266ad61d06d3d8586d9c9da5b0e6f1f42f0da6 100755
(executable)
--- a/
app/models/user.rb
+++ b/
app/models/user.rb
@@
-4,7
+4,8
@@
class User < ActiveRecord::Base
# Virtual attribute for the unencrypted password
attr_accessor :password
# Virtual attribute for the unencrypted password
attr_accessor :password
-
+ attr_accessor :current_user
+
validates_presence_of :login, :email
validates_presence_of :password, :if => :password_required?
validates_presence_of :password_confirmation, :if => :password_required?
validates_presence_of :login, :email
validates_presence_of :password, :if => :password_required?
validates_presence_of :password_confirmation, :if => :password_required?
diff --git
a/lib/authenticated_system.rb
b/lib/authenticated_system.rb
index 840d89aac08c489ed964271dd262f20cf9318876..370a3e961ed74dac41cff7b8ec1c2e26326dd592 100644
(file)
--- a/
lib/authenticated_system.rb
+++ b/
lib/authenticated_system.rb
@@
-3,17
+3,18
@@
module AuthenticatedSystem
# Returns true or false if the user is logged in.
# Preloads @current_user with the user model if they're logged in.
def logged_in?
# Returns true or false if the user is logged in.
# Preloads @current_user with the user model if they're logged in.
def logged_in?
- current_user != :false
+ (@current_user ||= session[:user_id] ? User.find_by_id(session[:user_id]) : :false).is_a?(User)
end
# Accesses the current user from the session.
def current_user
end
# Accesses the current user from the session.
def current_user
- @current_user
||= (session[:user] && User.find_by_id(session[:user])) || :false
+ @current_user
if logged_in?
end
# Store the given user in the session.
def current_user=(new_user)
end
# Store the given user in the session.
def current_user=(new_user)
- session[:user] = (new_user.nil? || new_user.is_a?(Symbol)) ? nil : new_user.id
+ session[:user] = new_user
+ session[:user_id] = new_user.nil? ? nil : new_user.id
@current_user = new_user
end
@current_user = new_user
end
diff --git
a/lib/authenticated_test_helper.rb
b/lib/authenticated_test_helper.rb
index a704035c023e2051f2978523225257f1d5e80bf3..83fc20cbcf5c7a4a8ccc1a66983d8e328b4ef686 100644
(file)
--- a/
lib/authenticated_test_helper.rb
+++ b/
lib/authenticated_test_helper.rb
@@
-1,7
+1,7
@@
module AuthenticatedTestHelper
# Sets the current user in the session from the user fixtures.
def login_as(user)
module AuthenticatedTestHelper
# Sets the current user in the session from the user fixtures.
def login_as(user)
- @request.session[:user] = user ? users(user).id : nil
+ @request.session[:user
_id
] = user ? users(user).id : nil
end
def content_type(type)
end
def content_type(type)
diff --git
a/test/functional/account_controller_test.rb
b/test/functional/account_controller_test.rb
index 3c8cd2265b95ef3d2c27795c71b559f1e56a9bf2..21e20ac1a8d4934f8dea623ffebe3cadc6883139 100644
(file)
--- a/
test/functional/account_controller_test.rb
+++ b/
test/functional/account_controller_test.rb
@@
-25,22
+25,23
@@
class AccountControllerTest < Test::Unit::TestCase
def test_should_fail_login_and_not_redirect
post :login, :login => 'quentin', :password => 'bad password'
def test_should_fail_login_and_not_redirect
post :login, :login => 'quentin', :password => 'bad password'
- assert_nil session[:user]
- assert_response :success
+ assert_nil session[:user]
+ assert_nil session[:user_id]
+ assert_response :success
end
def test_should_allow_signup
assert_difference User, :count do
end
def test_should_allow_signup
assert_difference User, :count do
-
create_user
-
assert_response :redirect
+ create_user
+ assert_response :redirect
end
end
def test_should_require_login_on_signup
assert_no_difference User, :count do
end
end
def test_should_require_login_on_signup
assert_no_difference User, :count do
-
create_user(:login => nil)
-
assert assigns(:user).errors.on(:login)
-
assert_response :success
+ create_user(:login => nil)
+ assert assigns(:user).errors.on(:login)
+ assert_response :success
end
end
end
end
@@
-72,6
+73,7
@@
class AccountControllerTest < Test::Unit::TestCase
login_as :quentin
get :logout
assert_nil session[:user]
login_as :quentin
get :logout
assert_nil session[:user]
+ assert_nil session[:user_id]
assert_response :redirect
end
assert_response :redirect
end
Benjamin Mako Hill
||
Want to submit a patch?