1
0

user.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. validates_presence_of :username
  13. validates :username, uniqueness: { case_sensitive: false }
  14. validates_format_of :username, :with => /\A[a-zA-Z0-9_-]{3,190}\Z/, :message => "can only contain letters, numbers, underscores, and dashes, and must be between 3 and 190 characters in length."
  15. validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid", if: -> { !requires_no_invitation_code? && User.using_invitation_code? }
  16. has_many :user_credentials, :dependent => :destroy, :inverse_of => :user
  17. has_many :events, -> { order("events.created_at desc") }, :dependent => :delete_all, :inverse_of => :user
  18. has_many :agents, -> { order("agents.created_at desc") }, :dependent => :destroy, :inverse_of => :user
  19. has_many :logs, :through => :agents, :class_name => "AgentLog"
  20. has_many :scenarios, :inverse_of => :user, :dependent => :destroy
  21. has_many :services, -> { by_name('asc') }, :dependent => :destroy
  22. def available_services
  23. Service.available_to_user(self).by_name
  24. end
  25. # Allow users to login via either email or username.
  26. def self.find_first_by_auth_conditions(warden_conditions)
  27. conditions = warden_conditions.dup
  28. if login = conditions.delete(:login)
  29. where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
  30. else
  31. where(conditions).first
  32. end
  33. end
  34. def active?
  35. !deactivated_at
  36. end
  37. def deactivate!
  38. User.transaction do
  39. agents.update_all(deactivated: true)
  40. update_attribute(:deactivated_at, Time.now)
  41. end
  42. end
  43. def activate!
  44. User.transaction do
  45. agents.update_all(deactivated: false)
  46. update_attribute(:deactivated_at, nil)
  47. end
  48. end
  49. def active_for_authentication?
  50. super && active?
  51. end
  52. def inactive_message
  53. active? ? super : :deactivated_account
  54. end
  55. def self.using_invitation_code?
  56. ENV['SKIP_INVITATION_CODE'] != 'true'
  57. end
  58. def requires_no_invitation_code!
  59. @requires_no_invitation_code = true
  60. end
  61. def requires_no_invitation_code?
  62. !!@requires_no_invitation_code
  63. end
  64. def undefined_agent_types
  65. agents.reorder('').group(:type).pluck(:type).select do |type|
  66. begin
  67. type.constantize
  68. false
  69. rescue NameError
  70. true
  71. end
  72. end
  73. end
  74. def undefined_agents
  75. agents.where(type: undefined_agent_types).select('id, schedule, events_count, type as undefined')
  76. end
  77. end