Permalink
Browse files

Implement authentication mechanism

  • Loading branch information...
1 parent 386b2d2 commit 4bafade21672325103129b001c135ea59995fca0 @lifo lifo committed Apr 7, 2010
Showing with 52 additions and 2 deletions.
  1. +15 −2 README
  2. +7 −0 app/controllers/tolk/application_controller.rb
  3. +30 −0 test/integration/authentication_test.rb
View
17 README
@@ -17,7 +17,7 @@ To setup :
* Usage
-Tolk treats <tt>Locale.primary_locale_name</tt> as the main source of strings to be translated. Developers are expected to make all the changes to that file ( en.yml in our case ) and treat all the other locale.yml files as readonly files.
+Tolk treats <tt>Tolk::Locale.primary_locale_name</tt> as the main source of strings to be translated. Developers are expected to make all the changes to that file ( en.yml in our case ) and treat all the other locale.yml files as readonly files.
As tolk stores all the keys and translated strings in the database, you need to ask Tolk to update it's database from the primary yml file :
@@ -31,4 +31,17 @@ Upon visiting http://<your app>/tolk - you will be presented with different opti
This will generate yml files for all non primary locales and put them in RAILS_ROOT/config/locales/ directory by default. You can pass directory path as the argument if you want the generated files to be at a different location :
- $ script/runner "Tolk::Locale.dump_all('/Users/lifo')"
+ $ script/runner "Tolk::Locale.dump_all('/Users/lifo')"
+
+* Authentication
+
+If you want to authenticate users who can access Tolk, you need to provide <tt>Tolk::ApplicationController.authenticator</tt> proc. For example :
+
+ # config/initializers/tolk.rb
+ Tolk::ApplicationController.authenticator = proc {
+ authenticate_or_request_with_http_basic do |user_name, password|
+ user_name == 'translator' && password == 'transpass'
+ end
+ }
+
+Authenticator proc will be run from a before filter in controller context.
@@ -2,5 +2,12 @@ module Tolk
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery
+
+ cattr_accessor :authenticator
+ before_filter :authenticate
+
+ def authenticate
+ self.authenticator.bind(self).call if self.authenticator && self.authenticator.respond_to?(:call)
+ end
end
end
@@ -0,0 +1,30 @@
+require 'test_helper'
+
+class AuthenticationTest < ActionController::IntegrationTest
+
+ def setup
+ Tolk::ApplicationController.authenticator = proc do
+ authenticate_or_request_with_http_basic {|user_name, password| user_name == 'lifo' && password == 'pass' }
+ end
+ end
+
+ def teardown
+ Tolk::ApplicationController.authenticator = nil
+ end
+
+ test "failed authentication" do
+ get '/tolk'
+ assert_response 401
+ end
+
+ test "successful authentication" do
+ get '/tolk', nil, 'HTTP_AUTHORIZATION' => encode_credentials('lifo', 'pass')
+ assert_response :success
+ end
+
+ protected
+
+ def encode_credentials(username, password)
+ "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
+ end
+end

0 comments on commit 4bafade

Please sign in to comment.