imap_authentication_check.rb 1.37 KB
Newer Older
1
2
3
4
5
6
module SystemCheck
  module IncomingEmail
    class ImapAuthenticationCheck < SystemCheck::BaseCheck
      set_name 'IMAP server credentials are correct?'

      def check?
7
8
9
        if config
          try_connect_imap
        else
10
          @error = "#{mail_room_config_path} does not have mailboxes set up"
11
          false
12
13
14
15
16
        end
      end

      def show_error
        try_fixing_it(
17
          "An error occurred: #{@error.class}: #{@error.message}",
18
19
20
21
22
23
24
25
26
27
          'Check that the information in config/gitlab.yml is correct'
        )
        for_more_information(
          'doc/administration/reply_by_email.md'
        )
        fix_and_rerun
      end

      private

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
      def try_connect_imap
        imap = Net::IMAP.new(config[:host], port: config[:port], ssl: config[:ssl])
        imap.starttls if config[:start_tls]
        imap.login(config[:email], config[:password])
        true
      rescue => error
        @error = error
        false
      end

      def config
        @config ||= load_config
      end

      def mail_room_config_path
        @mail_room_config_path ||=
          Rails.root.join('config', 'mail_room.yml').to_s
      end
46

47
48
49
      def load_config
        erb = ERB.new(File.read(mail_room_config_path))
        erb.filename = mail_room_config_path
50
51
        config_file = YAML.load(erb.result)

52
        config_file.dig(:mailboxes, 0)
53
54
55
56
      end
    end
  end
end