From bba3d1e252263fe60fbb47232671153366e57394 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Mon, 8 Oct 2012 15:26:09 -0300 Subject: [PATCH] Allowing users to configure the page after login --- app/controllers/public/account_controller.rb | 23 ++++++++++++++++++++--- app/models/environment.rb | 14 +++++++++++++- app/models/profile.rb | 4 ++++ app/views/features/index.rhtml | 17 ++++++----------- app/views/profile_editor/edit.rhtml | 5 +++++ db/migrate/20120824183534_add_redirection_after_login_to_environment.rb | 9 +++++++++ db/migrate/20120824184046_add_redirection_after_login_to_profiles.rb | 9 +++++++++ db/schema.rb | 2 ++ features/login.feature | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- features/step_definitions/noosfero_steps.rb | 36 ++++++++++++++++++++++++++++++++++++ test/functional/profile_editor_controller_test.rb | 17 +++++++++++++++++ test/unit/environment_test.rb | 22 ++++++++++++++++++++++ test/unit/profile_test.rb | 29 +++++++++++++++++++++++++++++ 13 files changed, 321 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20120824183534_add_redirection_after_login_to_environment.rb create mode 100644 db/migrate/20120824184046_add_redirection_after_login_to_profiles.rb diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index cca9cad..a854471 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -313,10 +313,27 @@ class AccountController < ApplicationController end def go_to_initial_page - if environment == current_user.environment - redirect_back_or_default(user.admin_url) + if environment.enabled?('allow_change_of_redirection_after_login') + case user.preferred_login_redirection + when 'keep_on_same_page' + redirect_back_or_default(user.admin_url) + when 'site_homepage' + redirect_to :controller => :home + when 'user_profile_page' + redirect_to user.public_profile_url + when 'user_homepage' + redirect_to user.url + when 'user_control_panel' + redirect_to user.admin_url + else + redirect_back_or_default(user.admin_url) + end else - redirect_back_or_default(:controller => 'home') + if environment == current_user.environment + redirect_back_or_default(user.admin_url) + else + redirect_back_or_default(:controller => 'home') + end end end diff --git a/app/models/environment.rb b/app/models/environment.rb index aa92b52..b02db16 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -123,10 +123,22 @@ class Environment < ActiveRecord::Base 'xmpp_chat' => _('XMPP/Jabber based chat'), 'show_zoom_button_on_article_images' => _('Show a zoom link on all article images'), 'captcha_for_logged_users' => _('Ask captcha when a logged user comments too'), - 'skip_new_user_email_confirmation' => _('Skip e-mail confirmation for new users') + 'skip_new_user_email_confirmation' => _('Skip e-mail confirmation for new users'), + 'allow_change_of_redirection_after_login' => _('Allow users to set the page to redirect after login') } end + def self.login_redirection_options + { + 'keep_on_same_page' => _('Stays on the same page the user was before login.'), + 'site_homepage' => _('Redirects the user to the environment homepage.'), + 'user_profile_page' => _('Redirects the user to his profile page.'), + 'user_homepage' => _('Redirects the user to his homepage.'), + 'user_control_panel' => _('Redirects the user to his control panel.') + } + end + validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true + # ################################################# # Relationships and applied behaviour # ################################################# diff --git a/app/models/profile.rb b/app/models/profile.rb index efdfb55..2db43c1 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -972,4 +972,8 @@ private :generate_url, :url_options end end + validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true + def preferred_login_redirection + redirection_after_login.blank? ? environment.redirection_after_login : redirection_after_login + end end diff --git a/app/views/features/index.rhtml b/app/views/features/index.rhtml index f15b06e..796e632 100644 --- a/app/views/features/index.rhtml +++ b/app/views/features/index.rhtml @@ -26,17 +26,12 @@ Check all the features you want to enable for your environment, uncheck all the

<%= _('Configure features') %>

- - - - - - - - - -
<%= _('Option') %><%= _('Choice') %>
<%= _('Organization Approval Method') %><%= select_organization_approval_method('environment', 'organization_approval_method') %>
- +

<%= _('Page to redirect after login') %>

+ <%= select 'environment', 'redirection_after_login', Environment.login_redirection_options.map{|key,value|[value,key]} %> +
+

<%= _('Organization Approval Method') %>

+ <%= select_organization_approval_method('environment', 'organization_approval_method') %> +
<% button_bar do %> diff --git a/app/views/profile_editor/edit.rhtml b/app/views/profile_editor/edit.rhtml index 74b9de0..e8b7984 100644 --- a/app/views/profile_editor/edit.rhtml +++ b/app/views/profile_editor/edit.rhtml @@ -39,6 +39,11 @@
<% end %> + <% if environment.enabled?('allow_change_of_redirection_after_login') %> +

<%= _('Page to redirect after login') %>

+ <%= select 'profile_data', 'redirection_after_login', Environment.login_redirection_options.map{|key,value|[value,key]}, { :selected => @profile.preferred_login_redirection} %> + <% end %> +

<%= _('Translations') %>

<%= labelled_check_box( _('Automaticaly redirect the visitor to the article translated to his/her language'), diff --git a/db/migrate/20120824183534_add_redirection_after_login_to_environment.rb b/db/migrate/20120824183534_add_redirection_after_login_to_environment.rb new file mode 100644 index 0000000..4802ee9 --- /dev/null +++ b/db/migrate/20120824183534_add_redirection_after_login_to_environment.rb @@ -0,0 +1,9 @@ +class AddRedirectionAfterLoginToEnvironment < ActiveRecord::Migration + def self.up + add_column :environments, :redirection_after_login, :string, :default => 'keep_on_same_page' + end + + def self.down + remove_column :environments, :redirection_after_login + end +end diff --git a/db/migrate/20120824184046_add_redirection_after_login_to_profiles.rb b/db/migrate/20120824184046_add_redirection_after_login_to_profiles.rb new file mode 100644 index 0000000..218b058 --- /dev/null +++ b/db/migrate/20120824184046_add_redirection_after_login_to_profiles.rb @@ -0,0 +1,9 @@ +class AddRedirectionAfterLoginToProfiles < ActiveRecord::Migration + def self.up + add_column :profiles, :redirection_after_login, :string + end + + def self.down + remove_column :profiles, :redirection_after_login + end +end diff --git a/db/schema.rb b/db/schema.rb index ba8d295..ff1d2f3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -261,6 +261,7 @@ ActiveRecord::Schema.define(:version => 20120825185219) do t.datetime "created_at" t.datetime "updated_at" t.integer "reports_lower_bound", :default => 0, :null => false + t.string "redirection_after_login" :default => "keep_on_same_page" end create_table "external_feeds", :force => true do |t| @@ -438,6 +439,7 @@ ActiveRecord::Schema.define(:version => 20120825185219) do t.string "national_region_code" t.boolean "is_template", :default => false t.integer "template_id" + t.string "redirection_after_login" end add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" diff --git a/features/login.feature b/features/login.feature index ab047ef..b75bca7 100644 --- a/features/login.feature +++ b/features/login.feature @@ -9,7 +9,8 @@ Feature: login | joaosilva | Joao Silva | Scenario: login from portal homepage - Given I am not logged in + Given feature "allow_change_of_redirection_after_login" is disabled on environment + And I am not logged in And I go to the homepage And I fill in the following: | Username | joaosilva | @@ -19,7 +20,8 @@ Feature: login And I should be logged in as "joaosilva" Scenario: login from some profile page - Given I am not logged in + Given feature "allow_change_of_redirection_after_login" is disabled on environment + And I am not logged in And the following users | login | name | | mariasilva | Maria Silva | @@ -35,7 +37,8 @@ Feature: login Then I should be on Maria Silva's homepage Scenario: view my control panel - Given I am not logged in + Given feature "allow_change_of_redirection_after_login" is disabled on environment + And I am not logged in And I go to Joao Silva's control panel And I should be on login page And I fill in the following: @@ -48,3 +51,146 @@ Feature: login Given I am logged in as "joaosilva" And I go to login page Then I should be on Joao Silva's control panel + + Scenario: stay on the same page after login if this is the environment default + Given feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to stay on the same page after login + And the following users + | login | name | + | mariasilva | Maria Silva | + And the following articles + | owner | name | homepage | + | mariasilva | my home page | true | + And I go to Maria Silva's homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on Maria Silva's homepage + + Scenario: go to site homepage if this is the environment default + Given feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to redirect to site homepage after login + And I go to Joao Silva's homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on the homepage + + Scenario: go to user profile after login if this is the environment default + Given feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to redirect to user profile page after login + And I go to the homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on Joao Silva's profile + + Scenario: go to profile homepage after login if this is the environment default + Given the following articles + | owner | name | body | homepage | + | joaosilva | Sample Article | This is an article | true | + And feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to redirect to profile homepage after login + And I go to the homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on Joao Silva's homepage + + Scenario: go to profile control panel after login if this is the environment default + Given feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to redirect to profile control panel after login + And I go to the homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on Joao Silva's control panel + + Scenario: stay on the same page after login if this is the profile default + Given feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to redirect to site homepage after login + And the profile joaosilva is configured to stay on the same page after login + And the following users + | login | name | + | mariasilva | Maria Silva | + And the following articles + | owner | name | homepage | + | mariasilva | my home page | true | + And I go to Maria Silva's homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on Maria Silva's homepage + + Scenario: go to site homepage if this is the profile default + Given feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to stay on the same page after login + And the profile joaosilva is configured to redirect to site homepage after login + And I go to Joao Silva's homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on the homepage + + Scenario: go to user profile after login if this is the profile default + Given feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to stay on the same page after login + And the profile joaosilva is configured to redirect to user profile page after login + And I go to the homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on Joao Silva's profile + + Scenario: go to profile homepage after login if this is the profile default + Given the following articles + | owner | name | body | homepage | + | joaosilva | Sample Article | This is an article | true | + And feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to stay on the same page after login + And the profile joaosilva is configured to redirect to profile homepage after login + And I go to the homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on Joao Silva's homepage + + Scenario: go to profile control panel after login if this is the profile default + Given feature "allow_change_of_redirection_after_login" is enabled on environment + And I am not logged in + And the environment is configured to stay on the same page after login + And the profile joaosilva is configured to redirect to profile control panel after login + And I go to the homepage + And I follow "Login" + And I fill in the following: + | Username | joaosilva | + | Password | 123456 | + When I press "Log in" + Then I should be on Joao Silva's control panel diff --git a/features/step_definitions/noosfero_steps.rb b/features/step_definitions/noosfero_steps.rb index 82473c5..ef647a7 100644 --- a/features/step_definitions/noosfero_steps.rb +++ b/features/step_definitions/noosfero_steps.rb @@ -712,3 +712,39 @@ When /^I make a AJAX request to (.*)$/ do |page| header 'X-Requested-With', 'XMLHttpRequest' visit(path_to(page)) end + +Given /^the environment is configured to (.*) after login$/ do |option| + redirection = case option + when 'stay on the same page' + 'keep_on_same_page' + when 'redirect to site homepage' + 'site_homepage' + when 'redirect to user profile page' + 'user_profile_page' + when 'redirect to profile homepage' + 'user_homepage' + when 'redirect to profile control panel' + 'user_control_panel' + end + environment = Environment.default + environment.redirection_after_login = redirection + environment.save +end + +Given /^the profile (.*) is configured to (.*) after login$/ do |profile, option| + redirection = case option + when 'stay on the same page' + 'keep_on_same_page' + when 'redirect to site homepage' + 'site_homepage' + when 'redirect to user profile page' + 'user_profile_page' + when 'redirect to profile homepage' + 'user_homepage' + when 'redirect to profile control panel' + 'user_control_panel' + end + profile = Profile.find_by_identifier(profile) + profile.redirection_after_login = redirection + profile.save +end diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index be63c73..b3fbeda 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -962,4 +962,21 @@ class ProfileEditorControllerTest < ActionController::TestCase assert_no_tag :tag => 'input', :attributes => {:name => 'profile_data[is_template]'} end + should 'display select to change redirection after login if enabled' do + e = Environment.default + e.enable('allow_change_of_redirection_after_login') + e.save + + get :edit, :profile => profile.identifier + assert_tag :tag => 'select', :attributes => {:id => 'profile_data_redirection_after_login'} + end + + should 'not display select to change redirection after login if not enabled' do + e = Environment.default + e.disable('allow_change_of_redirection_after_login') + e.save + + get :edit, :profile => profile.identifier + assert_no_tag :tag => 'select', :attributes => {:id => 'profile_data_redirection_after_login'} + end end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index caa3bfb..510c595 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -1220,4 +1220,26 @@ class EnvironmentTest < ActiveSupport::TestCase assert_includes environment.licenses, l2 assert_not_includes environment.licenses, l3 end + + should 'return a Hash on login redirection options' do + assert_kind_of Hash, Environment.login_redirection_options + end + + should 'respond to redirection after login' do + assert_respond_to Environment.new, :redirection_after_login + end + + should 'allow only environment login redirection options' do + environment = fast_create(Environment) + environment.redirection_after_login = 'invalid_option' + environment.save + assert environment.errors.invalid?(:redirection_after_login) + + Environment.login_redirection_options.keys.each do |redirection| + environment.redirection_after_login = redirection + environment.save + assert !environment.errors.invalid?(:redirection_after_login) + end + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 427ffd9..29ba067 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1916,4 +1916,33 @@ class ProfileTest < ActiveSupport::TestCase profile.save! end + should 'respond to redirection_after_login' do + assert_respond_to Profile.new, :redirection_after_login + end + + should 'return profile preference of redirection unless it is blank' do + environment = fast_create(Environment, :redirection_after_login => 'site_homepage') + profile = fast_create(Profile, :redirection_after_login => 'keep_on_same_page', :environment_id => environment.id) + assert_equal 'keep_on_same_page', profile.preferred_login_redirection + end + + should 'return environment preference of redirection when profile preference is blank' do + environment = fast_create(Environment, :redirection_after_login => 'site_homepage') + profile = fast_create(Profile, :environment_id => environment.id) + assert_equal 'site_homepage', profile.preferred_login_redirection + end + + should 'allow only environment login redirection options' do + profile = fast_create(Profile) + profile.redirection_after_login = 'invalid_option' + profile.save + assert profile.errors.invalid?(:redirection_after_login) + + Environment.login_redirection_options.keys.each do |redirection| + profile.redirection_after_login = redirection + profile.save + assert !profile.errors.invalid?(:redirection_after_login) + end + end + end -- libgit2 0.21.2