diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 05ee86c..c88c8c4 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -66,7 +66,9 @@ plan = @role.plan @role.destroy flash[:notice] = _('Access removed') - UserMailer.project_access_removed_notification(user, plan, current_user).deliver_now + deliver_if(recipients: user, key: 'users.added_as_coowner') do |r| + UserMailer.plan_access_removed(user, plan, current_user).deliver_now + end redirect_to controller: 'plans', action: 'share', id: @role.plan.id end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9b1e2f1..51d9e30 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,7 @@ class UsersController < ApplicationController helper PaginableHelper + helper PermsHelper + include ConditionalUserMailer after_action :verify_authorized respond_to :html @@ -50,6 +52,9 @@ end if @user.save! + deliver_if(recipients: @user, key: 'users.admin_privileges') do |r| + UserMailer.admin_privileges(r).deliver_now + end redirect_to({controller: 'users', action: 'admin_index'}, {notice: success_message(_('permissions'), _('saved'))}) # helpers.success key does not exist, replaced with a generic string else flash[:alert] = failed_update_error(@user, _('user')) diff --git a/app/helpers/mailer_helper.rb b/app/helpers/mailer_helper.rb index c924c66..a7ee2df 100644 --- a/app/helpers/mailer_helper.rb +++ b/app/helpers/mailer_helper.rb @@ -1,5 +1,5 @@ module MailerHelper - + include PermsHelper def feedback_confirmation_default_subject _('%{application_name}: Your plan has been submitted for feedback') end @@ -17,4 +17,15 @@ organisation_email: org.contact_email} end + # Returns an unordered HTML list with the permissions associated to the user passed + def privileges_list(user) + if user.respond_to?(:perms) && user.perms.respond_to?(:each) + names = name_and_text + r= "" + end + end end \ No newline at end of file diff --git a/app/helpers/perms_helper.rb b/app/helpers/perms_helper.rb new file mode 100644 index 0000000..a8a6692 --- /dev/null +++ b/app/helpers/perms_helper.rb @@ -0,0 +1,15 @@ +module PermsHelper + # Returns a hash whose keys are the names associated to Perms and values are the text to be displayed to the end user + def name_and_text + { + :add_organisations => _('Add organisations'), + :change_org_affiliation => _('Change affiliation'), + :grant_permissions => _('Grant permissions'), + :modify_templates => _('Modify templates'), + :modify_guidance => _('Modify guidance'), + :use_api => _('API rights'), + :change_org_details => _('Change organisation details'), + :grant_api_to_orgs => _('Grant API to organisations') + } + end +end \ No newline at end of file diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 35492c1..b54bf09 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,6 +1,6 @@ class UserMailer < ActionMailer::Base include MailerHelper - + helper MailerHelper default from: Rails.configuration.branding[:organisation][:email] def welcome_notification(user) @@ -29,8 +29,7 @@ end end - # TODO evaluate if it is needed since https://docs.google.com/document/d/1Zt3QfPZ2q6yMOCVFOevXviwRX-XbZeGSxAAvbRa9w-M/edit does not mention it - def project_access_removed_notification(user, plan, current_user) + def plan_access_removed(user, plan, current_user) @user = user @plan = plan @current_user = current_user @@ -117,4 +116,12 @@ end end end + + def admin_privileges(user) + @user = user + FastGettext.with_locale FastGettext.default_locale do + mail(to: user.email, subject: + _('Administrator privileges granted in %{tool_name}') %{ :tool_name => Rails.configuration.branding[:application][:name] }) + end + end end diff --git a/app/models/perm.rb b/app/models/perm.rb index 58aad3e..fcc429f 100644 --- a/app/models/perm.rb +++ b/app/models/perm.rb @@ -9,17 +9,6 @@ #attr_accessible :name, :as => [:default, :admin] validates :name, presence: {message: _("can't be blank")}, uniqueness: {message: _("must be unique")} - - ## - # Constant perms - #ADD_ORGS = Perm.where(name: 'add_organisations').first.freeze - #CHANGE_AFFILIATION = Perm.where(name: 'change_org_affiliation').first.freeze - #GRANT_PERMISSIONS = Perm.where(name: 'grant_permissions').first.freeze - #MODIFY_TEMPLATES = Perm.where(name: 'modify_templates').first.freeze - #MODIFY_GUIDANCE = Perm.where(name: 'modify_guidance').first.freeze - #USE_API = Perm.where(name: 'use_api').first.freeze - #CHANGE_ORG_DETAILS = Perm.where(name: 'change_org_details').first.freeze - #GRANT_API = Perm.where(name: 'grant_api_to_orgs').first.freeze scope :add_orgs, -> {Perm.find_by(name: 'add_organisations')} scope :change_affiliation, -> {Perm.find_by(name: 'change_org_affiliation')} diff --git a/app/models/user.rb b/app/models/user.rb index e46f805..3089e93 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,5 @@ class User < ActiveRecord::Base - + include ConditionalUserMailer ## # Devise # Include default devise modules. Others available are: @@ -239,8 +239,9 @@ break random_token unless User.exists?(api_token: random_token) end self.save! - # send an email to the user to notify them of their new api token - #UserMailer.api_token_granted_notification(self) + deliver_if(recipients: self, key: 'users.admin_privileges') do |r| + UserMailer.api_token_granted_notification(r).deliver_now + end end end diff --git a/app/views/user_mailer/admin_privileges.html.erb b/app/views/user_mailer/admin_privileges.html.erb new file mode 100644 index 0000000..c593275 --- /dev/null +++ b/app/views/user_mailer/admin_privileges.html.erb @@ -0,0 +1,25 @@ +<% + tool_name = Rails.configuration.branding[:application][:name] + user_name = @user.name + help_url = Rails.configuration.branding[:organisation][:url] + ul_list = raw(privileges_list(@user)) +%> +

