Commit a6e6ace7ff187c3f5513feb85ec58ac74084087c

Authored by Antonio Terceiro
2 parents e0f459cd ee4b142a

Merge branch 'AI3036-moderate-users-registration' into 'master'

Ai3036 Moderate Users Registration

http://noosfero.org/Development/ActionItem3036

See merge request !188
app/controllers/public/account_controller.rb
... ... @@ -15,11 +15,23 @@ class AccountController < ApplicationController
15 15  
16 16 def activate
17 17 @user = User.find_by_activation_code(params[:activation_code]) if params[:activation_code]
18   - if @user and @user.activate
19   - @message = _("Your account has been activated, now you can log in!")
20   - check_redirection
21   - session[:join] = params[:join] unless params[:join].blank?
22   - render :action => 'login', :userlogin => @user.login
  18 + if @user
  19 + unless @user.environment.enabled?('admin_must_approve_new_users')
  20 + if @user.activate
  21 + @message = _("Your account has been activated, now you can log in!")
  22 + check_redirection
  23 + session[:join] = params[:join] unless params[:join].blank?
  24 + render :action => 'login', :userlogin => @user.login
  25 + end
  26 + else
  27 + if @user.create_moderate_task
  28 + session[:notice] = _('Thanks for registering. The administrators were notified.')
  29 + @register_pending = true
  30 + @user.activation_code = nil
  31 + @user.save!
  32 + redirect_to :controller => :home
  33 + end
  34 + end
23 35 else
24 36 session[:notice] = _("It looks like you're trying to activate an account. Perhaps have already activated this account?")
25 37 redirect_to :controller => :home
... ... @@ -108,6 +120,7 @@ class AccountController < ApplicationController
108 120 check_join_in_community(@user)
109 121 go_to_signup_initial_page
110 122 else
  123 + session[:notice] = _('Thanks for registering!')
111 124 @register_pending = true
112 125 end
113 126 end
... ...
app/models/environment.rb
... ... @@ -124,6 +124,7 @@ class Environment < ActiveRecord::Base
124 124 'organizations_are_moderated_by_default' => _("Organizations have moderated publication by default"),
125 125 'enable_organization_url_change' => _("Allow organizations to change their URL"),
126 126 'admin_must_approve_new_communities' => _("Admin must approve creation of communities"),
  127 + 'admin_must_approve_new_users' => _("Admin must approve registration of new users"),
127 128 'show_balloon_with_profile_links_when_clicked' => _('Show a balloon with profile links when a profile image is clicked'),
128 129 'xmpp_chat' => _('XMPP/Jabber based chat'),
129 130 'show_zoom_button_on_article_images' => _('Show a zoom link on all article images'),
... ...
app/models/moderate_user_registration.rb 0 → 100644
... ... @@ -0,0 +1,59 @@
  1 +class ModerateUserRegistration < Task
  2 +
  3 + settings_items :user_id, :type => String
  4 + settings_items :name, :type => String
  5 + settings_items :author_name, :type => String
  6 + settings_items :email, :type => String
  7 +
  8 + after_create :schedule_spam_checking
  9 +
  10 + alias :environment :target
  11 + alias :environment= :target=
  12 +
  13 + def schedule_spam_checking
  14 + self.delay.check_for_spam
  15 + end
  16 +
  17 + include Noosfero::Plugin::HotSpot
  18 +
  19 + def sender
  20 + "#{name} (#{email})"
  21 + end
  22 +
  23 + def perform
  24 + user=environment.users.find_by_id(user_id)
  25 + user.activate
  26 + end
  27 +
  28 + def title
  29 + _("New user")
  30 + end
  31 +
  32 + def subject
  33 + name
  34 + end
  35 +
  36 + def information
  37 + { :message => _('%{sender} wants to register.'),
  38 + :variables => {:sender => sender} }
  39 + end
  40 +
  41 + def icon
  42 + result = {:type => :defined_image, :src => '/images/icons-app/person-minor.png', :name => name}
  43 + end
  44 +
  45 + def target_notification_description
  46 + _('%{sender} tried to register.') %
  47 + {:sender => sender}
  48 + end
  49 +
  50 + def target_notification_message
  51 + target_notification_description + "\n\n" +
  52 + _('You need to login on %{system} in order to approve or reject this user.') % { :environment => self.environment }
  53 + end
  54 +
  55 + def target_notification_message
  56 + _("User \"%{user}\" just requested to register. You have to approve or reject it through the \"Pending Validations\" section in your control panel.\n") % { :user => self.name }
  57 + end
  58 +
  59 +end
