Project

General

Profile

RE: User auth against an IMAP server - is it wanted? ยป auth_source_imap.rb

Sunding Wei, 2013-02-21 09:33

 
1
##
2
# Redmine IMAP Authentication Module
3
#
4
# All rights avoided, you are free to distribute or modify
5
# the source code under the terms you reserve the author information.
6
#
7
# Author:
8
#     Dingding Technology, Inc.
9
#     Sunding Wei <swei(at)dingding.me>
10
#
11
# File: redmine/app/models/auth_source_imap.rb
12
#
13
require "net/imap" 
14
require 'timeout'
15

    
16
#
17
# HOWTO
18
#
19
# 1. Execute the SQL
20
#    INSERT INTO auth_sources (type, name) values ("AuthSourceIMAP", "IMAP")
21
# 2. Change as you like
22
# 3. Redmine: set the user authentication mode to IMAP
23
# 4. Restart your web server
24
#
25

    
26
class AuthSourceIMAP < AuthSource
27
    def authenticate(login, password)
28
        # Define your IMAP server
29
        self.host = "imap.qq.com" 
30
        self.port = 143
31
        retVal = {
32
            :firstname => self.attr_firstname,
33
            :lastname => self.attr_lastname,
34
            :mail => self.attr_mail,
35
            :auth_source_id => self.id
36
        }
37
        # Email as account if you use Google Apps
38
        suffix = "@dingding.me";
39
        if not login.end_with?(suffix)
40
            login += suffix
41
        end
42
        # Authenticate
43
        begin
44
            imap = Net::IMAP.new(self.host, self.port)
45
            imap.authenticate('LOGIN', login, password)
46
        rescue Net::IMAP::NoResponseError => e
47
            retVal = nil
48
        end
49
        return retVal
50
    end
51

    
52
    def auth_method_name
53
        "IMAP" 
54
    end
55
end
    (1-1/1)