1
0

user.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Huginn is designed to be a multi-User system. Users have many Agents (and Events created by those Agents).
  2. class User < ActiveRecord::Base
  3. devise :database_authenticatable, :registerable,
  4. :recoverable, :rememberable, :trackable, :validatable, :lockable,
  5. :omniauthable
  6. INVITATION_CODES = [ENV['INVITATION_CODE'] || 'try-huginn']
  7. # Virtual attribute for authenticating by either username or email
  8. # This is in addition to a real persisted field like 'username'
  9. attr_accessor :login
  10. ACCESSIBLE_ATTRIBUTES = [ :email, :username, :login, :password, :password_confirmation, :remember_me, :invitation_code ]
  11. attr_accessible *ACCESSIBLE_ATTRIBUTES
  12. attr_accessible *(ACCESSIBLE_ATTRIBUTES + [:admin]), :as => :admin
  13. validates_presence_of :username
  14. validates_uniqueness_of :username
  15. validates_format_of :username, :with => /\A[a-zA-Z0-9_-]{3,15}\Z/, :message => "can only contain letters, numbers, underscores, and dashes, and must be between 3 and 15 characters in length."
  16. validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid"
  17. has_many :user_credentials, :dependent => :destroy, :inverse_of => :user
  18. has_many :events, -> { order("events.created_at desc") }, :dependent => :delete_all, :inverse_of => :user
  19. has_many :agents, -> { order("agents.created_at desc") }, :dependent => :destroy, :inverse_of => :user
  20. has_many :logs, :through => :agents, :class_name => "AgentLog"
  21. has_many :scenarios, :inverse_of => :user, :dependent => :destroy
  22. has_many :services, -> { by_name('asc') }, :dependent => :destroy
  23. def available_services
  24. Service.available_to_user(self).by_name
  25. end
  26. # Allow users to login via either email or username.
  27. def self.find_first_by_auth_conditions(warden_conditions)
  28. conditions = warden_conditions.dup
  29. if login = conditions.delete(:login)
  30. where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
  31. else
  32. where(conditions).first
  33. end
  34. end
  35. end