adding public auth

Oleg committed Aug 29, 2011
commit 14348670f8ed8cf2a472eca56d2f29b1cd7037b0
Showing 11 changed files with 76 additions and 30 deletions
app/controllers/cms_admin/base_controller.rb +1 -1
@@ @@ -3,7 +3,7 @@ class CmsAdmin::BaseController < ApplicationController
protect_from_forgery
# Authentication module must have #authenticate method
- include ComfortableMexicanSofa.config.authentication.to_s.constantize
+ include ComfortableMexicanSofa.config.admin_auth.to_s.constantize
before_filter :authenticate,
:load_admin_site,
app/controllers/cms_content_controller.rb +8 -3
@@ @@ -1,9 +1,14 @@
class CmsContentController < ApplicationController
-
+
+ # Authentication module must have #authenticate method
+ include ComfortableMexicanSofa.config.public_auth.to_s.constantize
+
before_filter :load_cms_site
before_filter :load_fixtures
- before_filter :load_cms_page, :only => :render_html
- before_filter :load_cms_layout, :only => [:render_css, :render_js]
+ before_filter :load_cms_page, :authenticate,
+ :only => :render_html
+ before_filter :load_cms_layout,
+ :only => [:render_css, :render_js]
def render_html(status = 200)
if layout = @cms_page.layout
config/initializers/comfortable_mexican_sofa.rb +6 -1
@@ @@ -6,7 +6,12 @@ ComfortableMexicanSofa.configure do |config|
# Module responsible for authentication. You can replace it with your own.
# It simply needs to have #authenticate method. See http_auth.rb for reference.
- # config.authentication = 'ComfortableMexicanSofa::HttpAuth'
+ # config.admin_auth = 'ComfortableMexicanSofa::HttpAuth'
+
+ # Module responsible for public authentication. Similar to the above. You also
+ # will have access to @cms_site, @cms_layout, @cms_page so you can use them in
+ # your logic. Default module doesn't do anything.
+ # config.public_auth = 'ComfortableMexicanSofa::DummyAuth'
# Default url to access admin area is http://yourhost/cms-admin/
# You can change 'cms-admin' to 'admin', for example. To disable admin area
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb +2 -1
@@ @@ -6,7 +6,8 @@ end
[ 'comfortable_mexican_sofa/version',
'comfortable_mexican_sofa/error',
'comfortable_mexican_sofa/configuration',
- 'comfortable_mexican_sofa/http_auth',
+ 'comfortable_mexican_sofa/authentication/http_auth',
+ 'comfortable_mexican_sofa/authentication/dummy_auth',
'comfortable_mexican_sofa/controller_methods',
'comfortable_mexican_sofa/view_hooks',
'comfortable_mexican_sofa/view_methods',
comfortable_mexican_sofa/authentication/dummy_auth.rb b/lib/comfortable_mexican_sofa/authentication/dummy_auth.rb +8 -0
@@ @@ -0,0 +1,8 @@
+ module ComfortableMexicanSofa::DummyAuth
+
+ # Will always let you in
+ def authenticate
+ true
+ end
+
+ end
\ No newline at end of file
comfortable_mexican_sofa/authentication/http_auth.rb b/lib/comfortable_mexican_sofa/authentication/http_auth.rb +18 -0
@@ @@ -0,0 +1,18 @@
+ module ComfortableMexicanSofa::HttpAuth
+ # Set username and password in config/initializers/comfortable_mexican_sofa.rb
+ # Like this:
+ # CmsHttpAuthentication.username = 'myname'
+ # CmsHttpAuthentication.password = 'mypassword'
+ mattr_accessor :username,
+ :password
+
+ # Simple http_auth. When implementing some other form of authentication
+ # this method should return +true+ if everything is great, or redirect user
+ # to some other page, thus denying access to cms admin section.
+ def authenticate
+ authenticate_or_request_with_http_basic do |username, password|
+ username == self.username && password == self.password
+ end
+ end
+
+ end
\ No newline at end of file
comfortable_mexican_sofa/configuration.rb b/lib/comfortable_mexican_sofa/configuration.rb +6 -2
@@ @@ -6,7 +6,10 @@ class ComfortableMexicanSofa::Configuration
attr_accessor :cms_title
# Module that will handle authentication to access cms-admin area
- attr_accessor :authentication
+ attr_accessor :admin_auth
+
+ # Module that will handle authentication for public pages
+ attr_accessor :public_auth
# Default url to access admin area is http://yourhost/cms-admin/
# You can change 'cms-admin' to 'admin', for example.
@@ @@ -43,7 +46,8 @@ class ComfortableMexicanSofa::Configuration
# Configuration defaults
def initialize
@cms_title = 'ComfortableMexicanSofa MicroCMS'
- @authentication = 'ComfortableMexicanSofa::HttpAuth'
+ @admin_auth = 'ComfortableMexicanSofa::HttpAuth'
+ @public_auth = 'ComfortableMexicanSofa::DummyAuth'
@seed_data_path = nil
@admin_route_prefix = 'cms-admin'
@admin_route_redirect = ''
comfortable_mexican_sofa/http_auth.rb b/lib/comfortable_mexican_sofa/http_auth.rb +0 -18
@@ @@ -1,18 +0,0 @@
- module ComfortableMexicanSofa::HttpAuth
- # Set username and password in config/initializers/comfortable_mexican_sofa.rb
- # Like this:
- # CmsHttpAuthentication.username = 'myname'
- # CmsHttpAuthentication.password = 'mypassword'
- mattr_accessor :username,
- :password
-
- # Simple http_auth. When implementing some other form of authentication
- # this method should return +true+ if everything is great, or redirect user
- # to some other page, thus denying access to cms admin section.
- def authenticate
- authenticate_or_request_with_http_basic do |username, password|
- username == self.username && password == self.password
- end
- end
-
- end
\ No newline at end of file
test/integration/authentication_test.rb +23 -2
@@ @@ -2,8 +2,20 @@ require File.expand_path('../test_helper', File.dirname(__FILE__))
class AuthenticationTest < ActionDispatch::IntegrationTest
+ module TestLockPublicAuth
+ def authenticate
+ return redirect_to('/lockout')
+ end
+ end
+
+ module TestUnlockPublicAuth
+ def authenticate
+ true
+ end
+ end
+
def test_get_with_unauthorized_access
- assert_equal 'ComfortableMexicanSofa::HttpAuth', ComfortableMexicanSofa.config.authentication
+ assert_equal 'ComfortableMexicanSofa::HttpAuth', ComfortableMexicanSofa.config.admin_auth
get '/cms-admin/sites'
assert_response :unauthorized
get '/'
@@ @@ -16,7 +28,7 @@ class AuthenticationTest < ActionDispatch::IntegrationTest
end
def test_get_with_changed_default_config
- assert_equal 'ComfortableMexicanSofa::HttpAuth', ComfortableMexicanSofa.config.authentication
+ assert_equal 'ComfortableMexicanSofa::HttpAuth', ComfortableMexicanSofa.config.admin_auth
ComfortableMexicanSofa::HttpAuth.username = 'newuser'
ComfortableMexicanSofa::HttpAuth.password = 'newpass'
http_auth :get, '/cms-admin/sites'
@@ @@ -24,4 +36,13 @@ class AuthenticationTest < ActionDispatch::IntegrationTest
http_auth :get, '/cms-admin/sites', {}, 'newuser', 'newpass'
assert_response :success
end
+
+ def test_get_public_with_custom_auth
+ CmsContentController.send :include, TestLockPublicAuth
+ get '/'
+ assert_response :redirect
+ assert_redirected_to '/lockout'
+ # reset auth module
+ CmsContentController.send :include, TestUnlockPublicAuth
+ end
end
\ No newline at end of file
test/test_helper.rb +2 -1
@@ @@ -17,7 +17,8 @@ class ActiveSupport::TestCase
def reset_config
ComfortableMexicanSofa.configure do |config|
config.cms_title = 'ComfortableMexicanSofa MicroCMS'
- config.authentication = 'ComfortableMexicanSofa::HttpAuth'
+ config.admin_auth = 'ComfortableMexicanSofa::HttpAuth'
+ config.public_auth = 'ComfortableMexicanSofa::DummyAuth'
config.admin_route_prefix = 'cms-admin'
config.admin_route_redirect = ''
config.allow_irb = false
test/unit/configuration_test.rb +2 -1
@@ @@ -7,7 +7,8 @@ class ConfigurationTest < ActiveSupport::TestCase
def test_configuration_presense
assert config = ComfortableMexicanSofa.configuration
assert_equal 'ComfortableMexicanSofa MicroCMS', config.cms_title
- assert_equal 'ComfortableMexicanSofa::HttpAuth', config.authentication
+ assert_equal 'ComfortableMexicanSofa::HttpAuth', config.admin_auth
+ assert_equal 'ComfortableMexicanSofa::DummyAuth', config.public_auth
assert_equal 'cms-admin', config.admin_route_prefix
assert_equal '', config.admin_route_redirect
assert_equal false, config.allow_irb