2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class Invite < ActiveRecord::Base
|
2019-07-19 00:59:12 -05:00
|
|
|
self.ignored_columns = %w{
|
|
|
|
via_email
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:43:33 -05:00
|
|
|
class UserExists < StandardError; end
|
2015-01-19 12:50:01 -06:00
|
|
|
include RateLimiter::OnCreateRecord
|
2013-05-06 23:39:01 -05:00
|
|
|
include Trashable
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2019-07-19 00:59:12 -05:00
|
|
|
BULK_INVITE_EMAIL_LIMIT = 200
|
|
|
|
|
2015-01-19 12:50:01 -06:00
|
|
|
rate_limit :limit_invites_per_day
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
2013-02-28 12:54:12 -06:00
|
|
|
belongs_to :invited_by, class_name: 'User'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-05-08 01:45:49 -05:00
|
|
|
has_many :invited_groups
|
|
|
|
has_many :groups, through: :invited_groups
|
2013-02-05 13:16:51 -06:00
|
|
|
has_many :topic_invites
|
2013-02-07 09:45:24 -06:00
|
|
|
has_many :topics, through: :topic_invites, source: :topic
|
2013-02-05 13:16:51 -06:00
|
|
|
validates_presence_of :invited_by_id
|
2019-02-06 11:08:06 -06:00
|
|
|
validates :email, email: true, format: { with: EmailValidator.email_regex }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
before_create do
|
|
|
|
self.invite_key ||= SecureRandom.hex
|
|
|
|
end
|
|
|
|
|
2014-07-14 10:56:26 -05:00
|
|
|
before_validation do
|
|
|
|
self.email = Email.downcase(email) unless email.nil?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
validate :user_doesnt_already_exist
|
|
|
|
attr_accessor :email_already_exists
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2019-07-19 00:59:12 -05:00
|
|
|
def self.emailed_status_types
|
|
|
|
@emailed_status_types ||= Enum.new(not_required: 0, pending: 1, bulk_pending: 2, sending: 3, sent: 4)
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def user_doesnt_already_exist
|
|
|
|
@email_already_exists = false
|
|
|
|
return if email.blank?
|
2018-01-19 08:29:15 -06:00
|
|
|
user = Invite.find_user_by_email(email)
|
2017-04-26 13:47:36 -05:00
|
|
|
|
|
|
|
if user && user.id != self.user_id
|
2013-02-05 13:16:51 -06:00
|
|
|
@email_already_exists = true
|
|
|
|
errors.add(:email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redeemed?
|
|
|
|
redeemed_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def expired?
|
|
|
|
created_at < SiteSetting.invite_expiry_days.days.ago
|
|
|
|
end
|
|
|
|
|
2014-01-21 15:53:46 -06:00
|
|
|
# link_valid? indicates whether the invite link can be used to log in to the site
|
|
|
|
def link_valid?
|
|
|
|
invalidated_at.nil?
|
|
|
|
end
|
|
|
|
|
2019-04-13 02:34:25 -05:00
|
|
|
def redeem(username: nil, name: nil, password: nil, user_custom_fields: nil, ip_address: nil)
|
2019-05-13 02:42:13 -05:00
|
|
|
if !expired? && !destroyed? && link_valid?
|
|
|
|
InviteRedeemer.new(self, username, name, password, user_custom_fields, ip_address).redeem
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.invite_by_email(email, invited_by, topic = nil, group_ids = nil, custom_message = nil)
|
|
|
|
create_invite_by_email(email, invited_by,
|
2016-09-20 12:12:00 -05:00
|
|
|
topic: topic,
|
|
|
|
group_ids: group_ids,
|
|
|
|
custom_message: custom_message,
|
2019-07-19 00:59:12 -05:00
|
|
|
emailed_status: emailed_status_types[:pending]
|
2017-07-27 20:20:09 -05:00
|
|
|
)
|
2015-08-31 09:06:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# generate invite link
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.generate_invite_link(email, invited_by, topic = nil, group_ids = nil)
|
|
|
|
invite = create_invite_by_email(email, invited_by,
|
2016-09-20 12:12:00 -05:00
|
|
|
topic: topic,
|
|
|
|
group_ids: group_ids,
|
2019-07-19 00:59:12 -05:00
|
|
|
emailed_status: emailed_status_types[:not_required]
|
2017-07-27 20:20:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
"#{Discourse.base_url}/invites/#{invite.invite_key}" if invite
|
2015-08-31 09:06:13 -05:00
|
|
|
end
|
|
|
|
|
2013-11-06 11:56:26 -06:00
|
|
|
# Create an invite for a user, supplying an optional topic
|
|
|
|
#
|
|
|
|
# Return the previously existing invite if already exists. Returns nil if the invite can't be created.
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.create_invite_by_email(email, invited_by, opts = nil)
|
2016-09-20 12:12:00 -05:00
|
|
|
opts ||= {}
|
|
|
|
|
|
|
|
topic = opts[:topic]
|
|
|
|
group_ids = opts[:group_ids]
|
|
|
|
custom_message = opts[:custom_message]
|
2019-07-19 00:59:12 -05:00
|
|
|
emailed_status = opts[:emailed_status] || emailed_status_types[:pending]
|
2013-11-06 11:56:26 -06:00
|
|
|
lower_email = Email.downcase(email)
|
2014-05-08 20:45:18 -05:00
|
|
|
|
2018-01-19 08:29:15 -06:00
|
|
|
if user = find_user_by_email(lower_email)
|
2018-12-05 09:43:07 -06:00
|
|
|
raise UserExists.new(I18n.t("invite.user_exists",
|
|
|
|
email: lower_email,
|
|
|
|
username: user.username,
|
|
|
|
base_path: Discourse.base_path
|
|
|
|
))
|
2014-05-08 20:45:18 -05:00
|
|
|
end
|
|
|
|
|
2014-01-21 14:13:55 -06:00
|
|
|
invite = Invite.with_deleted
|
2017-07-27 20:20:09 -05:00
|
|
|
.where(email: lower_email, invited_by_id: invited_by.id)
|
|
|
|
.order('created_at DESC')
|
|
|
|
.first
|
2014-01-21 14:13:55 -06:00
|
|
|
|
2014-05-08 20:45:18 -05:00
|
|
|
if invite && (invite.expired? || invite.deleted_at)
|
2014-01-21 14:13:55 -06:00
|
|
|
invite.destroy
|
|
|
|
invite = nil
|
|
|
|
end
|
2013-11-06 11:56:26 -06:00
|
|
|
|
2018-12-10 16:24:02 -06:00
|
|
|
if invite
|
2019-07-19 00:59:12 -05:00
|
|
|
if invite.emailed_status == Invite.emailed_status_types[:not_required]
|
|
|
|
emailed_status = invite.emailed_status
|
|
|
|
end
|
|
|
|
|
2018-12-10 16:24:02 -06:00
|
|
|
invite.update_columns(
|
|
|
|
created_at: Time.zone.now,
|
|
|
|
updated_at: Time.zone.now,
|
2019-07-19 00:59:12 -05:00
|
|
|
emailed_status: emailed_status
|
2018-12-10 16:24:02 -06:00
|
|
|
)
|
|
|
|
else
|
|
|
|
create_args = {
|
|
|
|
invited_by: invited_by,
|
|
|
|
email: lower_email,
|
2019-07-19 00:59:12 -05:00
|
|
|
emailed_status: emailed_status
|
2018-12-10 16:24:02 -06:00
|
|
|
}
|
2017-04-11 09:05:35 -05:00
|
|
|
|
2016-09-20 12:12:00 -05:00
|
|
|
create_args[:moderator] = true if opts[:moderator]
|
2017-06-30 05:39:37 -05:00
|
|
|
create_args[:custom_message] = custom_message if custom_message
|
2016-09-20 12:12:00 -05:00
|
|
|
invite = Invite.create!(create_args)
|
2013-11-06 11:56:26 -06:00
|
|
|
end
|
|
|
|
|
2014-05-08 20:45:18 -05:00
|
|
|
if topic && !invite.topic_invites.pluck(:topic_id).include?(topic.id)
|
|
|
|
invite.topic_invites.create!(invite_id: invite.id, topic_id: topic.id)
|
|
|
|
# to correct association
|
|
|
|
topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
if group_ids.present?
|
|
|
|
group_ids = group_ids - invite.invited_groups.pluck(:group_id)
|
2018-12-05 09:43:07 -06:00
|
|
|
|
2014-05-08 20:45:18 -05:00
|
|
|
group_ids.each do |group_id|
|
|
|
|
invite.invited_groups.create!(group_id: group_id)
|
|
|
|
end
|
|
|
|
end
|
2013-11-06 11:56:26 -06:00
|
|
|
|
2019-07-19 00:59:12 -05:00
|
|
|
if emailed_status == emailed_status_types[:pending]
|
|
|
|
invite.update_column(:emailed_status, Invite.emailed_status_types[:sending])
|
|
|
|
Jobs.enqueue(:invite_email, invite_id: invite.id)
|
|
|
|
end
|
2014-05-08 20:45:18 -05:00
|
|
|
|
|
|
|
invite.reload
|
2013-11-06 11:56:26 -06:00
|
|
|
invite
|
|
|
|
end
|
|
|
|
|
2018-01-19 08:29:15 -06:00
|
|
|
def self.find_user_by_email(email)
|
2019-10-30 01:08:47 -05:00
|
|
|
User.with_email(Email.downcase(email)).where(staged: false).first
|
2018-01-19 08:29:15 -06:00
|
|
|
end
|
|
|
|
|
2014-07-14 10:56:26 -05:00
|
|
|
def self.get_group_ids(group_names)
|
|
|
|
group_ids = []
|
|
|
|
if group_names
|
|
|
|
group_names = group_names.split(',')
|
|
|
|
group_names.each { |group_name|
|
|
|
|
group_detail = Group.find_by_name(group_name)
|
|
|
|
group_ids.push(group_detail.id) if group_detail
|
|
|
|
}
|
|
|
|
end
|
|
|
|
group_ids
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.find_all_invites_from(inviter, offset = 0, limit = SiteSetting.invites_per_page)
|
2013-11-08 13:11:41 -06:00
|
|
|
Invite.where(invited_by_id: inviter.id)
|
2017-07-27 20:20:09 -05:00
|
|
|
.where('invites.email IS NOT NULL')
|
|
|
|
.includes(user: :user_stat)
|
2018-06-05 02:29:17 -05:00
|
|
|
.order("CASE WHEN invites.user_id IS NOT NULL THEN 0 ELSE 1 END, user_stats.time_read DESC, invites.redeemed_at DESC")
|
2017-07-27 20:20:09 -05:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
.references('user_stats')
|
2013-11-08 13:11:41 -06:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.find_pending_invites_from(inviter, offset = 0)
|
2015-07-11 07:09:12 -05:00
|
|
|
find_all_invites_from(inviter, offset).where('invites.user_id IS NULL').order('invites.created_at DESC')
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.find_redeemed_invites_from(inviter, offset = 0)
|
2015-07-11 07:09:12 -05:00
|
|
|
find_all_invites_from(inviter, offset).where('invites.user_id IS NOT NULL').order('invites.redeemed_at DESC')
|
2013-11-08 13:11:41 -06:00
|
|
|
end
|
|
|
|
|
2015-08-24 14:03:25 -05:00
|
|
|
def self.find_pending_invites_count(inviter)
|
2017-11-14 04:38:54 -06:00
|
|
|
find_all_invites_from(inviter, 0, nil).where('invites.user_id IS NULL').reorder(nil).count
|
2015-08-24 14:03:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_redeemed_invites_count(inviter)
|
2017-11-14 04:38:54 -06:00
|
|
|
find_all_invites_from(inviter, 0, nil).where('invites.user_id IS NOT NULL').reorder(nil).count
|
2015-08-24 14:03:25 -05:00
|
|
|
end
|
|
|
|
|
2013-11-08 13:11:41 -06:00
|
|
|
def self.filter_by(email_or_username)
|
|
|
|
if email_or_username
|
|
|
|
where(
|
|
|
|
'(LOWER(invites.email) LIKE :filter) or (LOWER(users.username) LIKE :filter)',
|
|
|
|
filter: "%#{email_or_username.downcase}%"
|
|
|
|
)
|
|
|
|
else
|
2014-02-17 10:44:28 -06:00
|
|
|
all
|
2013-11-08 13:11:41 -06:00
|
|
|
end
|
|
|
|
end
|
2014-01-21 15:53:46 -06:00
|
|
|
|
|
|
|
def self.invalidate_for_email(email)
|
2014-05-06 08:41:59 -05:00
|
|
|
i = Invite.find_by(email: Email.downcase(email))
|
2014-01-21 15:53:46 -06:00
|
|
|
if i
|
|
|
|
i.invalidated_at = Time.zone.now
|
|
|
|
i.save
|
|
|
|
end
|
|
|
|
i
|
|
|
|
end
|
2014-05-27 15:14:37 -05:00
|
|
|
|
2014-07-03 15:06:49 -05:00
|
|
|
def self.redeem_from_email(email)
|
|
|
|
invite = Invite.find_by(email: Email.downcase(email))
|
2019-01-03 11:03:01 -06:00
|
|
|
InviteRedeemer.new(invite).redeem if invite
|
2014-07-03 15:06:49 -05:00
|
|
|
invite
|
|
|
|
end
|
|
|
|
|
2014-10-06 13:48:56 -05:00
|
|
|
def resend_invite
|
2019-11-04 14:15:46 -06:00
|
|
|
self.update_columns(updated_at: Time.zone.now)
|
2014-10-06 13:48:56 -05:00
|
|
|
Jobs.enqueue(:invite_email, invite_id: self.id)
|
|
|
|
end
|
|
|
|
|
2016-06-02 14:09:02 -05:00
|
|
|
def self.resend_all_invites_from(user_id)
|
|
|
|
Invite.where('invites.user_id IS NULL AND invites.email IS NOT NULL AND invited_by_id = ?', user_id).find_each do |invite|
|
2017-07-01 11:20:34 -05:00
|
|
|
invite.resend_invite
|
2016-06-02 14:09:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-07 03:49:46 -06:00
|
|
|
def self.rescind_all_expired_invites_from(user)
|
|
|
|
Invite.where('invites.user_id IS NULL AND invites.email IS NOT NULL AND invited_by_id = ? AND invites.created_at < ?',
|
|
|
|
user.id, SiteSetting.invite_expiry_days.days.ago).find_each do |invite|
|
2017-07-01 11:20:34 -05:00
|
|
|
invite.trash!(user)
|
2017-06-29 09:32:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-19 12:50:01 -06:00
|
|
|
def limit_invites_per_day
|
2015-02-11 00:45:46 -06:00
|
|
|
RateLimiter.new(invited_by, "invites-per-day", SiteSetting.max_invites_per_day, 1.day.to_i)
|
2015-01-19 12:50:01 -06:00
|
|
|
end
|
|
|
|
|
2014-05-27 15:14:37 -05:00
|
|
|
def self.base_directory
|
2014-11-25 10:55:09 -06:00
|
|
|
File.join(Rails.root, "public", "uploads", "csv", RailsMultisite::ConnectionManagement.current_db)
|
2014-05-27 15:14:37 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: invites
|
|
|
|
#
|
2014-02-06 18:07:36 -06:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# invite_key :string(32) not null
|
2019-01-11 13:29:56 -06:00
|
|
|
# email :string
|
2014-02-06 18:07:36 -06:00
|
|
|
# invited_by_id :integer not null
|
|
|
|
# user_id :integer
|
|
|
|
# redeemed_at :datetime
|
2014-08-27 00:19:25 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-02-06 18:07:36 -06:00
|
|
|
# deleted_at :datetime
|
|
|
|
# deleted_by_id :integer
|
|
|
|
# invalidated_at :datetime
|
2016-10-31 04:32:11 -05:00
|
|
|
# moderator :boolean default(FALSE), not null
|
2017-06-30 05:39:37 -05:00
|
|
|
# custom_message :text
|
2019-07-19 00:59:12 -05:00
|
|
|
# emailed_status :integer
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-07-29 12:57:08 -05:00
|
|
|
# index_invites_on_email_and_invited_by_id (email,invited_by_id)
|
2019-07-19 00:59:12 -05:00
|
|
|
# index_invites_on_emailed_status (emailed_status)
|
2013-05-23 21:48:32 -05:00
|
|
|
# index_invites_on_invite_key (invite_key) UNIQUE
|
|
|
|
#
|