+ <%= _('Hello %{user_name}') %{ :user_name => user_name } %> +

+<% if @user.perms.size > 0 %> +

+ <%= _('You have been granted administrator privileges in %{tool_name}:') %{ :tool_name => tool_name } %> +

+

+ <%= ul_list %> +

+<% else %> +

+ <%= _('You have been revoked administrator privileges in %{tool_name}.') %{ :tool_name => tool_name } %> +

+<% end %> +

+ <%= raw _('More information about administering the %{tool_name} for users at your institution is available at %{help_url}.') %{ :tool_name => tool_name, :help_url => link_to('help page', help_url)} %> +

+<%= render partial: 'email_signature' %> \ No newline at end of file diff --git a/app/views/user_mailer/plan_access_removed.html.erb b/app/views/user_mailer/plan_access_removed.html.erb new file mode 100644 index 0000000..dbb963c --- /dev/null +++ b/app/views/user_mailer/plan_access_removed.html.erb @@ -0,0 +1,5 @@ +<% FastGettext.with_locale FastGettext.default_locale do %> +

<%= _('Hello ') %><%= @user.email %>,

+

<%= _('Your access to ') %>"<%= @plan.title %>"<%= _(' has been removed by ') %><%= "#{@current_user.name(false)}"%>.

+<%= render partial: 'email_signature' %> +<% end %> diff --git a/app/views/user_mailer/project_access_removed_notification.html.erb b/app/views/user_mailer/project_access_removed_notification.html.erb deleted file mode 100644 index dbb963c..0000000 --- a/app/views/user_mailer/project_access_removed_notification.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% FastGettext.with_locale FastGettext.default_locale do %> -

<%= _('Hello ') %><%= @user.email %>,

-

<%= _('Your access to ') %>"<%= @plan.title %>"<%= _(' has been removed by ') %><%= "#{@current_user.name(false)}"%>.

