Commit 964df6262204b7e2c97ed93be0c3d00774baa463

Authored by Daniela Feitosa
2 parents 6ed2fbe7 bec2b3cd

Merge branch 'signup_redirect_improvements' into 'master'

Signup Redirect Improvements

Add environment feature for choosing where to redirect the user after signup. The options are the same as the ones for the login redirection options existing today.

This feature takes in consideration that the usar may have to follow a confirmation URL recieved by eletronic mail.
app/controllers/public/account_controller.rb
... ... @@ -69,6 +69,8 @@ class AccountController < ApplicationController
69 69 session[:notice] = _("This environment doesn't allow user registration.")
70 70 end
71 71  
  72 + store_location(request.referer) unless params[:return_to] or session[:return_to]
  73 +
72 74 @block_bot = !!session[:may_be_a_bot]
73 75 @invitation_code = params[:invitation_code]
74 76 begin
... ... @@ -77,6 +79,7 @@ class AccountController < ApplicationController
77 79 @user.environment = environment
78 80 @terms_of_use = environment.terms_of_use
79 81 @user.person_data = params[:profile_data]
  82 + @user.return_to = session[:return_to]
80 83 @person = Person.new(params[:profile_data])
81 84 @person.environment = @user.environment
82 85 if request.post?
... ... @@ -98,7 +101,7 @@ class AccountController < ApplicationController
98 101 end
99 102 if @user.activated?
100 103 self.current_user = @user
101   - redirect_to '/'
  104 + go_to_signup_initial_page
102 105 else
103 106 @register_pending = true
104 107 end
... ... @@ -368,7 +371,14 @@ class AccountController < ApplicationController
368 371 end
369 372  
370 373 def go_to_initial_page
  374 + if params[:redirection]
  375 + session[:return_to] = @user.return_to
  376 + @user.return_to = nil
  377 + @user.save
  378 + end
  379 +
371 380 if params[:return_to]
  381 + #I never get here
372 382 redirect_to params[:return_to]
373 383 elsif environment.enabled?('allow_change_of_redirection_after_login')
374 384 case user.preferred_login_redirection
... ... @@ -387,13 +397,43 @@ class AccountController < ApplicationController
387 397 end
388 398 else
389 399 if environment == current_user.environment
390   - redirect_back_or_default(user.admin_url)
  400 + case environment.redirection_after_login
  401 + when 'keep_on_same_page'
  402 + redirect_back_or_default(user.admin_url)
  403 + when 'site_homepage'
  404 + redirect_to :controller => :home
  405 + when 'user_profile_page'
  406 + redirect_to user.public_profile_url
  407 + when 'user_homepage'
  408 + redirect_to user.url
  409 + when 'user_control_panel'
  410 + redirect_to user.admin_url
  411 + else
  412 + redirect_back_or_default(user.admin_url)
  413 + end
391 414 else
392 415 redirect_back_or_default(:controller => 'home')
393 416 end
394 417 end
395 418 end
396 419  
  420 + def go_to_signup_initial_page
  421 + case @user.environment.redirection_after_signup
  422 + when 'keep_on_same_page'
  423 + redirect_back_or_default(user.admin_url)
  424 + when 'site_homepage'
  425 + redirect_to :controller => :home
  426 + when 'user_profile_page'
  427 + redirect_to user.public_profile_url
  428 + when 'user_homepage'
  429 + redirect_to user.url
  430 + when 'user_control_panel'
  431 + redirect_to user.admin_url
  432 + else
  433 + redirect_to user.url
  434 + end
  435 + end
  436 +
397 437 def redirect_if_logged_in
398 438 if logged_in?
399 439 go_to_initial_page
... ...
app/models/environment.rb
... ... @@ -145,6 +145,18 @@ class Environment < ActiveRecord::Base
145 145 end
146 146 validates_inclusion_of :redirection_after_login, :in => Environment.login_redirection_options.keys, :allow_nil => true
147 147  
  148 + def self.signup_redirection_options
  149 + {
  150 + 'keep_on_same_page' => _('Stays on the same page the user was before signup.'),
  151 + 'site_homepage' => _('Redirects the user to the environment homepage.'),
  152 + 'user_profile_page' => _('Redirects the user to his profile page.'),
  153 + 'user_homepage' => _('Redirects the user to his homepage.'),
  154 + 'user_control_panel' => _('Redirects the user to his control panel.')
  155 + }
  156 + end
  157 + validates_inclusion_of :redirection_after_signup, :in => Environment.signup_redirection_options.keys, :allow_nil => true
  158 +
  159 +