0 60 \ No newline at end of file
... ...
app/models/user.rb
... ... @@ -47,8 +47,12 @@ class User &lt; ActiveRecord::Base
47 47  
48 48 user.person = p
49 49 end
50   - if user.environment.enabled?('skip_new_user_email_confirmation')
51   - user.activate
  50 + if user.environment.enabled?('skip_new_user_email_confirmation')
  51 + if user.environment.enabled?('admin_must_approve_new_users')
  52 + create_moderate_task
  53 + else
  54 + user.activate
  55 + end
52 56 end
53 57 end
54 58 after_create :deliver_activation_code
... ... @@ -137,6 +141,15 @@ class User &lt; ActiveRecord::Base
137 141 end
138 142 end
139 143  
  144 + def create_moderate_task
  145 + @task = ModerateUserRegistration.new
  146 + @task.user_id = self.id
  147 + @task.name = self.name
  148 + @task.email = self.email
  149 + @task.target = self.environment
  150 + @task.save
  151 + end
  152 +
140 153 def activated?
141 154 self.activation_code.nil? && !self.activated_at.nil?
142 155 end
... ...
app/views/account/signup.html.erb
... ... @@ -2,18 +2,36 @@
2 2 <div id='thanks-for-signing'>
3 3 <% if environment.has_custom_welcome_screen? %>
4 4 <%= environment.settings[:signup_welcome_screen_body].html_safe %>
5   - <% else %>
6   - <h1><%= _("Welcome to %s!") % environment.name %></h1>
7   - <h3><%= _("Thanks for signing up, we're thrilled to have you on our social network!") %></h3>
8   - <p><%= _("Firstly, some tips for getting started:") %></p>
9   - <h4><%= _("Confirm your account!") %></h4>
  5 + <% elsif environment.enabled?('admin_must_approve_new_users')%>
  6 + <h1><%= _("Welcome to %s!") % environment.name %></h1>
  7 + <h3><%= _("Thanks for signing up, we're thrilled to have you on our social network!") %></h3>
  8 + <p><%= _("Firstly, some tips for getting started:") %></p>
  9 + <% unless environment.enabled?('skip_new_user_email_confirmation') %>
  10 + <h4><%= _("Confirm your account and wait for admin approvement!") %></h4>
10 11 <p><%= _("You should receive a welcome email from us shortly. Please take a second to follow the link within to confirm your account.") %></p>
11   - <p><%= _("You won't appear as %s until your account is confirmed.") % link_to(_('user'), {:controller => :search, :action => :people, :filter => 'more_recent'}, :target => '_blank') %></p>
12   - <h4><%= _("What to do next?") %></h4>
13   - <p><%= _("%s. Upload an avatar and let your friends find you easily :)") % link_to(_('Customize your profile'), {:controller => 'doc', :section => 'user', :topic => 'editing-person-info'}, :target => '_blank') %></p>
14   - <p><%= _("Learn the guidelines. Read the %s for more details on how to use this social network!") % link_to(_('Documentation'), {:controller => 'doc'}, :target => '_blank') %></p>
15   - <p><%= _("%s your Gmail, Yahoo and Hotmail contacts!") % link_to(_('Invite and find'), {:controller => 'doc', :section => 'user', :topic => 'invite-contacts'}, :target => '_blank') %></p>
16   - <p><%= _("Start exploring and have fun!") %></p>
  12 + <p><%= _("You won't appear as %s until your account is confirmed and approved.") % link_to(_('user'), {:controller => :search, :action => :people, :filter => 'more_recent'}, :target => '_blank') %></p>
  13 + <% else %>
  14 + <h4><%= _("Wait for admin approvement!") %></h4>
  15 + <p><%= _("The administrators will evaluate your signup request for approvement.") %></p>
  16 + <p><%= _("You won't appear as %s until your account is approved.") % link_to(_('user'), {:controller => :search, :action => :people, :filter => 'more_recent'}, :target => '_blank') %></p>
  17 + <% end %>
  18 + <h4><%= _("What to do next?") %></h4>
  19 + <p><%= _("%s. Upload an avatar and let your friends find you easily :)") % link_to(_('Customize your profile'), {:controller => 'doc', :section => 'user', :topic => 'editing-person-info'}, :target => '_blank') %></p>
  20 + <p><%= _("Learn the guidelines. Read the %s for more details on how to use this social network!") % link_to(_('Documentation'), {:controller => 'doc'}, :target => '_blank') %></p>
  21 + <p><%= _("%s your Gmail, Yahoo and Hotmail contacts!") % link_to(_('Invite and find'), {:controller => 'doc', :section => 'user', :topic => 'invite-contacts'}, :target => '_blank') %></p>
  22 + <p><%= _("Start exploring and have fun!") %></p>
  23 + <% else %>
  24 + <h1><%= _("Welcome to %s!") % environment.name %></h1>
  25 + <h3><%= _("Thanks for signing up, we're thrilled to have you on our social network!") %></h3>
  26 + <p><%= _("Firstly, some tips for getting started:") %></p>
  27 + <h4><%= _("Confirm your account!") %></h4>
  28 + <p><%= _("You should receive a welcome email from us shortly. Please take a second to follow the link within to confirm your account.") %></p>
  29 + <p><%= _("You won't appear as %s until your account is confirmed.") % link_to(_('user'), {:controller => :search, :action => :people, :filter => 'more_recent'}, :target => '_blank') %></p>
  30 + <h4><%= _("What to do next?") %></h4>
  31 + <p><%= _("%s. Upload an avatar and let your friends find you easily :)") % link_to(_('Customize your profile'), {:controller => 'doc', :section => 'user', :topic => 'editing-person-info'}, :target => '_blank') %></p>
  32 + <p><%= _("Learn the guidelines. Read the %s for more details on how to use this social network!") % link_to(_('Documentation'), {:controller => 'doc'}, :target => '_blank') %></p>
  33 + <p><%= _("%s your Gmail, Yahoo and Hotmail contacts!") % link_to(_('Invite and find'), {:controller => 'doc', :section => 'user', :topic => 'invite-contacts'}, :target => '_blank') %></p>
  34 + <p><%= _("Start exploring and have fun!") %></p>