-<%= render partial: 'email_signature' %> -<% end %> diff --git a/app/views/users/admin_grant_permissions.html.erb b/app/views/users/admin_grant_permissions.html.erb index ca462c3..bb4275d 100644 --- a/app/views/users/admin_grant_permissions.html.erb +++ b/app/views/users/admin_grant_permissions.html.erb @@ -1,3 +1,4 @@ +<% namesHash = name_and_text %>

<%= _('Edit User Privileges') %>

@@ -16,23 +17,23 @@ <% @perms.each do |perm| %> <% case perm.name when 'grant_permissions' %> - <%= _('Grant permissions') %> + <%= namesHash[perm.name.to_sym] %> <% when 'modify_templates' %> - <%= _('Modify templates') %> + <%= namesHash[perm.name.to_sym] %> <% when 'modify_guidance' %> - <%= _('Modify guidance') %> + <%= namesHash[perm.name.to_sym] %> <% when 'use_api' %> - <%= _('API rights') %> + <%= namesHash[perm.name.to_sym] %> <% when 'change_org_details' %> - <%= _('Change organisation details') %> + <%= namesHash[perm.name.to_sym] %> <% end %> <% end %> diff --git a/test/helpers/mailer_helper_test.rb b/test/helpers/mailer_helper_test.rb new file mode 100644 index 0000000..617a7bc --- /dev/null +++ b/test/helpers/mailer_helper_test.rb @@ -0,0 +1,22 @@ +require 'test_helper' + +class MailerHelperTest < ActionView::TestCase + setup do + @user = User.find_by(email: "super_admin@example.com") + @user.perms.destroy_all + end + test "returns nil when objects does not have a method perms" do + assert_nil privileges_list({}) + end + test "returns an empty ul list for an user without permissions" do + assert_equal("", privileges_list(@user)) + end + test "return an ul list with the permission for an user" do + names = name_and_text # PermsHelper method included within MailerHelper + @user.perms << Perm.first + @user.perms << Perm.second + @user.save + expected="" + assert_equal(expected, privileges_list(@user)) + end +end \ No newline at end of file diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb index 8755cf8..5999ad6 100644 --- a/test/mailers/previews/user_mailer_preview.rb +++ b/test/mailers/previews/user_mailer_preview.rb @@ -1,26 +1,32 @@ class UserMailerPreview < ActionMailer::Preview + def initialize + @user = User.find_by(email: 'super_admin@example.com') + end def welcome_notification - UserMailer.welcome_notification(User.find_by(email: 'super_admin@example.com')) + UserMailer.welcome_notification(@user) end def sharing_notification - user = User.find_by(email: 'super_admin@example.com') - UserMailer.sharing_notification(Role.find_by(user_id: user.id), user) + UserMailer.sharing_notification(Role.find_by(user_id: @user.id), user) end def permissions_change_notification - user = User.find_by(email: 'super_admin@example.com') - UserMailer.permissions_change_notification(Role.find_by(user_id: user.id), user) + UserMailer.permissions_change_notification(Role.find_by(user_id: @user.id), user) + end + # relative_url at /rails/mailers/user_mailer/plan_access_removed + def plan_access_removed + UserMailer.plan_access_removed(@user, @user.plans.first, @user) end def api_token_granted_notification - user = User.find_by(email: 'super_admin@example.com') - UserMailer.api_token_granted_notification(user) + UserMailer.api_token_granted_notification(@user) end def plan_visibility - user = User.find_by(email: 'super_admin@example.com') - UserMailer.plan_visibility(user, user.plans.first) + UserMailer.plan_visibility(@user, @user.plans.first) end def new_comment - commenter = User.find_by(email: 'super_admin@example.com') plan = Plan.joins(:roles).where(Role.creator_condition).first - UserMailer.new_comment(commenter, plan) + UserMailer.new_comment(@user, plan) + end + # relative_url at /rails/mailers/user_mailer/admin_privileges + def admin_privileges + UserMailer.admin_privileges(@user) end end \ No newline at end of file