2015-01-19 13:50:01 -05:00
|
|
|
require_dependency 'rate_limiter'
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class Invite < ActiveRecord::Base
|
2015-01-19 13:50:01 -05:00
|
|
|
include RateLimiter::OnCreateRecord
|
2013-05-07 00:39:01 -04:00
|
|
|
include Trashable
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2015-01-19 13:50:01 -05:00
|
|
|
rate_limit :limit_invites_per_day
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
2013-02-28 13:54:12 -05:00
|
|
|
belongs_to :invited_by, class_name: 'User'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-05-08 02:45:49 -04:00
|
|
|
has_many :invited_groups
|
|
|
|
has_many :groups, through: :invited_groups
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :topic_invites
|
2013-02-07 10:45:24 -05:00
|
|
|
has_many :topics, through: :topic_invites, source: :topic
|
2013-02-05 14:16:51 -05:00
|
|
|
validates_presence_of :invited_by_id
|
2014-10-09 10:44:15 -04:00
|
|
|
validates :email, email: true
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
before_create do
|
|
|
|
self.invite_key ||= SecureRandom.hex
|
|
|
|
end
|
|
|
|
|
2014-07-14 11:56:26 -04:00
|
|
|
before_validation do
|
|
|
|
self.email = Email.downcase(email) unless email.nil?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
validate :user_doesnt_already_exist
|
|
|
|
attr_accessor :email_already_exists
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def user_doesnt_already_exist
|
|
|
|
@email_already_exists = false
|
|
|
|
return if email.blank?
|
2014-05-06 09:41:59 -04:00
|
|
|
u = User.find_by("email = ?", Email.downcase(email))
|
2014-01-21 16:53:46 -05:00
|
|
|
if u && u.id != self.user_id
|
2013-02-05 14:16:51 -05: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 16:53:46 -05:00
|
|
|
# link_valid? indicates whether the invite link can be used to log in to the site
|
|
|
|
def link_valid?
|
|
|
|
invalidated_at.nil?
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def redeem
|
2014-01-21 16:53:46 -05:00
|
|
|
InviteRedeemer.new(self).redeem unless expired? || destroyed? || !link_valid?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-06 12:56:26 -05: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.
|
2014-05-08 21:45:18 -04:00
|
|
|
def self.invite_by_email(email, invited_by, topic=nil, group_ids=nil)
|
2013-11-06 12:56:26 -05:00
|
|
|
lower_email = Email.downcase(email)
|
2014-05-08 21:45:18 -04:00
|
|
|
user = User.find_by(email: lower_email)
|
|
|
|
|
|
|
|
if user
|
|
|
|
topic.grant_permission_to_user(lower_email) if topic && topic.private_message?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2014-01-21 15:13:55 -05:00
|
|
|
invite = Invite.with_deleted
|
2014-05-08 21:45:18 -04:00
|
|
|
.where(email: lower_email, invited_by_id: invited_by.id)
|
2014-01-21 15:13:55 -05:00
|
|
|
.order('created_at DESC')
|
|
|
|
.first
|
|
|
|
|
2014-05-08 21:45:18 -04:00
|
|
|
if invite && (invite.expired? || invite.deleted_at)
|
2014-01-21 15:13:55 -05:00
|
|
|
invite.destroy
|
|
|
|
invite = nil
|
|
|
|
end
|
2013-11-06 12:56:26 -05:00
|
|
|
|
2014-05-08 21:45:18 -04:00
|
|
|
if !invite
|
|
|
|
invite = Invite.create!(invited_by: invited_by, email: lower_email)
|
2013-11-06 12:56:26 -05:00
|
|
|
end
|
|
|
|
|
2014-05-08 21:45:18 -04: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)
|
|
|
|
group_ids.each do |group_id|
|
|
|
|
invite.invited_groups.create!(group_id: group_id)
|
|
|
|
end
|
|
|
|
end
|
2013-11-06 12:56:26 -05:00
|
|
|
|
|
|
|
Jobs.enqueue(:invite_email, invite_id: invite.id)
|
2014-05-08 21:45:18 -04:00
|
|
|
|
|
|
|
invite.reload
|
2013-11-06 12:56:26 -05:00
|
|
|
invite
|
|
|
|
end
|
|
|
|
|
2014-07-14 11:56:26 -04:00
|
|
|
# generate invite tokens without email
|
|
|
|
def self.generate_disposable_tokens(invited_by, quantity=nil, group_names=nil)
|
|
|
|
invite_tokens = []
|
|
|
|
quantity ||= 1
|
|
|
|
group_ids = get_group_ids(group_names)
|
|
|
|
|
|
|
|
quantity.to_i.times do
|
|
|
|
invite = Invite.create!(invited_by: invited_by)
|
|
|
|
group_ids = group_ids - invite.invited_groups.pluck(:group_id)
|
|
|
|
group_ids.each do |group_id|
|
|
|
|
invite.invited_groups.create!(group_id: group_id)
|
|
|
|
end
|
|
|
|
invite_tokens.push(invite.invite_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
invite_tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-08-01 07:06:31 -04:00
|
|
|
def self.find_all_invites_from(inviter, offset=0)
|
2013-11-08 14:11:41 -05:00
|
|
|
Invite.where(invited_by_id: inviter.id)
|
|
|
|
.includes(:user => :user_stat)
|
|
|
|
.order('CASE WHEN invites.user_id IS NOT NULL THEN 0 ELSE 1 END',
|
|
|
|
'user_stats.time_read DESC',
|
|
|
|
'invites.redeemed_at DESC')
|
2014-08-01 07:06:31 -04:00
|
|
|
.limit(SiteSetting.invites_per_page)
|
|
|
|
.offset(offset)
|
2013-11-08 14:11:41 -05:00
|
|
|
.references('user_stats')
|
|
|
|
end
|
|
|
|
|
2014-08-01 07:06:31 -04:00
|
|
|
def self.find_redeemed_invites_from(inviter, offset=0)
|
|
|
|
find_all_invites_from(inviter, offset).where('invites.user_id IS NOT NULL')
|
2013-11-08 14:11:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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 11:44:28 -05:00
|
|
|
all
|
2013-11-08 14:11:41 -05:00
|
|
|
end
|
|
|
|
end
|
2014-01-21 16:53:46 -05:00
|
|
|
|
|
|
|
def self.invalidate_for_email(email)
|
2014-05-06 09:41:59 -04:00
|
|
|
i = Invite.find_by(email: Email.downcase(email))
|
2014-01-21 16:53:46 -05:00
|
|
|
if i
|
|
|
|
i.invalidated_at = Time.zone.now
|
|
|
|
i.save
|
|
|
|
end
|
|
|
|
i
|
|
|
|
end
|
2014-05-27 16:14:37 -04:00
|
|
|
|
2014-07-03 16:06:49 -04:00
|
|
|
def self.redeem_from_email(email)
|
|
|
|
invite = Invite.find_by(email: Email.downcase(email))
|
|
|
|
if invite
|
|
|
|
InviteRedeemer.new(invite).redeem
|
|
|
|
end
|
|
|
|
invite
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:10:35 -04:00
|
|
|
def self.redeem_from_token(token, email, username=nil, name=nil, topic_id=nil)
|
2014-07-14 11:56:26 -04:00
|
|
|
invite = Invite.find_by(invite_key: token)
|
|
|
|
if invite
|
|
|
|
invite.update_column(:email, email)
|
2014-07-16 17:40:38 -04:00
|
|
|
invite.topic_invites.create!(invite_id: invite.id, topic_id: topic_id) if topic_id && Topic.find_by_id(topic_id) && !invite.topic_invites.pluck(:topic_id).include?(topic_id)
|
2014-07-14 11:56:26 -04:00
|
|
|
user = InviteRedeemer.new(invite, username, name).redeem
|
|
|
|
end
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2014-10-06 14:48:56 -04:00
|
|
|
def resend_invite
|
|
|
|
self.update_columns(created_at: Time.zone.now, updated_at: Time.zone.now)
|
|
|
|
Jobs.enqueue(:invite_email, invite_id: self.id)
|
|
|
|
end
|
|
|
|
|
2015-01-19 13:50:01 -05:00
|
|
|
def limit_invites_per_day
|
2015-02-11 01:45:46 -05:00
|
|
|
RateLimiter.new(invited_by, "invites-per-day", SiteSetting.max_invites_per_day, 1.day.to_i)
|
2015-01-19 13:50:01 -05:00
|
|
|
end
|
|
|
|
|
2014-05-27 16:14:37 -04:00
|
|
|
def self.base_directory
|
2014-11-25 11:55:09 -05:00
|
|
|
File.join(Rails.root, "public", "uploads", "csv", RailsMultisite::ConnectionManagement.current_db)
|
2014-05-27 16:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.chunk_path(identifier, filename, chunk_number)
|
|
|
|
File.join(Invite.base_directory, "tmp", identifier, "#{filename}.part#{chunk_number}")
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: invites
|
|
|
|
#
|
2014-02-06 19:07:36 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# invite_key :string(32) not null
|
2014-07-17 02:10:44 -04:00
|
|
|
# email :string(255)
|
2014-02-06 19:07:36 -05:00
|
|
|
# invited_by_id :integer not null
|
|
|
|
# user_id :integer
|
|
|
|
# redeemed_at :datetime
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-02-06 19:07:36 -05:00
|
|
|
# deleted_at :datetime
|
|
|
|
# deleted_by_id :integer
|
|
|
|
# invalidated_at :datetime
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-07-29 13:57:08 -04:00
|
|
|
# index_invites_on_email_and_invited_by_id (email,invited_by_id)
|
2013-05-23 22:48:32 -04:00
|
|
|
# index_invites_on_invite_key (invite_key) UNIQUE
|
|
|
|
#
|