17 35 <% end %>
18 36 </div>
19 37 <% else %>
... ...
features/signup.feature
... ... @@ -298,3 +298,55 @@ Feature: signup
298 298 And wait for the captcha signup time
299 299 And I press "Create my account"
300 300 Then "José da Silva" should be a member of "Free Software"
  301 +
  302 + @selenium
  303 + Scenario: user registration is moderated by admin
  304 + Given feature "admin_must_approve_new_users" is enabled on environment
  305 + And feature "skip_new_user_email_confirmation" is disabled on environment
  306 + And I go to /account/signup
  307 + And I fill in "Username" with "teste"
  308 + And I fill in "Password" with "123456"
  309 + And I fill in "Password confirmation" with "123456"
  310 + And I fill in "e-Mail" with "teste@teste.com"
  311 + And I fill in "Full name" with "Teste da Silva"
  312 + And wait for the captcha signup time
  313 + And I press "Create my account"
  314 + And I go to teste's confirmation URL
  315 + And I am logged in as admin
  316 + And I follow "Control panel"
  317 + And I follow "Tasks"
  318 + And I choose "Accept"
  319 + And I press "Apply!"
  320 + And I follow "Logout"
  321 + And Teste da Silva's account is activated
  322 + And I follow "Login"
  323 + And I fill in "Username / Email" with "teste"
  324 + And I fill in "Password" with "123456"
  325 + And I press "Log in"
  326 + Then I should see "teste"
  327 +
  328 +
  329 + @selenium
  330 + Scenario: user registration is not accepted by the admin
  331 + Given feature "admin_must_approve_new_users" is enabled on environment
  332 + And feature "skip_new_user_email_confirmation" is disabled on environment
  333 + And I go to /account/signup
  334 + And I fill in "Username" with "teste"
  335 + And I fill in "Password" with "123456"
  336 + And I fill in "Password confirmation" with "123456"
  337 + And I fill in "e-Mail" with "teste@teste.com"
  338 + And I fill in "Full name" with "Teste da Silva"
  339 + And wait for the captcha signup time
  340 + And I press "Create my account"
  341 + And I go to teste's confirmation URL
  342 + And I am logged in as admin
  343 + And I follow "Control panel"
  344 + And I follow "Tasks"
  345 + And I choose "Reject"
  346 + And I press "Apply!"
  347 + And I follow "Logout"
  348 + And I follow "Login"
  349 + And I fill in "Username / Email" with "teste"
  350 + And I fill in "Password" with "123456"
  351 + And I press "Log in"
  352 + Then I should not see "teste"
301 353 \ No newline at end of file
... ...
test/unit/moderate_user_registration_test.rb 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +# encoding: UTF-8
  2 +require File.dirname(__FILE__) + '/../test_helper'
  3 +
  4 +class ModerateUserRegistrationTest < ActiveSupport::TestCase
  5 + fixtures :users, :environments
  6 +
  7 + def test_should_on_perform_activate_user
  8 + user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test')
  9 + user.save!
  10 + environment = Environment.default
  11 + t= ModerateUserRegistration.new
  12 + t.user_id = user.id
  13 + t.name = user.name
  14 + t.author_name = user.name
  15 + t.email = user.email
  16 + t.target= environment
  17 + t.save!
  18 + assert !user.activated?
  19 + t.perform
  20 + assert environment.users.find_by_id(user.id).activated?
  21 + end
  22 +end
... ...