diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 111f4de..8a3312f 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -45,7 +45,6 @@ class UsersController < AdminController redirect_to :action => :index, :q => params[:q], :filter => params[:filter] end - def destroy_user if request.post? person = environment.people.find_by id: params[:id] @@ -58,7 +57,6 @@ class UsersController < AdminController redirect_to :action => :index, :q => params[:q], :filter => params[:filter] end - def download respond_to do |format| format.html @@ -87,8 +85,11 @@ class UsersController < AdminController end def send_mail - @mailing = environment.mailings.build(params[:mailing]) if request.post? + @mailing = environment.mailings.build(params[:mailing]) + @mailing.recipients_roles = [] + @mailing.recipients_roles << "profile_admin" if params[:recipients][:profile_admins].include?("true") + @mailing.recipients_roles << "environment_administrator" if params[:recipients][:env_admins].include?("true") @mailing.locale = locale @mailing.person = user if @mailing.save diff --git a/app/mailers/environment_mailing.rb b/app/mailers/environment_mailing.rb index 3b16a76..a1c2721 100644 --- a/app/mailers/environment_mailing.rb +++ b/app/mailers/environment_mailing.rb @@ -1,11 +1,23 @@ class EnvironmentMailing < Mailing + settings_items :recipients_roles, :type => :array + attr_accessible :recipients_roles + def recipients(offset=0, limit=100) - source.people.order(:id).offset(offset).limit(limit) + recipients_by_role.order(:id).offset(offset).limit(limit) .joins("LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)") .where("m.person_id" => nil) end + def recipients_by_role + if recipients_roles.blank? + source.people + else + roles = Environment::Role.where("key in (?)", self.recipients_roles) + Person.by_role(roles).where(environment_id: self.source_id) + end + end + def each_recipient offset = 0 limit = 100 diff --git a/app/views/users/_index_buttons.html.erb b/app/views/users/_index_buttons.html.erb index 17e2893..7eeeaed 100644 --- a/app/views/users/_index_buttons.html.erb +++ b/app/views/users/_index_buttons.html.erb @@ -1,6 +1,6 @@ <% button_bar do %> <%= button :'text-plain', _('User list as [CSV]'), :action => 'download.csv' %> <%= button :'text-html', _('User list as [XML]'), :action => 'download.xml' %> - <%= button :send, _('Send e-mail to all users'), :action => 'send_mail' %> + <%= button :send, _('Send e-mail to users'), :action => 'send_mail' %> <%= button :back, _('Back'), :controller => 'admin_panel' %> <% end %> diff --git a/app/views/users/send_mail.html.erb b/app/views/users/send_mail.html.erb index 7a478b4..7c2d940 100644 --- a/app/views/users/send_mail.html.erb +++ b/app/views/users/send_mail.html.erb @@ -3,10 +3,19 @@ <%= error_messages_for :mailing %> <%= render :file => 'shared/tiny_mce' %> - <%= form_for :mailing do |f| %> +
+ <%= label_tag(_("Recipients: "), nil, { class: "formlabel" }) %> + <%= labelled_radio_button(_('All Users'), :send_to, "all", true, { id: "send_to_all" }) %>
+ <%= labelled_radio_button(_('Only Admins'), :send_to, "admins" , false, { id: "send_to_admins" }) %>
+
+ <%= labelled_check_box(_('Environment Admins'), 'recipients[env_admins]', true, false, { disabled: true, id: "env_admins" }) %> + <%= labelled_check_box(_('Profile Admins'), 'recipients[profile_admins]', true, false, { disabled: true, id: "profile_admins" }) %> +
+
<%= labelled_form_field(_('Subject:'), f.text_field(:subject)) %> <%= labelled_form_field(_('Body:'), f.text_area(:body, :class => 'mceEditor')) %> <%= submit_button(:send, _('Send')) %> <%= button :cancel, _('Cancel e-mail'), :controller => 'users' %> <% end %> +<%= javascript_include_tag "send_email.js" %> diff --git a/features/send_email_to_environment_members.feature b/features/send_email_to_environment_members.feature index 0acbcbc..79ad2d4 100644 --- a/features/send_email_to_environment_members.feature +++ b/features/send_email_to_environment_members.feature @@ -43,3 +43,29 @@ Feature: send emails to environment members users Then I should be on /admin/users/send_mail When I follow "Cancel e-mail" Then I should be on /admin/users + + Scenario: Should display recipients options + Given I am logged in as admin + And I go to /admin/users/send_mail + Then I should see "Recipients" + Then I should see "All Users" + Then I should see "Only Admins" + Then I should see "Environment Admins" + Then I should see "Profile Admins" + + Scenario: All users should be marked as default recipients + Given I am logged in as admin + And I go to /admin/users/send_mail + Then the "send_to_all" radio button should be checked + Then the "send_to_admins" radio button should not be checked + + @selenium + Scenario: Should disable checkboxes when recipients is set to All users + Given I am logged in as admin + And I go to /admin/users/send_mail + Then the field "#profile_admins" should be disabled + Then the field "#env_admins" should be disabled + When I choose "Only Admins" + Then the field "#profile_admins" should be enabled + Then the field "#env_admins" should be enabled + diff --git a/public/javascripts/send_email.js b/public/javascripts/send_email.js new file mode 100644 index 0000000..53c38d8 --- /dev/null +++ b/public/javascripts/send_email.js @@ -0,0 +1,22 @@ +var all_users_radio_btn = document.getElementById("send_to_all"); +var admins_radio_btn = document.getElementById("send_to_admins"); +var env_admins_checkbox = document.getElementById("env_admins"); +var profile_admins_checkbox = document.getElementById("profile_admins"); + +admins_radio_btn.onchange = function() { + change_checkbox_state("admins"); +}; + +all_users_radio_btn.onchange = function() { + change_checkbox_state("all_users"); +}; + +function change_checkbox_state (recipients) { + if(recipients == "all_users"){ + env_admins_checkbox.disabled = true; + profile_admins_checkbox.disabled = true; + }else { + env_admins_checkbox.disabled = false; + profile_admins_checkbox.disabled = false; + } +}; diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index 1a180f5..b61bf31 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -141,4 +141,36 @@ class UsersControllerTest < ActionController::TestCase assert_redirected_to :action => 'index' end + should 'redirect to index after send email with success' do + post :send_mail, mailing: { subject: "Subject", body: "Body" }, recipients: { profile_admins: "false", env_admins: "false" } + assert_redirected_to :action => 'index' + assert_match /The e-mails are being sent/, session[:notice] + end + + should 'mailing recipients_roles should be empty when none is set' do + post :send_mail, mailing: { subject: "Subject", body: "Body" }, recipients: { profile_admins: "false", env_admins: "false" } + mailing = EnvironmentMailing.last + assert_equal mailing.recipients_roles, [] + end + + should 'mailing recipients_roles should be set correctly' do + post :send_mail, mailing: { subject: "Subject", body: "Body" }, recipients: { profile_admins: "true", env_admins: "true" } + mailing = EnvironmentMailing.last + assert_equal mailing.recipients_roles, ["profile_admin", "environment_administrator"] + end + + should 'send mail to admins recipients' do + admin_user = create_user('new_admin').person + admin_user_2 = create_user('new_admin_2').person + + environment.add_admin admin_user + environment.add_admin admin_user_2 + + assert_equal 2, environment.admins.count + assert_difference "MailingSent.count", 2 do + post :send_mail, mailing: { subject: "UnB", body: "Hail UnB" }, recipients: { profile_admins: "false", env_admins: "true" } + process_delayed_job_queue + end + end + end diff --git a/test/unit/environment_mailing_test.rb b/test/unit/environment_mailing_test.rb index 7537b94..be74da7 100644 --- a/test/unit/environment_mailing_test.rb +++ b/test/unit/environment_mailing_test.rb @@ -82,6 +82,49 @@ class EnvironmentMailingTest < ActiveSupport::TestCase assert_equal [person_1], mailing.recipients(0, 1) end + should 'return all environment admins when recipients_roles is set to environment_administrator' do + environment.add_admin person_1 + + mailing = create_mailing(environment, :locale => 'pt', :person => person_1) + mailing.recipients_roles = ["environment_administrator"] + mailing.save + + assert_equivalent(environment.admins, mailing.recipients) + end + + should 'return all people with role profile_admin when recipients_roles is set to profile_admin' do + environment.add_admin person_1 + + mailing = create_mailing(environment, :locale => 'pt', :person => person_1) + mailing.recipients_roles = ["profile_admin"] + mailing.save + role = Role.find_by(key: 'profile_admin', environment_id: environment) + profile_admins = Person.by_role(role).where(environment_id: environment) + + assert_equivalent(profile_admins, mailing.recipients) + end + + should 'return all people when recipients_roles is not set' do + environment.add_admin person_1 + + mailing = create_mailing(environment, :locale => 'pt', :person => person_1) + mailing.save + + assert_equivalent(environment.people, mailing.recipients) + end + + should 'return profile_admins and environment admins when both roles are set as recipients' do + environment.add_admin person_1 + + mailing = create_mailing(environment, :locale => 'pt', :person => person_1) + mailing.recipients_roles = ["profile_admin", "environment_administrator"] + mailing.save + role = Role.find_by(key: 'profile_admin', environment_id: environment) + profile_admins = Person.by_role(role).where(environment_id: environment) + + assert_equivalent(profile_admins+environment.admins, mailing.recipients) + end + should 'return true if already sent mailing to a recipient' do mailing = create_mailing(environment, :person => person_1) process_delayed_job_queue -- libgit2 0.21.2