1
0

user.rb 3.0 KB

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