Permalink
Browse files

[#3015] Send users with the specific Roles to their Project overview.

* Added UserHomepagesController#show which will check if a user has any Roles
  configured for a redirect and if so, redirect them to the project overview
* Remapped the Redmine core routes to use UserHomepagesController#show as the
  root
* Added a route for the WelcomeController at /welcome
  • Loading branch information...
1 parent b6b0cc3 commit 4e8023a5605b4fca2a839b796ba11a4916aa9cec @edavis10 committed Sep 12, 2009
@@ -0,0 +1,28 @@
+class UserHomepagesController < ApplicationController
+ unloadable
+ before_filter :redirect_to_welcome_if_anonymous
+
+ def show
+ user_homepage_roles = Role.find_all_by_id(Setting.plugin_redmine_user_homepage['roles'])
+
+ memberships = User.current.memberships.all(:include => 'roles')
+ if memberships.any? {|membership|
+ membership.roles.any? {|role| user_homepage_roles.include?(role) }
+ }
+ redirect_to :controller => 'projects', :action => 'show', :id => User.current.projects.first(:order => 'id ASC')
+ else
+ redirect_to_welcome
+ end
+
+ end
+
+ private
+
+ def redirect_to_welcome
+ redirect_to :controller => 'welcome', :action => 'index'
+ end
+
+ def redirect_to_welcome_if_anonymous
+ redirect_to_welcome unless User.current.logged?
+ end
+end
@@ -0,0 +1,2 @@
+module UserHomepagesHelper
+end
View
@@ -0,0 +1,4 @@
+# Remap home to use user_homepages but give welcome a high presedence
+# route, otherwise it will never map and cause a redirect loop
+home '', :controller => 'user_homepages', :action => 'show'
+connect 'welcome', :controller => 'welcome'
@@ -0,0 +1,107 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class UserHomepagesControllerTest < ActionController::TestCase
+ def get_homepage
+ get :show
+ end
+
+ def login_as_user(user)
+ @request.session[:user_id] = user.id
+ end
+
+ context "routing" do
+ should_route :get, "/", :controller => 'user_homepages', :action => 'show'
+ should_route :get, "/welcome", :controller => 'welcome', :action => 'index'
+
+ should "generate home_path" do
+ assert_equal "/", home_path
+ end
+ end
+
+ context "GET show as an anonymous user" do
+ setup do
+ setup_plugin_configuration
+ get_homepage
+ end
+
+ should_use_the_default_homepage
+ end
+
+ context "GET show as User without any projects" do
+ setup do
+ setup_plugin_configuration
+ @user = User.generate_with_protected!
+ login_as_user(@user)
+
+ get_homepage
+ end
+
+ should_use_the_default_homepage
+ end
+
+ context "GET show as User with one project" do
+ setup do
+ setup_plugin_configuration
+ @user = User.generate_with_protected!
+ login_as_user(@user)
+ @project = Project.generate!
+ end
+
+ context "with a configured 'user homepage' Role" do
+ setup do
+ Member.generate!(:user_id => @user.id, :project_id => @project.id, :role_ids => [@configured_role_one.id])
+ get_homepage
+ end
+
+ should_redirect_to("the project overview") { {:controller => 'projects', :action => 'show', :id => @project.identifier} }
+ end
+
+ context "without a configured 'user homepage' Role" do
+ setup do
+ Member.generate!(:user => @user, :project => @project, :roles => [@nonconfigured_role_one])
+ get_homepage
+ end
+
+ should_use_the_default_homepage
+ end
+ end
+
+ context "GET show as User with many projects" do
+ setup do
+ setup_plugin_configuration
+ @user = User.generate_with_protected!
+ login_as_user(@user)
+ @project = Project.generate!
+ @project_two = Project.generate!
+ @project_three = Project.generate!
+
+ # TODO: Object daddy is saving the projects but the instance
+ # isn't reloading with the new identifier/id/name so it's
+ # failing tests
+ [@project, @project_two, @project_three].each { |p| p.reload }
+ Member.generate!(:user => @user, :project => @project, :roles => [@nonconfigured_role_one])
+ Member.generate!(:user => @user, :project => @project_two, :roles => [@nonconfigured_role_one])
+ end
+
+ context "with a configured 'user homepage' Role on any project" do
+ setup do
+ Member.generate!(:user_id => @user.id, :project_id => @project_three.id, :role_ids => [@configured_role_one.id])
+ get_homepage
+ [@project, @project_two, @project_three].each do |p|
+ p.reload
+ end
+ end
+
+ should_redirect_to("the first project overview") { { :controller => 'projects', :action => 'show', :id => @project.identifier } }
+ end
+
+ context "without a configured 'user homepage' Role" do
+ setup do
+ get_homepage
+ end
+
+ should_use_the_default_homepage
+ end
+
+ end
+end
@@ -0,0 +1,22 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ProjectOverviewHooksTest < ActionController::IntegrationTest
+ should "change the homepage to the project overview once a user with the 'user homepage' Role logs in" do
+ setup_plugin_configuration
+ user = User.generate_with_protected!(:password => 'test', :password_confirmation => 'test')
+ user.reload
+ assert user.valid?, user.errors.full_messages
+ project = Project.generate!
+ Member.generate!(:user_id => user.id, :project_id => project.id, :role_ids => [@configured_role_one.id])
+
+ get '/'
+ assert_response :redirect
+ assert_redirected_to '/welcome'
+
+ log_user(user.login, 'test')
+
+ get '/'
+ assert_response :redirect
+ assert_redirected_to "/projects/#{project.identifier}"
+ end
+end
View
@@ -28,7 +28,20 @@ def configure_plugin(fields={})
end
def setup_plugin_configuration
+ @configured_role_one = Role.generate!
+ assert @configured_role_one.valid?
+ @configured_role_two = Role.generate!
+ @nonconfigured_role_one = Role.generate!
+
configure_plugin({
+ 'roles' => [@configured_role_one.id, @configured_role_two.id]
})
end
end
+
+# Shoulda
+class Test::Unit::TestCase
+ def self.should_use_the_default_homepage
+ should_redirect_to("the default homepage") { { :controller => 'welcome', :action => 'index' } }
+ end
+end

0 comments on commit 4e8023a

Please sign in to comment.