2011-07-24 22:22:13 +02:00
|
|
|
require 'ezcrypto'
|
|
|
|
|
2011-07-23 21:55:26 +02:00
|
|
|
class User < ActiveRecord::Base
|
2011-07-27 20:34:30 +02:00
|
|
|
|
2011-09-16 19:44:29 +02:00
|
|
|
#acts_as_notes_owner
|
|
|
|
|
2011-07-23 21:55:26 +02:00
|
|
|
validates_presence_of :first_name,:last_name
|
2011-09-29 21:16:40 +02:00
|
|
|
validates_uniqueness_of :login
|
2011-07-23 21:55:26 +02:00
|
|
|
has_many :servers, :dependent => :destroy
|
2011-07-24 15:56:47 +02:00
|
|
|
has_one :prefs, :dependent => :destroy
|
2011-07-27 20:34:30 +02:00
|
|
|
has_many :folders, :dependent => :destroy
|
2011-08-02 23:12:17 +02:00
|
|
|
has_many :messages, :dependent => :destroy
|
2011-08-16 20:05:58 +02:00
|
|
|
has_many :contacts, :dependent => :destroy
|
2011-07-24 22:22:13 +02:00
|
|
|
|
|
|
|
def set_cached_password(session,password)
|
|
|
|
if $defaults['session_encryption']
|
|
|
|
session[:session_salt] = generate_salt
|
|
|
|
session[:user_password] = EzCrypto::Key.encrypt_with_password($defaults['session_password'], session[:session_salt], password)
|
|
|
|
else
|
|
|
|
session[:user_password] = password
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_cached_password(session)
|
|
|
|
if $defaults['session_encryption']
|
|
|
|
EzCrypto::Key.decrypt_with_password($defaults['session_password'], session[:session_salt], session[:user_password])
|
|
|
|
else
|
|
|
|
session[:user_password]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_salt
|
|
|
|
(0...8).map{65.+(rand(25)).chr}.join
|
|
|
|
end
|
|
|
|
|
2011-09-29 21:16:40 +02:00
|
|
|
def name
|
2011-09-03 13:07:40 +02:00
|
|
|
first_name + " " + last_name
|
|
|
|
end
|
|
|
|
|
2011-09-29 21:16:40 +02:00
|
|
|
def full_id
|
|
|
|
(name + " <" + email + ">") if email
|
|
|
|
end
|
|
|
|
|
|
|
|
def email
|
|
|
|
if login =~ /\@/
|
|
|
|
login
|
2011-09-03 13:07:40 +02:00
|
|
|
else
|
2011-09-29 21:16:40 +02:00
|
|
|
(login + "@" + domain) if domain.presence
|
2011-09-03 13:07:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-09 22:10:25 +02:00
|
|
|
def username
|
2011-09-29 21:16:40 +02:00
|
|
|
login.gsub(/\@/,"_").gsub(/\./,"_")
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_domain?
|
|
|
|
return domain if domain.presence
|
|
|
|
if login =~ /\@/
|
|
|
|
login.split(/\@/)[1]
|
|
|
|
end
|
2011-09-09 22:10:25 +02:00
|
|
|
end
|
|
|
|
|
2011-07-23 21:55:26 +02:00
|
|
|
end
|