2007-03-30 04:36:52 +00:00
|
|
|
require 'digest/sha1'
|
2011-09-07 17:19:04 +02:00
|
|
|
require 'bcrypt'
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
2007-07-08 06:41:10 +00:00
|
|
|
# Virtual attribute for the unencrypted password
|
|
|
|
|
attr_accessor :password
|
2011-01-16 18:14:07 +01:00
|
|
|
attr_protected :is_admin # don't allow mass-assignment for this
|
2012-04-24 20:47:07 +02:00
|
|
|
|
|
|
|
|
attr_accessible :login, :first_name, :last_name, :password_confirmation, :password, :auth_type, :open_id_url
|
|
|
|
|
#for will_paginate plugin
|
|
|
|
|
cattr_accessor :per_page
|
|
|
|
|
@@per_page = 5
|
2007-07-08 06:41:10 +00:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
has_many :contexts,
|
|
|
|
|
:order => 'position ASC',
|
|
|
|
|
:dependent => :delete_all do
|
2012-04-17 16:47:37 +02:00
|
|
|
def find_by_params(params)
|
2013-02-27 11:50:49 +01:00
|
|
|
find(params['id'] || params['context_id']) || nil
|
2012-04-17 16:47:37 +02:00
|
|
|
end
|
2009-10-02 19:45:49 -04:00
|
|
|
def update_positions(context_ids)
|
2011-01-16 18:14:07 +01:00
|
|
|
context_ids.each_with_index {|id, position|
|
2009-10-02 19:45:49 -04:00
|
|
|
context = self.detect { |c| c.id == id.to_i }
|
2010-10-31 21:27:13 +08:00
|
|
|
raise I18n.t('models.user.error_context_not_associated', :context => id, :user => @user.id) if context.nil?
|
2009-10-02 19:45:49 -04:00
|
|
|
context.update_attribute(:position, position + 1)
|
2011-01-16 18:14:07 +01:00
|
|
|
}
|
2009-10-02 19:45:49 -04:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
has_many :projects,
|
2008-03-18 19:10:34 +00:00
|
|
|
:order => 'projects.position ASC',
|
2007-03-30 04:36:52 +00:00
|
|
|
:dependent => :delete_all do
|
2012-04-17 16:47:37 +02:00
|
|
|
def find_by_params(params)
|
2013-02-27 11:50:49 +01:00
|
|
|
find(params['id'] || params['project_id'])
|
2012-04-17 16:47:37 +02:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
def update_positions(project_ids)
|
2011-01-16 18:14:07 +01:00
|
|
|
project_ids.each_with_index {|id, position|
|
2007-03-30 04:36:52 +00:00
|
|
|
project = self.detect { |p| p.id == id.to_i }
|
2010-10-31 21:27:13 +08:00
|
|
|
raise I18n.t('models.user.error_project_not_associated', :project => id, :user => @user.id) if project.nil?
|
2007-03-30 04:36:52 +00:00
|
|
|
project.update_attribute(:position, position + 1)
|
2011-01-16 18:14:07 +01:00
|
|
|
}
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
def projects_in_state_by_position(state)
|
2012-04-18 14:22:58 +02:00
|
|
|
self.sort{ |a,b| a.position <=> b.position }.select{ |p| p.state == state }
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
def next_from(project)
|
|
|
|
|
self.offset_from(project, 1)
|
|
|
|
|
end
|
|
|
|
|
def previous_from(project)
|
|
|
|
|
self.offset_from(project, -1)
|
|
|
|
|
end
|
|
|
|
|
def offset_from(project, offset)
|
|
|
|
|
projects = self.projects_in_state_by_position(project.state)
|
|
|
|
|
position = projects.index(project)
|
|
|
|
|
return nil if position == 0 && offset < 0
|
|
|
|
|
projects.at( position + offset)
|
|
|
|
|
end
|
|
|
|
|
def cache_note_counts
|
2012-04-18 14:22:58 +02:00
|
|
|
project_note_counts = Note.group(:project_id).count
|
2007-03-30 04:36:52 +00:00
|
|
|
self.each do |project|
|
|
|
|
|
project.cached_note_count = project_note_counts[project.id] || 0
|
|
|
|
|
end
|
|
|
|
|
end
|
2007-04-06 05:45:21 +00:00
|
|
|
def alphabetize(scope_conditions = {})
|
2012-04-18 14:22:58 +02:00
|
|
|
projects = where(scope_conditions)
|
2007-04-08 04:26:13 +00:00
|
|
|
projects.sort!{ |x,y| x.name.downcase <=> y.name.downcase }
|
2007-04-06 05:45:21 +00:00
|
|
|
self.update_positions(projects.map{ |p| p.id })
|
|
|
|
|
return projects
|
|
|
|
|
end
|
2011-06-12 04:29:35 +02:00
|
|
|
def actionize(scope_conditions = {})
|
2012-04-18 14:22:58 +02:00
|
|
|
todos_in_project = where(scope_conditions).includes(:todos)
|
2011-06-12 04:29:35 +02:00
|
|
|
todos_in_project.sort!{ |x, y| -(x.todos.active.count <=> y.todos.active.count) }
|
|
|
|
|
todos_in_project.reject{ |p| p.todos.active.count > 0 }
|
|
|
|
|
sorted_project_ids = todos_in_project.map {|p| p.id}
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2012-04-18 14:22:58 +02:00
|
|
|
all_project_ids = all.map {|p| p.id}
|
2011-06-12 04:29:35 +02:00
|
|
|
other_project_ids = all_project_ids - sorted_project_ids
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2011-06-12 04:29:35 +02:00
|
|
|
update_positions(sorted_project_ids + other_project_ids)
|
2011-04-29 23:15:41 +02:00
|
|
|
|
2012-04-18 14:22:58 +02:00
|
|
|
return where(scope_conditions)
|
2008-09-23 17:06:14 -03:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
has_many :todos,
|
2007-08-28 12:38:16 +00:00
|
|
|
:order => 'todos.completed_at DESC, todos.created_at DESC',
|
2012-10-04 10:20:26 -04:00
|
|
|
:dependent => :delete_all do
|
|
|
|
|
def count_by_group(g)
|
|
|
|
|
except(:order).group(g).count
|
|
|
|
|
end
|
|
|
|
|
end
|
2008-07-19 20:27:45 +02:00
|
|
|
has_many :recurring_todos,
|
|
|
|
|
:order => 'recurring_todos.completed_at DESC, recurring_todos.created_at DESC',
|
|
|
|
|
:dependent => :delete_all
|
2007-03-30 04:36:52 +00:00
|
|
|
has_many :deferred_todos,
|
|
|
|
|
:class_name => 'Todo',
|
|
|
|
|
:conditions => [ 'state = ?', 'deferred' ],
|
|
|
|
|
:order => 'show_from ASC, todos.created_at DESC' do
|
|
|
|
|
def find_and_activate_ready
|
2012-04-18 14:22:58 +02:00
|
|
|
where('show_from <= ?', Time.zone.now).collect { |t| t.activate! }
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
has_many :notes, :order => "created_at DESC", :dependent => :delete_all
|
|
|
|
|
has_one :preference, :dependent => :destroy
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
validates_presence_of :login
|
|
|
|
|
validates_presence_of :password, :if => :password_required?
|
|
|
|
|
validates_length_of :password, :within => 5..40, :if => :password_required?
|
2007-07-07 04:31:32 +00:00
|
|
|
validates_presence_of :password_confirmation, :if => :password_required?
|
2011-09-30 19:49:18 +02:00
|
|
|
validates_confirmation_of :password
|
2007-03-30 04:36:52 +00:00
|
|
|
validates_length_of :login, :within => 3..80
|
|
|
|
|
validates_uniqueness_of :login, :on => :create
|
2012-04-17 16:47:37 +02:00
|
|
|
validate :validate_auth_type
|
2007-03-30 04:36:52 +00:00
|
|
|
|
2007-07-17 04:47:35 +00:00
|
|
|
before_create :crypt_password, :generate_token
|
2007-07-08 06:41:10 +00:00
|
|
|
before_update :crypt_password
|
2008-07-14 13:10:55 -04:00
|
|
|
|
2012-04-17 16:47:37 +02:00
|
|
|
def validate_auth_type
|
2007-03-30 04:36:52 +00:00
|
|
|
unless Tracks::Config.auth_schemes.include?(auth_type)
|
2007-07-16 02:18:07 +00:00
|
|
|
errors.add("auth_type", "not a valid authentication type (#{auth_type})")
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
alias_method :prefs, :preference
|
|
|
|
|
|
|
|
|
|
def self.authenticate(login, pass)
|
2007-11-26 01:57:21 +00:00
|
|
|
return nil if login.blank?
|
2012-04-18 14:22:58 +02:00
|
|
|
candidate = where("login = ?", login).first
|
2007-03-30 04:36:52 +00:00
|
|
|
return nil if candidate.nil?
|
2011-01-20 07:33:39 +08:00
|
|
|
|
|
|
|
|
if Tracks::Config.auth_schemes.include?('database')
|
2011-09-05 01:29:48 +02:00
|
|
|
return candidate if candidate.auth_type == 'database' and
|
|
|
|
|
candidate.password_matches? pass
|
2011-01-20 07:33:39 +08:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-11-26 02:00:09 +00:00
|
|
|
if Tracks::Config.auth_schemes.include?('ldap')
|
|
|
|
|
return candidate if candidate.auth_type == 'ldap' && SimpleLdapAuthenticator.valid?(login, pass)
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2011-01-20 07:33:39 +08:00
|
|
|
if Tracks::Config.auth_schemes.include?('cas')
|
|
|
|
|
# because we can not auth them with out thier real password we have to settle for this
|
|
|
|
|
return candidate if candidate.auth_type.eql?("cas")
|
|
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-11-26 02:00:09 +00:00
|
|
|
return nil
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def self.no_users_yet?
|
|
|
|
|
count == 0
|
|
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def self.find_admin
|
2012-04-17 16:47:37 +02:00
|
|
|
where(:is_admin => true).first
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def to_param
|
|
|
|
|
login
|
|
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def display_name
|
|
|
|
|
if first_name.blank? && last_name.blank?
|
|
|
|
|
return login
|
|
|
|
|
elsif first_name.blank?
|
|
|
|
|
return last_name
|
|
|
|
|
elsif last_name.blank?
|
|
|
|
|
return first_name
|
|
|
|
|
end
|
|
|
|
|
"#{first_name} #{last_name}"
|
|
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def change_password(pass,pass_confirm)
|
|
|
|
|
self.password = pass
|
|
|
|
|
self.password_confirmation = pass_confirm
|
|
|
|
|
save!
|
|
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def time
|
2008-06-17 01:13:25 -04:00
|
|
|
Time.now.in_time_zone(prefs.time_zone)
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def date
|
2008-09-13 13:33:48 -07:00
|
|
|
time.midnight
|
|
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2008-09-13 13:33:48 -07:00
|
|
|
def at_midnight(date)
|
2008-11-29 12:00:06 -05:00
|
|
|
return ActiveSupport::TimeZone[prefs.time_zone].local(date.year, date.month, date.day, 0, 0, 0)
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-07-17 04:47:35 +00:00
|
|
|
def generate_token
|
2012-04-05 22:19:47 +02:00
|
|
|
self.token = Digest::SHA1.hexdigest "#{Time.now.to_i}#{rand}"
|
2007-07-17 04:47:35 +00:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-07-08 06:41:10 +00:00
|
|
|
def remember_token?
|
2011-09-30 19:49:18 +02:00
|
|
|
remember_token_expires_at && Time.now.utc < remember_token_expires_at
|
2007-07-08 06:41:10 +00:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-07-08 06:41:10 +00:00
|
|
|
# These create and unset the fields required for remembering users between browser closes
|
|
|
|
|
def remember_me
|
|
|
|
|
self.remember_token_expires_at = 2.weeks.from_now.utc
|
2012-04-05 22:19:47 +02:00
|
|
|
self.remember_token ||= Digest::SHA1.hexdigest("#{login}--#{remember_token_expires_at}")
|
|
|
|
|
save
|
2007-07-08 06:41:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def forget_me
|
|
|
|
|
self.remember_token_expires_at = nil
|
|
|
|
|
self.remember_token = nil
|
2012-04-05 22:19:47 +02:00
|
|
|
save
|
2007-07-08 06:41:10 +00:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
2011-09-05 01:10:47 +02:00
|
|
|
# Returns true if the user has a password hashed using SHA-1.
|
|
|
|
|
def uses_deprecated_password?
|
|
|
|
|
crypted_password =~ /^[a-f0-9]{40}$/i
|
|
|
|
|
end
|
|
|
|
|
|
2011-09-05 01:29:48 +02:00
|
|
|
def password_matches?(pass)
|
|
|
|
|
if uses_deprecated_password?
|
2011-10-05 16:25:30 +02:00
|
|
|
crypted_password == sha1(pass)
|
2011-09-05 01:29:48 +02:00
|
|
|
else
|
|
|
|
|
BCrypt::Password.new(crypted_password) == pass
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2011-09-30 19:49:18 +02:00
|
|
|
def salted(s)
|
2011-07-23 10:52:38 +02:00
|
|
|
"#{Tracks::Config.salt}--#{s}--"
|
|
|
|
|
end
|
|
|
|
|
|
2011-09-30 19:49:18 +02:00
|
|
|
def sha1(s)
|
2012-04-17 16:47:37 +02:00
|
|
|
Digest::SHA1.hexdigest(salted(s))
|
2011-07-23 10:52:38 +02:00
|
|
|
end
|
|
|
|
|
|
2012-04-17 16:47:37 +02:00
|
|
|
def create_hash(s)
|
|
|
|
|
BCrypt::Password.create(s)
|
2007-07-08 06:41:10 +00:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2011-10-10 22:31:51 +02:00
|
|
|
protected
|
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def crypt_password
|
2007-07-08 06:41:10 +00:00
|
|
|
return if password.blank?
|
2012-04-17 16:47:37 +02:00
|
|
|
write_attribute("crypted_password", self.create_hash(password)) if password == password_confirmation
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def password_required?
|
2007-07-08 06:41:10 +00:00
|
|
|
auth_type == 'database' && crypted_password.blank? || !password.blank?
|
|
|
|
|
end
|
2011-09-30 19:49:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|