default http authentication is in

Oleg committed Oct 14, 2010
commit 13636ebf1a3657d04ef4ab7158391f04eb77b495
Showing 9 changed files with 66 additions and 20 deletions
app/controllers/cms_admin/base_controller.rb +6 -1
@@ @@ -1,5 +1,10 @@
+ # Authentication module must have #authenticate method
+ include ComfortableMexicanSofa.config.authentication.to_s.constantize
+
class CmsAdmin::BaseController < ApplicationController
+ before_filter :authenticate
+
layout 'cms_admin'
-
+
end
config/initializers/comfortable_mexican_sofa.rb +6 -2
@@ @@ -2,6 +2,10 @@
ComfortableMexicanSofa.configure do |config|
config.cms_title = 'ComfortableMexicanSofa'
- config.authentication = 'CmsAuthentication'
+ config.authentication = 'CmsHttpAuthentication'
config.multiple_sites = false
- end
\ No newline at end of file
+ end
+
+ # Credentials for CmsHttpAuthentication
+ CmsHttpAuthentication.username = 'username'
+ CmsHttpAuthentication.password = 'password'
\ No newline at end of file
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb +0 -2
@@ @@ -43,7 +43,6 @@ class CreateCms < ActiveRecord::Migration
t.timestamps
end
add_index :cms_blocks, [:cms_page_id, :type, :label]
- # TODO: index this
# -- Snippets -----------------------------------------------------------
create_table :cms_snippets do |t|
@@ @@ -60,7 +59,6 @@ class CreateCms < ActiveRecord::Migration
t.integer :file_file_size
t.timestamps
end
-
end
def self.down
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb +4 -8
@@ @@ -1,8 +1,6 @@
- if defined?(Rails) && Rails::VERSION::MAJOR == 3
- require File.expand_path('comfortable_mexican_sofa/cms_engine', File.dirname(__FILE__))
- end
-
- [ 'comfortable_mexican_sofa/cms_configuration',
+ [ 'comfortable_mexican_sofa/cms_engine',
+ 'comfortable_mexican_sofa/cms_configuration',
+ 'comfortable_mexican_sofa/cms_http_authentication',
'comfortable_mexican_sofa/cms_rails_extensions',
'comfortable_mexican_sofa/cms_form_builder',
'comfortable_mexican_sofa/cms_acts_as_tree',
@@ @@ -40,9 +38,7 @@ module ComfortableMexicanSofa
# Modify CMS configuration
# Example:
# ComfortableMexicanSofa.configure do |config|
- # config.authentication = :http_auth
- # config.http_auth_user = 'username'
- # config.http_auth_pass = 'password'
+ # config.cms_title = 'Comfortable Mexican Sofa'
# end
def configure
yield configuration
comfortable_mexican_sofa/cms_configuration.rb b/lib/comfortable_mexican_sofa/cms_configuration.rb +1 -1
@@ @@ -12,7 +12,7 @@ class ComfortableMexicanSofa::Configuration
# Configuration defaults
def initialize
@cms_title = 'ComfortableMexicanSofa'
- @authentication = 'CmsAuthentication'
+ @authentication = 'CmsHttpAuthentication'
@multiple_sites = false
end
comfortable_mexican_sofa/cms_http_authentication.rb b/lib/comfortable_mexican_sofa/cms_http_authentication.rb +18 -0
@@ @@ -0,0 +1,18 @@
+ module CmsHttpAuthentication
+
+ # 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 +28 -0
@@ @@ -0,0 +1,28 @@
+ require File.dirname(__FILE__) + '/../test_helper'
+
+ class AuthenticationTest < ActionDispatch::IntegrationTest
+
+ def test_get_with_unauthorized_access
+ assert_equal 'CmsHttpAuthentication', ComfortableMexicanSofa.config.authentication
+ get '/cms-admin/pages'
+ assert_response :unauthorized
+ get '/'
+ assert_response :success
+ end
+
+ def test_get_with_authorized_access
+ get '/cms-admin/pages', {}, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('username:password')}"}
+ assert_response :success
+ end
+
+ def test_get_with_changed_default_config
+ assert_equal 'CmsHttpAuthentication', ComfortableMexicanSofa.config.authentication
+ CmsHttpAuthentication.username = 'newuser'
+ CmsHttpAuthentication.password = 'newpass'
+ get '/cms-admin/pages', {}, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('username:password')}"}
+ assert_response :unauthorized
+ get '/cms-admin/pages', {}, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('newuser:newpass')}"}
+ assert_response :success
+ end
+
+ end
\ No newline at end of file
test/test_helper.rb +2 -5
@@ @@ -10,7 +10,7 @@ class ActiveSupport::TestCase
# resetting default configuration
ComfortableMexicanSofa.configure do |config|
config.cms_title = 'ComfortableMexicanSofa'
- config.authentication = 'CmsAuthentication'
+ config.authentication = 'CmsHttpAuthentication'
config.multiple_sites = false
end
end
@@ @@ -34,13 +34,10 @@ class ActiveSupport::TestCase
def rendered_content_formatter(string)
string.gsub(/^[ ]+/, '')
end
-
end
class ActionController::TestCase
-
- def http_auth
+ def setup
@request.env['HTTP_AUTHORIZATION'] = "Basic #{Base64.encode64('username:password')}"
end
-
end
test/unit/cms_configuration_test.rb +1 -1
@@ @@ -5,7 +5,7 @@ class CmsConfigurationTest < ActiveSupport::TestCase
def test_configuration_presense
assert config = ComfortableMexicanSofa.configuration
assert_equal 'ComfortableMexicanSofa', config.cms_title
- assert_equal 'CmsAuthentication', config.authentication
+ assert_equal 'CmsHttpAuthentication', config.authentication
assert_equal false, config.multiple_sites
end