You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

email_addresses_controller.rb 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class EmailAddressesController < ApplicationController
  19. self.main_menu = false
  20. before_action :find_user, :require_admin_or_current_user
  21. before_action :find_email_address, :only => [:update, :destroy]
  22. require_sudo_mode :create, :update, :destroy
  23. def index
  24. @addresses = @user.email_addresses.order(:id).where(:is_default => false).to_a
  25. @address ||= EmailAddress.new
  26. end
  27. def create
  28. saved = false
  29. if @user.email_addresses.count <= Setting.max_additional_emails.to_i
  30. @address = EmailAddress.new(:user => @user, :is_default => false)
  31. @address.safe_attributes = params[:email_address]
  32. saved = @address.save
  33. end
  34. respond_to do |format|
  35. format.html do
  36. if saved
  37. redirect_to user_email_addresses_path(@user)
  38. else
  39. index
  40. render :action => 'index'
  41. end
  42. end
  43. format.js do
  44. @address = nil if saved
  45. index
  46. render :action => 'index'
  47. end
  48. end
  49. end
  50. def update
  51. if params[:notify].present?
  52. @address.notify = params[:notify].to_s
  53. end
  54. @address.save
  55. respond_to do |format|
  56. format.html do
  57. redirect_to user_email_addresses_path(@user)
  58. end
  59. format.js do
  60. @address = nil
  61. index
  62. render :action => 'index'
  63. end
  64. end
  65. end
  66. def destroy
  67. @address.destroy
  68. respond_to do |format|
  69. format.html do
  70. redirect_to user_email_addresses_path(@user)
  71. end
  72. format.js do
  73. @address = nil
  74. index
  75. render :action => 'index'
  76. end
  77. end
  78. end
  79. private
  80. def find_user
  81. @user = User.find(params[:user_id])
  82. end
  83. def find_email_address
  84. @address = @user.email_addresses.where(:is_default => false).find(params[:id])
  85. rescue ActiveRecord::RecordNotFound
  86. render_404
  87. end
  88. def require_admin_or_current_user
  89. unless @user == User.current
  90. require_admin
  91. end
  92. end
  93. end