148 160 # #################################################
149 161 # Relationships and applied behaviour
150 162 # #################################################
... ...
app/models/user.rb
... ... @@ -71,7 +71,8 @@ class User < ActiveRecord::Base
71 71 body :recipient => user.name,
72 72 :activation_code => user.activation_code,
73 73 :environment => user.environment.name,
74   - :url => user.environment.top_url
  74 + :url => user.environment.top_url,
  75 + :return_to => user.return_to
75 76 end
76 77  
77 78 def signup_welcome_email(user)
... ... @@ -93,7 +94,7 @@ class User < ActiveRecord::Base
93 94 self.person.save!
94 95 end
95 96 end
96   -
  97 +
97 98 has_one :person, :dependent => :destroy
98 99 belongs_to :environment
99 100  
... ... @@ -183,7 +184,7 @@ class User < ActiveRecord::Base
183 184 encryption_methods[sym] = block
184 185 end
185 186  
186   - # the encryption method used for this instance
  187 + # the encryption method used for this instance
187 188 def encryption_method
188 189 (password_type || User.system_encryption_method).to_sym
189 190 end
... ... @@ -226,7 +227,7 @@ class User < ActiveRecord::Base
226 227 end
227 228  
228 229 def remember_token?
229   - remember_token_expires_at && Time.now.utc < remember_token_expires_at
  230 + remember_token_expires_at && Time.now.utc < remember_token_expires_at
230 231 end
231 232  
232 233 # These create and unset the fields required for remembering users between browser closes
... ... @@ -255,7 +256,7 @@ class User &lt; ActiveRecord::Base
255 256 raise IncorrectPassword unless self.authenticated?(current)
256 257 self.force_change_password!(new, confirmation)
257 258 end
258   -
  259 +
259 260 # Changes the password of a user without asking for the old password. This
260 261 # method is intended to be used by the "I forgot my password", and must be
261 262 # used with care.
... ... @@ -326,7 +327,7 @@ class User &lt; ActiveRecord::Base
326 327 end
327 328  
328 329 protected
329   - # before filter
  330 + # before filter
330 331 def encrypt_password
331 332 return if password.blank?
332 333 self.salt ||= Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
... ...
app/views/features/index.rhtml
... ... @@ -26,9 +26,13 @@ Check all the features you want to enable for your environment, uncheck all the
26 26  
27 27 <h2><%= _('Configure features') %></h2>
28 28  
  29 +<h3><%= _('Page to redirect after signup') %></h3>
  30 + <%= select 'environment', 'redirection_after_signup', Environment.signup_redirection_options.map{|key,value|[value,key]} %>
  31 +<hr/>
29 32 <h3><%= _('Page to redirect after login') %></h3>
30 33 <%= select 'environment', 'redirection_after_login', Environment.login_redirection_options.map{|key,value|[value,key]} %>
31 34 <hr/>
  35 +
32 36 <h3><%= _('Organization Approval Method') %></h3>
33 37 <%= select_organization_approval_method('environment', 'organization_approval_method') %>
34 38 <hr/>
... ...
app/views/user/mailer/activation_code.rhtml
1 1 <%= _('Hi, %{recipient}!') % { :recipient => @recipient } %>
2 2  
3   -<%= word_wrap(_('Welcome to %{environment}! To activate your account, follow the link: %{activation_url}') % { :environment => @environment, :activation_url => @url + url_for(:controller => :account, :action => :activate, :activation_code => @activation_code) }) %>
  3 +<%= word_wrap(_('Welcome to %{environment}! To activate your account, follow the link: %{activation_url}') % { :environment => @environment, :activation_url => @url + url_for(:controller => :account, :action => :activate, :activation_code => @activation_code, :redirection => (true if @return_to) ) }) %>
