user.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. require "civicrm"
  2. class User < ActiveRecord::Base
  3. include CiviCRM::UserMethods
  4. # Include default devise modules. Others available are:
  5. # :confirmable, :lockable, :timeoutable and :omniauthable
  6. devise :database_authenticatable, :confirmable, :registerable,
  7. :recoverable, :rememberable, :validatable, :lockable,
  8. remember_for: 90.days
  9. has_many :signatures
  10. has_many :user_preferences
  11. has_many :events, class_name: Ahoy::Event
  12. belongs_to :partner
  13. validates :email, email: true
  14. validate :password_complexity
  15. delegate :actions, :views, to: :events
  16. before_update :invalidate_password_reset_tokens, if: :email_changed?
  17. before_update :invalidate_new_activists_password, if: :admin_changed?
  18. after_validation :reset_password_expiration_flag, if: :encrypted_password_changed?
  19. alias_attribute :activist?, :admin?
  20. alias :preferences :user_preferences
  21. def invalidate_password_reset_tokens
  22. self.reset_password_token = nil
  23. end
  24. def invalidate_new_activists_password
  25. self.password_expired = true
  26. end
  27. def reset_password_expiration_flag
  28. self.password_expired = false
  29. end
  30. def email_taken?
  31. errors.added? :email, :taken
  32. end
  33. def send_email_taken_notice
  34. if self.confirmed?
  35. UserMailer.signup_attempt_with_existing_email(self).deliver_now
  36. else
  37. send_confirmation_instructions
  38. end
  39. end
  40. def password_complexity
  41. if admin? && password.present? and password.length < 30
  42. errors.add :password, "must be at least 30 (try choosing 6 memorable words)"
  43. end
  44. end
  45. def name
  46. [first_name, last_name].join(" ")
  47. end
  48. def percentile_rank
  49. user_action_counts = Rails.cache.fetch("user_action_counts", expires_in: 24.hours) {
  50. User.select("users.id, count(ahoy_events.id) AS events_count")
  51. .joins("LEFT OUTER JOIN ahoy_events ON ahoy_events.user_id = users.id")
  52. .where("ahoy_events.name IS null OR ahoy_events.name = ?", "Action")
  53. .group("users.id")
  54. .map { |u| u.events_count }
  55. }
  56. user_count = events.actions.count
  57. percentile = user_action_counts.percentile_rank(user_count - 1).round(0)
  58. end
  59. def signed?(petition)
  60. return false unless record_activity?
  61. Signature.where(user: self, petition: petition).exists?
  62. end
  63. def taken_action?(action_page)
  64. return false unless record_activity?
  65. actions.on_page(action_page).exists?
  66. end
  67. def partner?
  68. partner.present?
  69. end
  70. def can?(ability)
  71. case ability
  72. when :browse_actions
  73. admin? || activist? || collaborator?
  74. when :administer_actions
  75. admin? || activist?
  76. when :administer_homepage
  77. admin? || activist?
  78. when :view_analytics
  79. admin? || activist? || collaborator?
  80. when :administer_partners?
  81. admin? || activist?
  82. when :administer_topics?
  83. admin? || activist?
  84. when :administer_users?
  85. admin?
  86. else
  87. admin?
  88. end
  89. end
  90. def privileged_role?
  91. admin? || activist? || collaborator?
  92. end
  93. # This is here for collission avoidance when generating new user names in tests
  94. def self.next_id
  95. self.last.nil? ? 1 : self.last.id + 1
  96. end
  97. protected
  98. def after_confirmation
  99. subscribe!(opt_in = true) if self.subscribe?
  100. end
  101. end