about summary refs log tree commit diff
path: root/app/controllers/admin/change_emails_controller.rb
blob: a689d3a5301764695b879cd04ec5b3b649540c1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# frozen_string_literal: true

module Admin
  class ChangeEmailsController < BaseController
    before_action :set_account
    before_action :require_local_account!

    def show
      authorize @user, :change_email?
    end

    def update
      authorize @user, :change_email?

      new_email = resource_params.fetch(:unconfirmed_email)

      if new_email != @user.email
        @user.update!(
          unconfirmed_email: new_email,
          # Regenerate the confirmation token:
          confirmation_token: nil
        )

        log_action :change_email, @user

        @user.send_confirmation_instructions
      end

      redirect_to admin_account_path(@account.id), notice: I18n.t('admin.accounts.change_email.changed_msg')
    end

    private

    def set_account
      @account = Account.find(params[:account_id])
      @user = @account.user
    end

    def require_local_account!
      redirect_to admin_account_path(@account.id) unless @account.local? && @account.user.present?
    end

    def resource_params
      params.require(:user).permit(
        :unconfirmed_email
      )
    end
  end
end