4 4  
5 5 <%= _("Greetings,") %>
6 6  
... ...
db/migrate/20140205191914_add_redirection_after_signup_to_environment.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddRedirectionAfterSignupToEnvironment < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :environments, :redirection_after_signup, :string, :default => 'keep_on_same_page'
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :environments, :redirection_after_signup
  8 + end
  9 +end
... ...
db/migrate/20140312184749_add_return_to_to_users.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddReturnToToUsers < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :users, :return_to, :string
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :users, :return_to, :string
  8 + end
  9 +end
... ...
db/schema.rb
1   -# This file is auto-generated from the current state of the database. Instead of editing this file,
  1 +# This file is auto-generated from the current state of the database. Instead of editing this file,
2 2 # please use the migrations feature of Active Record to incrementally modify your database, and
3 3 # then regenerate this schema definition.
4 4 #
... ... @@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 20140108132730) do
  12 +ActiveRecord::Schema.define(:version => 20140312184749) do
13 13  
14 14 create_table "abuse_reports", :force => true do |t|
15 15 t.integer "reporter_id"
... ... @@ -282,6 +282,7 @@ ActiveRecord::Schema.define(:version =&gt; 20140108132730) do
282 282 t.text "signup_welcome_text"
283 283 t.string "languages"
284 284 t.string "default_language"
  285 + t.string "redirection_after_signup", :default => "keep_on_same_page"
285 286 end
286 287  
287 288 create_table "external_feeds", :force => true do |t|
... ... @@ -619,6 +620,7 @@ ActiveRecord::Schema.define(:version =&gt; 20140108132730) do
619 620 t.datetime "chat_status_at"
620 621 t.string "activation_code", :limit => 40
621 622 t.datetime "activated_at"
  623 + t.string "return_to"
622 624 end
623 625  
624 626 create_table "validation_infos", :force => true do |t|
... ...
features/signup.feature
... ... @@ -3,7 +3,7 @@ Feature: signup
3 3 I want to sign up to the site
4 4 So I can have fun using its features
5 5  
6   -@selenium
  6 + @selenium
