mailr/app/controllers/login_controller.rb

72 lines
1.7 KiB
Ruby
Raw Normal View History

require 'ezcrypto'
2011-06-24 23:48:08 +02:00
require 'imapmailbox'
class LoginController < ApplicationController
2011-06-24 23:48:08 +02:00
def index
if not(logged_user.nil?)
2011-06-24 23:48:08 +02:00
redirect_to :controller =>"webmail", :action=>"index"
else
@login_user = Customer.new
2011-06-24 23:48:08 +02:00
end
end
2011-06-24 23:48:08 +02: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
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"])
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"]
redirect_to(session["return_to"])
2009-01-07 20:55:58 +01:00
session["return_to"] = nil
else
2011-06-24 23:48:08 +02:00
redirect_to :action=>"index"
end
else
2011-06-24 23:48:08 +02:00
logger.debug "*** Not logged"
@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"
end
end
2011-06-24 23:48:08 +02:00
def logout
reset_session
flash["status"] = t(:user_logged_out)
2011-06-24 23:48:08 +02:00
redirect_to :action => "index"
end
2011-06-24 23:48:08 +02: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}"
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
end
2011-06-24 23:48:08 +02: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
end