Project

General

Profile

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

Sunding Wei, 2013-02-21 09:33

 
##
# Redmine IMAP Authentication Module
#
# All rights avoided, you are free to distribute or modify
# the source code under the terms you reserve the author information.
#
# Author:
# Dingding Technology, Inc.
# Sunding Wei <swei(at)dingding.me>
#
# File: redmine/app/models/auth_source_imap.rb
#
require "net/imap"
require 'timeout'

#
# HOWTO
#
# 1. Execute the SQL
# INSERT INTO auth_sources (type, name) values ("AuthSourceIMAP", "IMAP")
# 2. Change as you like
# 3. Redmine: set the user authentication mode to IMAP
# 4. Restart your web server
#

class AuthSourceIMAP < AuthSource
def authenticate(login, password)
# Define your IMAP server
self.host = "imap.qq.com"
self.port = 143
retVal = {
:firstname => self.attr_firstname,
:lastname => self.attr_lastname,
:mail => self.attr_mail,
:auth_source_id => self.id
}
# Email as account if you use Google Apps
suffix = "@dingding.me";
if not login.end_with?(suffix)
login += suffix
end
# Authenticate
begin
imap = Net::IMAP.new(self.host, self.port)
imap.authenticate('LOGIN', login, password)
rescue Net::IMAP::NoResponseError => e
retVal = nil
end
return retVal
end

def auth_method_name
"IMAP"
end
end
    (1-1/1)