Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial import from access_control_list repository

  • Loading branch information...
commit cf98c48d6cd30e71f124ecda51c9a4af8f8e028e 0 parents
Sean Huber authored
3  CHANGELOG
@@ -0,0 +1,3 @@
+2008-06-05 - Sean Huber (shuber@huberry.com)
+ * Initial import from access_control_list repository
+ * subdomain field is configurable
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Sean Huber (shuber@huberry.com)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 README.markdown
@@ -0,0 +1,29 @@
+Huberry::SubdomainAccount
+=========================
+
+A rails plugin that handles subdomain accounts
+
+
+Installation
+------------
+
+ script/plugin install git://github.com/shuber/subdomain_account.git
+
+
+Example
+-------
+
+Coming soon...
+
+
+Controller Methods
+------------------
+
+ # Returns the current account or nil if one is not found
+ current_account
+
+
+Contact
+-------
+
+Problems, comments, and suggestions all welcome: [shuber@huberry.com](mailto:shuber@huberry.com)
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run the authentication tests'
+task :default => :test
+
+desc 'Test the authentication plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the authentication plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Authentication'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
5 init.rb
@@ -0,0 +1,5 @@
+require 'huberry/subdomain_account/controller_methods'
+
+ActionController::Base.extend Huberry::SubdomainAccount::ControllerMethods
+
+$:.unshift File.dirname(__FILE__) + '/lib'
64 lib/huberry/subdomain_account/controller_methods.rb
@@ -0,0 +1,64 @@
+module Huberry
+ module SubdomainAccount
+ module ControllerMethods
+ def self.extended(base)
+ base.class_eval do
+ include InstanceMethods
+ include AuthenticationPatch if included_modules.include?('Huberry::Authentication::ControllerMethods')
+
+ cattr_accessor :subdomain_account_model, :subdomain_field
+ self.subdomain_account_model = 'Account'
+ self.subdomain_field = :subdomain
+
+ attr_accessor :current_account
+ helper_method :current_account
+ end
+ end
+
+ module InstanceMethods
+ protected
+ def root_url_with_subdomain(subdomain = request.subdomains.first.to_s, use_ssl = request.ssl?)
+ (use_ssl ? 'https://' : 'http://') + host_with_subdomain(subdomain)
+ end
+
+ def host_with_subdomain(subdomain = request.subdomains.first.to_s)
+ subdomain + '.' + request.domain
+ end
+
+ def account_domain
+ account_domain = ''
+ account_domain << request.subdomains[1..-1].join('.') + '.' if request.subdomains.size > 1
+ account_domain << request.domain + request.port_string
+ end
+
+ def find_current_account(force_query = false)
+ if @queried_for_current_account.nil? || force_query
+ account_class = self.class.subdomain_account_model.to_s.constantize
+ self.current_account = account_class.send "find_by_#{self.class.subdomain_field}", request.subdomains.first.to_s
+ end
+ self.current_account
+ end
+
+ def subdomain_account_required
+ if find_current_account.nil?
+ respond_to do |format|
+ format.html { render :file => "#{RAILS_ROOT}/public/404.html", :status => 404 }
+ format.all { render :nothing => true, :status => 404 }
+ end
+ end
+ end
+ end
+
+ module AuthenticationPatch
+ protected
+ def find_current_user(force_query = false)
+ if @queried_for_current_user || force_query
+ users = self.class.authentication_model.to_s.underscore.pluralize
+ self.current_user = self.current_account.send(users).find(session[:user_id]) rescue nil
+ end
+ self.current_user
+ end
+ end
+ end
+ end
+end
73 test/functional_test.rb
@@ -0,0 +1,73 @@
+require File.dirname(__FILE__) + '/init'
+
+class Account < ActiveRecord::Base
+ has_many :users
+end
+
+class User < ActiveRecord::Base
+ belongs_to :account
+end
+
+class TestController < ActionController::Base
+ before_filter :subdomain_account_required, :only => :account_required
+
+ def account_required
+ render :text => 'test'
+ end
+
+ def account_not_required
+ render :text => 'test'
+ end
+
+ def rescue_action(e)
+ raise e
+ end
+end
+
+ActionController::Routing::Routes.append do |map|
+ map.connect 'account_required', :controller => 'test', :action => 'account_required', :format => 'xml'
+ map.connect 'account_not_required', :controller => 'test', :action => 'account_not_required'
+end
+
+class FunctionalTest < Test::Unit::TestCase
+
+ def setup
+ create_accounts_table
+ create_users_table
+
+ @account = create_account
+ @user = @account.users.create(valid_user_hash)
+
+ @controller = TestController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @controller.instance_variable_set('@_session', @request.session)
+ end
+
+ def teardown
+ drop_all_tables
+ end
+
+ def test_should_get_account_required
+ @request.host = "#{@account.subdomain}.localhost.com"
+ get :account_required, :format => 'xml'
+ assert_response :success
+ end
+
+ def test_should_get_account_not_required
+ get :account_not_required
+ assert_response :success
+ end
+
+ def test_should_render_404_if_account_not_found
+ @request.host = 'localhost.com'
+ get :account_required, :format => 'xml'
+ assert_response :not_found
+
+ @request.host = 'invalid.localhost.com'
+ get :account_required, :format => 'xml'
+ assert_response :not_found
+ end
+
+end
11 test/helpers/account_test_helper.rb
@@ -0,0 +1,11 @@
+module UserTestHelper
+
+ def create_account(options = {})
+ Account.create(valid_account_hash(options))
+ end
+
+ def valid_account_hash(options = {})
+ { :subdomain => 'test' }.merge(options)
+ end
+
+end
36 test/helpers/table_test_helper.rb
@@ -0,0 +1,36 @@
+module TableTestHelper
+
+ def create_accounts_table
+ silence_stream(STDOUT) do
+ ActiveRecord::Schema.define(:version => 1) do
+ create_table :accounts do |t|
+ t.string :subdomain
+ end
+ end
+ end
+ end
+
+ def create_users_table
+ silence_stream(STDOUT) do
+ ActiveRecord::Schema.define(:version => 1) do
+ create_table :users do |t|
+ t.integer :account_id
+ t.string :email
+ t.string :hashed_password
+ t.string :salt
+ end
+ end
+ end
+ end
+
+ def drop_all_tables
+ ActiveRecord::Base.connection.tables.each do |table|
+ drop_table(table)
+ end
+ end
+
+ def drop_table(table)
+ ActiveRecord::Base.connection.drop_table(table)
+ end
+
+end
11 test/helpers/user_test_helper.rb
@@ -0,0 +1,11 @@
+module UserTestHelper
+
+ def create_user(options = {})
+ User.create(valid_user_hash(options))
+ end
+
+ def valid_user_hash(options = {})
+ { :email => 'test@test.com' }.merge(options)
+ end
+
+end
40 test/init.rb
@@ -0,0 +1,40 @@
+$:.reject! { |path| path.include? 'TextMate' }
+require 'test/unit'
+
+# Require and include test helpers
+#
+Dir[File.join(File.dirname(__FILE__), 'helpers', '*_test_helper.rb')].each do |helper|
+ require helper
+ /(.*?)_test_helper\.rb/.match File.basename(helper)
+ class_name = $1.split('_').collect{ |name| name.downcase.capitalize }.join('') + 'TestHelper'
+ Test::Unit::TestCase.send :include, Object.const_get(class_name) if Object.const_defined?(class_name)
+end
+
+# Load ActiveRecord
+#
+require 'rubygems'
+gem 'activerecord'
+require 'active_record'
+ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :dbfile => ':memory:'
+
+# Load ActionPack
+#
+gem 'actionpack'
+require 'action_pack'
+require 'action_controller'
+require 'action_controller/routing'
+require 'action_controller/assertions'
+require 'action_controller/test_process'
+
+# Routing
+#
+class ActionController::Routing::RouteSet
+ def append
+ yield Mapper.new(self)
+ install_helpers
+ end
+end
+
+# Require the main init.rb for the plugin
+#
+require File.join(File.dirname(File.dirname(__FILE__)), 'init')
Please sign in to comment.
Something went wrong with that request. Please try again.