Commit 3923d48a58c9dde292da58e207893f73509fc541

Authored by Daniel
1 parent 0efa7f89

social_network: Join community on login/signup

Join community if user log in or sign up after following a "Join" link within
a community.

(ActionItem3096)
app/controllers/public/account_controller.rb
... ... @@ -17,6 +17,7 @@ class AccountController < ApplicationController
17 17 @user = User.find_by_activation_code(params[:activation_code]) if params[:activation_code]
18 18 if @user and @user.activate
19 19 @message = _("Your account has been activated, now you can log in!")
  20 + session[:join] = params[:join] unless params[:join].blank?
20 21 render :action => 'login', :userlogin => @user.login
21 22 else
22 23 session[:notice] = _("It looks like you're trying to activate an account. Perhaps have already activated this account?")
... ... @@ -35,6 +36,7 @@ class AccountController < ApplicationController
35 36 self.current_user ||= User.authenticate(params[:user][:login], params[:user][:password], environment) if params[:user]
36 37  
37 38 if logged_in?
  39 + join_community(self.current_user)
38 40 if params[:remember_me] == "1"
39 41 self.current_user.remember_me
40 42 cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
... ... @@ -91,6 +93,7 @@ class AccountController < ApplicationController
91 93 if session[:may_be_a_bot]
92 94 return false unless verify_recaptcha :model=>@user, :message=>_('Captcha (the human test)')
93 95 end
  96 + @user.community_to_join = session[:join]
94 97 @user.signup!
95 98 owner_role = Role.find_by_name('owner')
96 99 @user.person.affiliate(@user.person, [owner_role]) if owner_role
... ... @@ -101,6 +104,7 @@ class AccountController < ApplicationController
101 104 end
102 105 if @user.activated?
103 106 self.current_user = @user
  107 + join_community(@user)
104 108 go_to_signup_initial_page
105 109 else
106 110 @register_pending = true
... ... @@ -448,4 +452,12 @@ class AccountController < ApplicationController
448 452 redirect_back_or_default(default)
449 453 end
450 454 end
  455 +
  456 + def join_community(user)
  457 + profile_to_join = session[:join]
  458 + unless profile_to_join.blank?
  459 + environment.profiles.find_by_identifier(profile_to_join).add_member(user.person)
  460 + session.delete(:join)
  461 + end
  462 + end
451 463 end
... ...
app/controllers/public/profile_controller.rb
... ... @@ -3,7 +3,7 @@ class ProfileController < PublicController
3 3 needs_profile
4 4 before_filter :check_access_to_profile, :except => [:join, :join_not_logged, :index, :add]
5 5 before_filter :store_location, :only => [:join, :join_not_logged, :report_abuse, :send_mail]
6   - before_filter :login_required, :only => [:add, :join, :join_not_logged, :leave, :unblock, :leave_scrap, :remove_scrap, :remove_activity, :view_more_activities, :view_more_network_activities, :report_abuse, :register_report, :leave_comment_on_activity, :send_mail]
  6 + before_filter :login_required, :only => [:add, :join, :leave, :unblock, :leave_scrap, :remove_scrap, :remove_activity, :view_more_activities, :view_more_network_activities, :report_abuse, :register_report, :leave_comment_on_activity, :send_mail]
7 7  
8 8 helper TagsHelper
9 9  
... ... @@ -97,21 +97,12 @@ class ProfileController < PublicController
97 97 end
98 98  
99 99 def join_not_logged
100   - if request.post?
101   - profile.add_member(user)
102   - session[:notice] = _('%s administrator still needs to accept you as member.') % profile.name if profile.closed?
103   - redirect_to_previous_location
  100 + session[:join] = profile.identifier
  101 +
  102 + if user
  103 + redirect_to :controller => 'profile', :action => 'join'
104 104 else
105   - if user.memberships.include?(profile)
106   - session[:notice] = _('You are already a member of %s.') % profile.name
107   - redirect_to profile.url
108   - return
109   - end
110   - if request.xhr?
111   - render :layout => false
112   - else
113   - redirect_to profile.url
114   - end
  105 + redirect_to :controller => '/account', :action => 'login'
115 106 end
116 107 end
117 108  
... ...
app/models/user.rb
... ... @@ -50,6 +50,9 @@ class User < ActiveRecord::Base
50 50 self.person.preferred_domain && self.person.preferred_domain.name || self.environment.default_hostname(true)
51 51 end
52 52  
  53 + # virtual attribute used to stash which community to join on signup or login
  54 + attr_accessor :community_to_join
  55 +
53 56 class Mailer < ActionMailer::Base
54 57 def activation_email_notify(user)
55 58 user_email = "#{user.login}@#{user.email_domain}"
... ... @@ -72,7 +75,8 @@ class User &lt; ActiveRecord::Base
72 75 :activation_code => user.activation_code,
73 76 :environment => user.environment.name,
74 77 :url => user.environment.top_url,
75   - :redirection => (true if user.return_to)
  78 + :redirection => (true if user.return_to),
  79 + :join => (user.community_to_join if user.community_to_join)
76 80 end
77 81  
78 82 def signup_welcome_email(user)
... ...
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, :redirection => @redirection) }) %>
  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 => @redirection, :join => @join) }) %>
4 4  
5 5 <%= _("Greetings,") %>
6 6  
... ...
features/signup.feature
... ... @@ -275,7 +275,7 @@ Feature: signup
275 275 And I fill in "Username" with "josesilva"
276 276 And I fill in "Password" with "secret"
277 277 And I press "Log in"
278   - Then "Joao Silva" should be a member of "Free Software"
  278 + Then "José da Silva" should be a member of "Free Software"
279 279  
280 280 @selenium
281 281 Scenario: join community on direct signup
... ... @@ -297,4 +297,4 @@ Feature: signup
297 297 | Full name | José da Silva |
298 298 And wait for the captcha signup time
299 299 And I press "Create my account"
300   - Then "Joao Silva" should be a member of "Free Software"
  300 + Then "José da Silva" should be a member of "Free Software"
... ...