diff --git a/app/controllers/profile_admin_controller.rb b/app/controllers/profile_admin_controller.rb index 88ffaf4..e08d798 100644 --- a/app/controllers/profile_admin_controller.rb +++ b/app/controllers/profile_admin_controller.rb @@ -1,2 +1,21 @@ class ProfileAdminController < ApplicationController + + # declares that the controller needs an specific type of profile. Example: + # + # class PersonDetailControlles < ProfileAdminController + # requires_profile_class Person + # end + # + # The above controller will reject every request to it unless the current + # profile (as indicated by the first URL component) is of class Person (or of + # a subclass of Person) + def self.requires_profile_class(some_class) + before_filter do |controller| + unless controller.profile.kind_of?(some_class) + controller.instance_variable_set('@message', _("This action is not available for \"%s\".") % controller.profile.name) + controller.render :file => 'app/views/shared/access_denied.rhtml' , :layout => true, :status => 403 + end + end + end + end diff --git a/test/functional/profile_admin_controller_test.rb b/test/functional/profile_admin_controller_test.rb index 51a4bd8..a1bce3f 100644 --- a/test/functional/profile_admin_controller_test.rb +++ b/test/functional/profile_admin_controller_test.rb @@ -4,15 +4,36 @@ require 'profile_admin_controller' # Re-raise errors caught by the controller. class ProfileAdminController; def rescue_action(e) raise e end; end +class OnlyForPersonTestController < ProfileAdminController + requires_profile_class Person + def index + render :text => '
something
' + end +end + class ProfileAdminControllerTest < Test::Unit::TestCase + def setup @controller = ProfileAdminController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end - # Replace this with your real tests. - def test_truth - assert true + def test_should_allow_person + @controller = OnlyForPersonTestController.new + person = Person.new(:name => 'Random Joe') + @controller.stubs(:profile).returns(person) + + get :index + assert_response :success + end + + def test_should_not_allow_bare_profile + @controller = OnlyForPersonTestController.new + org = Organization.new(:name => 'Hacking Institute') + @controller.stubs(:profile).returns(org) + + get :index + assert_response 403 # forbidden end end -- libgit2 0.21.2