email_addresses_controller.rb 2.7 KB
Newer Older
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2016  Jean-Philippe Lang
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# 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
19 20
  before_action :find_user, :require_admin_or_current_user
  before_action :find_email_address, :only => [:update, :destroy]
21
  require_sudo_mode :create, :update, :destroy
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

  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)
      attrs = params[:email_address]
      if attrs.is_a?(Hash)
        @address.address = attrs[:address].to_s
      end
      saved = @address.save
    end

    respond_to do |format|
      format.html {
        if saved
          redirect_to user_email_addresses_path(@user)
        else
          index
          render :action => 'index'
        end
      }
      format.js {
        @address = nil if saved
        index
        render :action => 'index'
      }
    end
  end

  def update
    if params[:notify].present?
      @address.notify = params[:notify].to_s
    end
    @address.save

    respond_to do |format|
      format.html {
        redirect_to user_email_addresses_path(@user)
      }
      format.js {
        @address = nil
        index
        render :action => 'index'
      }
    end
  end

  def destroy
    @address.destroy

    respond_to do |format|
      format.html {
        redirect_to user_email_addresses_path(@user)
      }
      format.js {
        @address = nil
        index
        render :action => 'index'
      }
    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