7 7 Scenario: successfull registration
8 8 Given I am on the homepage
9 9 When I follow "Login"
... ... @@ -60,3 +60,183 @@ Feature: signup
60 60 And I fill in "Name" with ""
61 61 When I press "Save"
62 62 Then I should see "Name can't be blank"
  63 +
  64 + @selenium
  65 + Scenario: user should stay on same page after signup
  66 + Given the environment is configured to stay on the same page after signup
  67 + And feature "skip_new_user_email_confirmation" is enabled on environment
  68 + And I am on /search/people
  69 + When I follow "Sign up"
  70 + And I fill in the following within ".no-boxes":
  71 + | e-Mail | josesilva@example.com |
  72 + | Username | josesilva |
  73 + | Password | secret |
  74 + | Password confirmation | secret |
  75 + | Full name | José da Silva |
  76 + And wait for the captcha signup time
  77 + And I press "Create my account"
  78 + Then I should be on /search/people
  79 +
  80 + @selenium
  81 + Scenario: user should go to his homepage after signup
  82 + Given the environment is configured to redirect to profile homepage after signup
  83 + And feature "skip_new_user_email_confirmation" is enabled on environment
  84 + And I am on /search/people
  85 + When I follow "Sign up"
  86 + And I fill in the following within ".no-boxes":
  87 + | e-Mail | josesilva@example.com |
  88 + | Username | josesilva |
  89 + | Password | secret |
  90 + | Password confirmation | secret |
  91 + | Full name | José da Silva |
  92 + And wait for the captcha signup time
  93 + And I press "Create my account"
  94 + Then I should be on josesilva's profile
  95 +
  96 + @selenium
  97 + Scenario: user should go to his control panel after signup
  98 + Given the environment is configured to redirect to profile control panel after signup
  99 + And feature "skip_new_user_email_confirmation" is enabled on environment
  100 + And I am on /search/people
  101 + When I follow "Sign up"
  102 + And I fill in the following within ".no-boxes":
  103 + | e-Mail | josesilva@example.com |
  104 + | Username | josesilva |
  105 + | Password | secret |
  106 + | Password confirmation | secret |
  107 + | Full name | José da Silva |
  108 + And wait for the captcha signup time
  109 + And I press "Create my account"
  110 + Then I should be on josesilva's control panel
  111 +
  112 + @selenium
  113 + Scenario: user should go to his profile page after signup
  114 + Given the environment is configured to redirect to user profile page after signup
  115 + And feature "skip_new_user_email_confirmation" is enabled on environment
  116 + And I am on /search/people
  117 + When I follow "Sign up"
  118 + And I fill in the following within ".no-boxes":
  119 + | e-Mail | josesilva@example.com |
  120 + | Username | josesilva |
  121 + | Password | secret |
  122 + | Password confirmation | secret |
  123 + | Full name | José da Silva |
  124 + And wait for the captcha signup time
  125 + And I press "Create my account"
  126 + Then I should be on josesilva's profile
  127 +
  128 + @selenium
  129 + Scenario: user should go to the environment's homepage after signup
  130 + Given the environment is configured to redirect to site homepage after signup
  131 + And feature "skip_new_user_email_confirmation" is enabled on environment
  132 + And I am on /search/people
  133 + When I follow "Sign up"
  134 + And I fill in the following within ".no-boxes":
  135 + | e-Mail | josesilva@example.com |
  136 + | Username | josesilva |
  137 + | Password | secret |
  138 + | Password confirmation | secret |
  139 + | Full name | José da Silva |
  140 + And wait for the captcha signup time
  141 + And I press "Create my account"
  142 + Then I should be on the homepage
  143 +
  144 + @selenium
  145 + Scenario: user should stay on same page after following confirmation link
  146 + Given the environment is configured to stay on the same page after login
  147 + And feature "skip_new_user_email_confirmation" is disabled on environment
  148 + And I am on /search/people
  149 + When I follow "Sign up"
  150 + And I fill in the following within ".no-boxes":
  151 + | e-Mail | josesilva@example.com |
  152 + | Username | josesilva |
  153 + | Password | secret |
  154 + | Password confirmation | secret |
  155 + | Full name | José da Silva |
  156 + And wait for the captcha signup time
  157 + And I press "Create my account"
  158 + And I go to josesilva's confirmation URL
  159 + And I fill in "Username" with "josesilva"
  160 + And I fill in "Password" with "secret"
  161 + And I press "Log in"
  162 + Then I should be on /search/people
  163 +
  164 + @selenium
  165 + Scenario: user should go to his homepage after following confirmation link
  166 + Given the environment is configured to redirect to profile homepage after login
  167 + And feature "skip_new_user_email_confirmation" is disabled on environment
  168 + And I am on /search/people
  169 + When I follow "Sign up"
  170 + And I fill in the following within ".no-boxes":
  171 + | e-Mail | josesilva@example.com |
  172 + | Username | josesilva |
  173 + | Password | secret |
  174 + | Password confirmation | secret |
  175 + | Full name | José da Silva |
  176 + And wait for the captcha signup time
  177 + And I press "Create my account"
  178 + And I go to josesilva's confirmation URL
  179 + And I fill in "Username" with "josesilva"
  180 + And I fill in "Password" with "secret"
  181 + And I press "Log in"
  182 + Then I should be on /profile/josesilva
  183 +
  184 + @selenium
  185 + Scenario: user should go to his control panel after following confirmation link
  186 + Given the environment is configured to redirect to profile control panel after login
  187 + And feature "skip_new_user_email_confirmation" is disabled on environment
  188 + And I am on /search/people
  189 + When I follow "Sign up"
  190 + And I fill in the following within ".no-boxes":
  191 + | e-Mail | josesilva@example.com |
  192 + | Username | josesilva |
  193 + | Password | secret |
  194 + | Password confirmation | secret |
  195 + | Full name | José da Silva |
  196 + And wait for the captcha signup time
  197 + And I press "Create my account"
  198 + And I go to josesilva's confirmation URL
  199 + And I fill in "Username" with "josesilva"
  200 + And I fill in "Password" with "secret"
  201 + And I press "Log in"
  202 + Then I should be on /myprofile/josesilva
  203 +
  204 + @selenium
  205 + Scenario: user should go to his profile page after following confirmation link
  206 + Given the environment is configured to redirect to user profile page after login
  207 + And feature "skip_new_user_email_confirmation" is disabled on environment
  208 + And I am on /search/people
  209 + When I follow "Sign up"
  210 + And I fill in the following within ".no-boxes":
  211 + | e-Mail | josesilva@example.com |
  212 + | Username | josesilva |
  213 + | Password | secret |
  214 + | Password confirmation | secret |
  215 + | Full name | José da Silva |
  216 + And wait for the captcha signup time
  217 + And I press "Create my account"
  218 + And I go to josesilva's confirmation URL
  219 + And I fill in "Username" with "josesilva"
  220 + And I fill in "Password" with "secret"
  221 + And I press "Log in"
  222 + Then I should be on /profile/josesilva
  223 +
  224 + @selenium
  225 + Scenario: user should go to the environment homepage after following confirmation link
  226 + Given the environment is configured to redirect to site homepage after login
  227 + And feature "skip_new_user_email_confirmation" is disabled on environment
  228 + And I am on /search/people
  229 + When I follow "Sign up"
  230 + And I fill in the following within ".no-boxes":
  231 + | e-Mail | josesilva@example.com |
  232 + | Username | josesilva |
  233 + | Password | secret |
  234 + | Password confirmation | secret |
  235 + | Full name | José da Silva |
  236 + And wait for the captcha signup time
  237 + And I press "Create my account"
  238 + And I go to josesilva's confirmation URL
  239 + And I fill in "Username" with "josesilva"
  240 + And I fill in "Password" with "secret"
  241 + And I press "Log in"
  242 + Then I should be on the homepage
... ...
features/step_definitions/noosfero_steps.rb
... ... @@ -738,6 +738,24 @@ Given /^the profile (.*) is configured to (.*) after login$/ do |profile, option
738 738 profile.save
739 739 end
740 740  
  741 +Given /^the environment is configured to (.*) after signup$/ do |option|
  742 + redirection = case option
  743 + when 'stay on the same page'
  744 + 'keep_on_same_page'
  745 + when 'redirect to site homepage'
  746 + 'site_homepage'
  747 + when 'redirect to user profile page'
  748 + 'user_profile_page'
  749 + when 'redirect to profile homepage'
  750 + 'user_homepage'
  751 + when 'redirect to profile control panel'
  752 + 'user_control_panel'
  753 + end
  754 + environment = Environment.default
  755 + environment.redirection_after_signup = redirection
  756 + environment.save
  757 +end
  758 +
741 759 When /^wait for the captcha signup time$/ do
742 760 environment = Environment.default
743 761 sleep environment.min_signup_delay + 1
... ...
features/support/paths.rb
... ... @@ -111,6 +111,10 @@ module NavigationHelpers
111 111 when /the user data path/
112 112 '/account/user_data'
113 113  
  114 + when /^(.+)'s confirmation URL/
  115 + user = User[$1]
  116 + "/account/activate?activation_code=#{user.activation_code}&redirection=" + (user.return_to.nil? ? 'false' : 'true')
  117 +
114 118 when /^(.+)'s members page/
115 119 '/profile/%s/members' % profile_identifier($1)
116 120  
... ...
test/unit/environment_test.rb
... ... @@ -1220,6 +1220,27 @@ class EnvironmentTest &lt; ActiveSupport::TestCase
1220 1220 end
1221 1221 end
1222 1222  
  1223 + should 'return a Hash on signup redirection options' do
  1224 + assert_kind_of Hash, Environment.signup_redirection_options
  1225 + end
  1226 +
  1227 + should 'respond to redirection after signup' do
  1228 + assert_respond_to Environment.new, :redirection_after_signup
  1229 + end
  1230 +
  1231 + should 'allow only environment signup redirection options' do
  1232 + environment = fast_create(Environment)
  1233 + environment.redirection_after_signup = 'invalid_option'
  1234 + environment.save
  1235 + assert environment.errors.invalid?(:redirection_after_signup)
  1236 +
  1237 + Environment.signup_redirection_options.keys.each do |redirection|
  1238 + environment.redirection_after_signup = redirection
  1239 + environment.save
  1240 + assert !environment.errors.invalid?(:redirection_after_signup)
  1241 + end
  1242 + end
  1243 +
1223 1244 should 'respond to signup_welcome_text' do
1224 1245 assert_respond_to Environment.new, :signup_welcome_text
1225 1246 end
... ...