2009-01-07 20:27:12 +01:00
|
|
|
require 'ezcrypto'
|
2011-06-24 23:48:08 +02:00
|
|
|
require 'imapmailbox'
|
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
class LoginController < ApplicationController
|
2011-06-24 23:48:08 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def index
|
|
|
|
if not(logged_user.nil?)
|
2011-06-24 23:48:08 +02:00
|
|
|
redirect_to :controller =>"webmail", :action=>"index"
|
2009-01-07 20:27:12 +01:00
|
|
|
else
|
|
|
|
@login_user = Customer.new
|
2011-06-24 23:48:08 +02:00
|
|
|
end
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-24 23:48:08 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def authenticate
|
2009-01-07 20:55:58 +01:00
|
|
|
if user = auth(params['login_user']["email"], params['login_user']["password"])
|
|
|
|
session["user"] = user.id
|
2009-01-07 20:27:12 +01:00
|
|
|
if CDF::CONFIG[:crypt_session_pass]
|
2009-01-07 20:55:58 +01:00
|
|
|
session["wmp"] = EzCrypto::Key.encrypt_with_password(CDF::CONFIG[:encryption_password], CDF::CONFIG[:encryption_salt], params['login_user']["password"])
|
2009-01-07 20:27:12 +01:00
|
|
|
else
|
|
|
|
# dont use crypt
|
2009-01-07 20:55:58 +01:00
|
|
|
session["wmp"] = params['login_user']["password"]
|
2011-06-24 23:48:08 +02:00
|
|
|
end
|
2009-01-07 20:55:58 +01:00
|
|
|
if session["return_to"]
|
2009-01-07 21:06:34 +01:00
|
|
|
redirect_to(session["return_to"])
|
2009-01-07 20:55:58 +01:00
|
|
|
session["return_to"] = nil
|
2009-01-07 20:27:12 +01:00
|
|
|
else
|
2011-06-24 23:48:08 +02:00
|
|
|
redirect_to :action=>"index"
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
|
|
|
else
|
2011-06-24 23:48:08 +02:00
|
|
|
logger.debug "*** Not logged"
|
2009-01-07 20:27:12 +01:00
|
|
|
@login_user = Customer.new
|
2009-09-01 14:23:05 +02:00
|
|
|
flash["error"] = t :wrong_email_or_password
|
2011-06-24 23:48:08 +02:00
|
|
|
redirect_to :action => "index"
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
|
|
|
end
|
2011-06-24 23:48:08 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def logout
|
|
|
|
reset_session
|
2011-03-29 02:59:28 +02:00
|
|
|
flash["status"] = t(:user_logged_out)
|
2011-06-24 23:48:08 +02:00
|
|
|
redirect_to :action => "index"
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-24 23:48:08 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
protected
|
|
|
|
|
|
|
|
def need_subdomain?() true end
|
|
|
|
def secure_user?() false end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def auth(email, password)
|
2011-06-24 23:48:08 +02:00
|
|
|
mailbox = IMAPMailbox.new(Rails.logger)
|
|
|
|
logger.info "*** mailbox #{mailbox.inspect}"
|
2009-01-07 20:27:12 +01:00
|
|
|
begin
|
2011-06-24 23:48:08 +02:00
|
|
|
mailbox.connect(email, password)
|
|
|
|
rescue Exception => exc
|
|
|
|
logger.debug "*** auth/Mailbox Object => #{exc.message}"
|
|
|
|
return nil
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-24 23:48:08 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
mailbox.disconnect
|
|
|
|
mailbox = nil
|
|
|
|
if user = Customer.find_by_email(email)
|
|
|
|
return user
|
|
|
|
else
|
|
|
|
# create record in database
|
|
|
|
user = Customer.create("email"=>email)
|
|
|
|
MailPref.create('customer_id' => user.id)
|
|
|
|
return user
|
2011-06-24 23:48:08 +02:00
|
|
|
end
|
|
|
|
end
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|