| 
									
										
										
										
											2019-03-16 09:37:35 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2023-01-01 06:19:35 +00:00
										 |  |  | # Copyright (C) 2006-2023  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EmailAddressesController < ApplicationController | 
					
						
							| 
									
										
										
										
											2016-11-19 10:30:02 +00:00
										 |  |  |   self.main_menu = false | 
					
						
							| 
									
										
										
										
											2016-07-14 07:27:31 +00:00
										 |  |  |   before_action :find_user, :require_admin_or_current_user | 
					
						
							|  |  |  |   before_action :find_email_address, :only => [:update, :destroy] | 
					
						
							| 
									
										
										
										
											2015-06-19 18:41:10 +00:00
										 |  |  |   require_sudo_mode :create, :update, :destroy | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def index | 
					
						
							|  |  |  |     @addresses = @user.email_addresses.order(:id).where(:is_default => false).to_a | 
					
						
							|  |  |  |     @address ||= EmailAddress.new | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def create | 
					
						
							|  |  |  |     saved = false | 
					
						
							|  |  |  |     if @user.email_addresses.count <= Setting.max_additional_emails.to_i | 
					
						
							|  |  |  |       @address = EmailAddress.new(:user => @user, :is_default => false) | 
					
						
							| 
									
										
										
										
											2016-07-17 08:27:27 +00:00
										 |  |  |       @address.safe_attributes = params[:email_address] | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |       saved = @address.save | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       format.html do | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |         if saved | 
					
						
							|  |  |  |           redirect_to user_email_addresses_path(@user) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           index | 
					
						
							|  |  |  |           render :action => 'index' | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       format.js do | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |         @address = nil if saved | 
					
						
							|  |  |  |         index | 
					
						
							|  |  |  |         render :action => 'index' | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def update | 
					
						
							|  |  |  |     if params[:notify].present? | 
					
						
							|  |  |  |       @address.notify = params[:notify].to_s | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     @address.save | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       format.html do | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |         redirect_to user_email_addresses_path(@user) | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       format.js do | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |         @address = nil | 
					
						
							|  |  |  |         index | 
					
						
							|  |  |  |         render :action => 'index' | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def destroy | 
					
						
							|  |  |  |     @address.destroy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       format.html do | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |         redirect_to user_email_addresses_path(@user) | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       format.js do | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |         @address = nil | 
					
						
							|  |  |  |         index | 
					
						
							|  |  |  |         render :action => 'index' | 
					
						
							| 
									
										
										
										
											2020-11-09 13:24:52 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-01-17 14:14:12 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def find_user | 
					
						
							|  |  |  |     @user = User.find(params[:user_id]) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def find_email_address | 
					
						
							|  |  |  |     @address = @user.email_addresses.where(:is_default => false).find(params[:id]) | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def require_admin_or_current_user | 
					
						
							|  |  |  |     unless @user == User.current | 
					
						
							|  |  |  |       require_admin | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |