user.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_MODULES = [:database_authenticatable, :registerable,
  4. :recoverable, :rememberable, :trackable,
  5. :validatable, :lockable, :omniauthable,
  6. (ENV['REQUIRE_CONFIRMED_EMAIL'] == 'true' ? :confirmable : nil)].compact
  7. devise *DEVISE_MODULES
  8. INVITATION_CODES = [ENV['INVITATION_CODE'] || 'try-huginn']
  9. # Virtual attribute for authenticating by either username or email
  10. # This is in addition to a real persisted field like 'username'
  11. attr_accessor :login
  12. ACCESSIBLE_ATTRIBUTES = [ :email, :username, :login, :password, :password_confirmation, :remember_me, :invitation_code ]
  13. attr_accessible *ACCESSIBLE_ATTRIBUTES
  14. attr_accessible *(ACCESSIBLE_ATTRIBUTES + [:admin]), :as => :admin
  15. validates_presence_of :username
  16. validates :username, uniqueness: { case_sensitive: false }
  17. 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."
  18. validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid", if: -> { !requires_no_invitation_code? && User.using_invitation_code? }
  19. has_many :user_credentials, :dependent => :destroy, :inverse_of => :user
  20. has_many :events, -> { order("events.created_at desc") }, :dependent => :delete_all, :inverse_of => :user
  21. has_many :agents, -> { order("agents.created_at desc") }, :dependent => :destroy, :inverse_of => :user
  22. has_many :logs, :through => :agents, :class_name => "AgentLog"
  23. has_many :scenarios, :inverse_of => :user, :dependent => :destroy
  24. has_many :services, -> { by_name('asc') }, :dependent => :destroy
  25. def available_services
  26. Service.available_to_user(self).by_name
  27. end
  28. # Allow users to login via either email or username.
  29. def self.find_first_by_auth_conditions(warden_conditions)
  30. conditions = warden_conditions.dup
  31. if login = conditions.delete(:login)
  32. where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
  33. else
  34. where(conditions).first
  35. end
  36. end
  37. def active?
  38. !deactivated_at
  39. end
  40. def deactivate!
  41. User.transaction do
  42. agents.update_all(deactivated: true)
  43. update_attribute(:deactivated_at, Time.now)
  44. end
  45. end
  46. def activate!
  47. User.transaction do
  48. agents.update_all(deactivated: false)
  49. update_attribute(:deactivated_at, nil)
  50. end
  51. end
  52. def active_for_authentication?
  53. super && active?
  54. end
  55. def inactive_message
  56. active? ? super : :deactivated_account
  57. end
  58. def self.using_invitation_code?
  59. ENV['SKIP_INVITATION_CODE'] != 'true'
  60. end
  61. def requires_no_invitation_code!
  62. @requires_no_invitation_code = true
  63. end
  64. def requires_no_invitation_code?
  65. !!@requires_no_invitation_code
  66. end
  67. end