Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Closes #382. Detect if browser supports cookies.
It uses the cookie detection module in lib. Sets a cookie => redirects
determines if cookie was set in subsequent request => issues warning if
not set.
  • Loading branch information
cy authored and jerboaa committed Feb 14, 2012
1 parent 1c5a6ce commit ecc981f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/main_controller.rb
Expand Up @@ -4,6 +4,8 @@
class MainController < ApplicationController

include MainHelper
include CookieDetection

protect_from_forgery :except => [:login, :page_not_found]

# check for authorization
Expand All @@ -20,6 +22,7 @@ class MainController < ApplicationController
# is redirected to main page if session is still active and valid.

def login

# external auth has been done, skip markus authorization
if MarkusConfigurator.markus_config_remote_user_auth
if @markus_auth_remote_user.nil?
Expand All @@ -44,6 +47,12 @@ def login
end
end

# check cookies
if !cookies_enabled
flash[:login_notice] = I18n.t(:cookies_off)
return
end

@current_user = current_user
# redirect to main page if user is already logged in.
if logged_in? && !request.post?
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Expand Up @@ -16,6 +16,7 @@ en:
logging_in: "Logging in..."
login: "Login"
login_failed: "Login failed"
cookies_off: "Please configure your browser to accept cookies."
password_not_blank: "Your password must not be blank."
username_not_blank: "Your username must not be blank."
username_and_password_not_blank: "Your username and password must not be blank."
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Expand Up @@ -16,6 +16,7 @@ fr:
logging_in: "Connexion"
login: "Identifiant"
login_failed: "L'établissement de la connexion a échoué."
cookies_off: "Veuillez configurer votre navigateur afin d'accepter les cookies."
password_not_blank: "Veuillez entrer votre mot de passe."
username_not_blank: "Veuillez entrer votre nom d'utilisateur."
username_and_password_not_blank: "Veuillez entrer vos nom d'utilisateur et mot de passe."
Expand Down
17 changes: 17 additions & 0 deletions lib/cookie_detection.rb
@@ -0,0 +1,17 @@
# detects if cookies are enabled in the user's browser, by attempting to read/write a cookie.

module CookieDetection

protected

# true if cookies are enabled, false otherwise.
def cookies_enabled
return true unless cookies["cookieTest"].blank?
cookies["cookieTest"] = Time.now
session[:return_to] = request.fullpath
if cookies["cookie_test"].blank?
return false
end
return true
end
end
2 changes: 2 additions & 0 deletions test/functional/main_controller_test.rb
Expand Up @@ -17,6 +17,8 @@ class MainControllerTest < AuthenticatedControllerTest

def setup
clear_fixtures
# bypass cookie detection in the test because the command line, which is running the test, cannot accept cookies
@request.cookies["cookieTest"] = "fake cookie bypasses filter"
end

context "A not authenticated user" do
Expand Down
2 changes: 2 additions & 0 deletions test/functional/role_switching_test.rb
Expand Up @@ -11,6 +11,8 @@ class RoleSwitchingTest < AuthenticatedControllerTest

def setup
clear_fixtures
# bypass cookie detection in the test because the command line, which is running the test, cannot accept cookies
@request.cookies["cookieTest"] = "fake cookie bypasses filter"
end

context "A valid admin" do
Expand Down

0 comments on commit ecc981f

Please sign in to comment.