foreman/app/models/user.rb @ d97375e0
686cb440 | Ohad Levy | require 'digest/sha1'
|
|
1ba05a93 | Ohad Levy | class User < ActiveRecord::Base
|
|
7a4ec5cf | Paul Kelly | attr_protected :password_hash, :password_salt, :admin
|
|
attr_accessor :password, :password_confirmation
|
|||
1ba05a93 | Ohad Levy | belongs_to :auth_source
|
|
f685dd61 | Ohad Levy | has_many :changes, :class_name => 'Audit', :as => :user
|
|
9c0e127b | Paul Kelly | has_many :usergroups, :through => :usergroup_member
|
|
has_many :direct_hosts, :as => :owner, :class_name => "Host"
|
|||
27210309 | Paul Kelly | has_and_belongs_to_many :notices, :join_table => 'user_notices'
|
|
6874bbd9 | Paul Kelly | ||
1ba05a93 | Ohad Levy | validates_uniqueness_of :login, :message => "already exists"
|
|
7a4ec5cf | Paul Kelly | validates_presence_of :login, :mail, :auth_source_id
|
|
validates_presence_of :password_hash, :if => Proc.new {|user| user.manage_password?}
|
|||
validates_confirmation_of :password, :if => Proc.new {|user| user.manage_password?}, :unless => Proc.new {|user| user.password.empty?}
|
|||
1ba05a93 | Ohad Levy | validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i
|
|
validates_length_of :login, :maximum => 30
|
|||
validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-\.]*$/i, :allow_nil => true
|
|||
validates_length_of :firstname, :lastname, :maximum => 30, :allow_nil => true
|
|||
validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true
|
|||
validates_length_of :mail, :maximum => 60, :allow_nil => true
|
|||
f5df7d44 | Paul Kelly | before_destroy Ensure_not_used_by.new(:hosts), :ensure_admin_is_not_deleted
|
|
7a4ec5cf | Paul Kelly | validate :name_used_in_a_usergroup
|
|
before_validation :prepare_password
|
|||
9c0e127b | Paul Kelly | ||
1ba05a93 | Ohad Levy | def to_label
|
|
"#{firstname} #{lastname}"
|
|||
end
|
|||
9c0e127b | Paul Kelly | alias_method :name, :to_label
|
|
def <=>(other)
|
|||
self.name <=> other.name
|
|||
end
|
|||
# The text item to see in a select dropdown menu
|
|||
def select_title
|
|||
6874bbd9 | Paul Kelly | to_label + " (#{login})"
|
|
9c0e127b | Paul Kelly | end
|
|
1ba05a93 | Ohad Levy | ||
f5df7d44 | Paul Kelly | def self.create_admin
|
|
email = SETTINGS[:administrator] || "root@" + Facter.domain
|
|||
user = User.create(:login => "admin", :firstname => "Admin", :lastname => "User",
|
|||
:mail => email, :auth_source => AuthSourceInternal.first, :password => "changeme")
|
|||
user.update_attribute :admin, true
|
|||
user
|
|||
end
|
|||
1ba05a93 | Ohad Levy | def self.try_to_login(login, password)
|
|
# Make sure no one can sign in with an empty password
|
|||
return nil if password.to_s.empty?
|
|||
if user = find(:first, :conditions => ["login=?", login])
|
|||
# user is already in local database
|
|||
if user.auth_source
|
|||
7a4ec5cf | Paul Kelly | # user has an authentication method
|
|
1ba05a93 | Ohad Levy | return nil unless user.auth_source.authenticate(login, password)
|
|
end
|
|||
else
|
|||
# user is not yet registered, try to authenticate with available sources
|
|||
attrs = AuthSource.authenticate(login, password)
|
|||
if attrs
|
|||
user = new(*attrs)
|
|||
user.login = login
|
|||
if user.save
|
|||
user.reload
|
|||
7a4ec5cf | Paul Kelly | logger.info "User '#{user.login}' auto-created from #{user.auth_source}"
|
|
1ba05a93 | Ohad Levy | else
|
|
7a4ec5cf | Paul Kelly | logger.info "Failed to save User '#{user.login}' #{user.errors.full_messages}"
|
|
1ba05a93 | Ohad Levy | end
|
|
end
|
|||
end
|
|||
7a4ec5cf | Paul Kelly | user.update_attribute(:last_login_on, Time.now.utc) if user and not user.new_record?
|
|
return user
|
|||
1ba05a93 | Ohad Levy | rescue => text
|
|
raise text
|
|||
end
|
|||
7a4ec5cf | Paul Kelly | def matching_password?(pass)
|
|
self.password_hash == encrypt_password(pass)
|
|||
end
|
|||
9c0e127b | Paul Kelly | def indirect_hosts
|
|
all_groups = []
|
|||
for usergroup in usergroups
|
|||
all_groups += usergroup.all_usergroups
|
|||
end
|
|||
all_groups.uniq.map{|g| g.hosts}.flatten.uniq
|
|||
end
|
|||
def hosts
|
|||
direct_hosts + indirect_hosts
|
|||
end
|
|||
def recipients
|
|||
[mail]
|
|||
end
|
|||
7a4ec5cf | Paul Kelly | def manage_password?
|
|
auth_source and auth_source.can_set_password?
|
|||
end
|
|||
private
|
|||
def prepare_password
|
|||
unless password.blank?
|
|||
self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
|
|||
self.password_hash = encrypt_password(password)
|
|||
end
|
|||
end
|
|||
def encrypt_password(pass)
|
|||
Digest::SHA1.hexdigest([pass, password_salt].join)
|
|||
end
|
|||
9c0e127b | Paul Kelly | ||
7a4ec5cf | Paul Kelly | def name_used_in_a_usergroup
|
|
9c0e127b | Paul Kelly | if Usergroup.all.map(&:name).include?(self.login)
|
|
errors.add_to_base "A usergroup already exists with this name"
|
|||
end
|
|||
end
|
|||
f5df7d44 | Paul Kelly | # The internal Admin Account is always available
|
|
# this is required as when not using external authentication, the systems logs you in with the
|
|||
# admin account automatically
|
|||
def ensure_admin_is_not_deleted
|
|||
if login == "admin"
|
|||
errors.add_to_base "Can't Delete Internal Admin account"
|
|||
logger.warn "Unable to delete Internal Admin Account"
|
|||
return false
|
|||
end
|
|||
end
|
|||
1ba05a93 | Ohad Levy | end
|