2017-02-13 15:19:41 -06:00
|
|
|
InviteRedeemer = Struct.new(:invite, :username, :name, :password) do
|
2013-05-30 10:33:11 -05:00
|
|
|
|
|
|
|
def redeem
|
|
|
|
Invite.transaction do
|
2014-07-01 11:52:52 -05:00
|
|
|
if invite_was_redeemed?
|
|
|
|
process_invitation
|
|
|
|
return invited_user
|
|
|
|
end
|
2013-05-30 10:33:11 -05:00
|
|
|
end
|
|
|
|
|
2014-07-01 11:52:52 -05:00
|
|
|
# If `invite_passthrough_hours` is defined, allow them to re-use the invite link
|
|
|
|
# to login again.
|
2014-07-16 18:38:16 -05:00
|
|
|
if invite.redeemed_at && invite.redeemed_at >= SiteSetting.invite_passthrough_hours.hours.ago
|
2014-07-01 11:52:52 -05:00
|
|
|
return invited_user
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2013-05-30 10:33:11 -05:00
|
|
|
end
|
|
|
|
|
2013-08-28 02:18:31 -05:00
|
|
|
# extracted from User cause it is very specific to invites
|
2017-02-13 15:19:41 -06:00
|
|
|
def self.create_user_from_invite(invite, username, name, password=nil)
|
2014-07-03 15:06:49 -05:00
|
|
|
user_exists = User.find_by_email(invite.email)
|
|
|
|
return user if user_exists
|
|
|
|
|
2014-07-15 14:48:29 -05:00
|
|
|
if username && UsernameValidator.new(username).valid_format? && User.username_available?(username)
|
2014-07-14 10:56:26 -05:00
|
|
|
available_username = username
|
|
|
|
else
|
|
|
|
available_username = UserNameSuggester.suggest(invite.email)
|
|
|
|
end
|
|
|
|
available_name = name || available_username
|
2013-08-28 02:18:31 -05:00
|
|
|
|
2014-07-14 10:56:26 -05:00
|
|
|
user = User.new(email: invite.email, username: available_username, name: available_name, active: true, trust_level: SiteSetting.default_invitee_trust_level)
|
2016-09-20 12:12:00 -05:00
|
|
|
|
2017-02-13 15:19:41 -06:00
|
|
|
if password
|
|
|
|
user.password_required!
|
|
|
|
user.password = password
|
|
|
|
end
|
|
|
|
|
2017-03-05 07:55:21 -06:00
|
|
|
if !SiteSetting.must_approve_users? || (SiteSetting.must_approve_users? && invite.invited_by.staff?)
|
|
|
|
user.approved = true
|
|
|
|
user.approved_by_id = invite.invited_by_id
|
|
|
|
user.approved_at = Time.zone.now
|
|
|
|
end
|
|
|
|
|
2016-09-20 12:12:00 -05:00
|
|
|
user.moderator = true if invite.moderator? && invite.invited_by.staff?
|
2013-08-28 02:18:31 -05:00
|
|
|
user.save!
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2013-05-30 10:33:11 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def invited_user
|
|
|
|
@invited_user ||= get_invited_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_invitation
|
2017-03-05 07:55:21 -06:00
|
|
|
approve_account_if_needed
|
2013-05-30 10:33:11 -05:00
|
|
|
add_to_private_topics_if_invited
|
|
|
|
add_user_to_invited_topics
|
2014-05-08 01:45:49 -05:00
|
|
|
add_user_to_groups
|
2013-05-30 10:33:11 -05:00
|
|
|
send_welcome_message
|
|
|
|
notify_invitee
|
2015-03-25 11:55:18 -05:00
|
|
|
delete_duplicate_invites
|
2013-05-30 10:33:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def invite_was_redeemed?
|
|
|
|
# Return true if a row was updated
|
|
|
|
mark_invite_redeemed == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_invite_redeemed
|
2013-07-01 13:45:52 -05:00
|
|
|
Invite.where(['id = ? AND redeemed_at IS NULL AND created_at >= ?',
|
|
|
|
invite.id, SiteSetting.invite_expiry_days.days.ago]).update_all('redeemed_at = CURRENT_TIMESTAMP')
|
2013-05-30 10:33:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_invited_user
|
|
|
|
result = get_existing_user
|
2017-02-13 15:19:41 -06:00
|
|
|
result ||= InviteRedeemer.create_user_from_invite(invite, username, name, password)
|
2013-05-30 10:33:11 -05:00
|
|
|
result.send_welcome_message = false
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_existing_user
|
2014-05-06 08:41:59 -05:00
|
|
|
User.find_by(email: invite.email)
|
2013-05-30 10:33:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def add_to_private_topics_if_invited
|
|
|
|
invite.topics.private_messages.each do |t|
|
|
|
|
t.topic_allowed_users.create(user_id: invited_user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_user_to_invited_topics
|
|
|
|
Invite.where('invites.email = ? and invites.id != ?', invite.email, invite.id).includes(:topics).where(topics: {archetype: Archetype::private_message}).each do |i|
|
|
|
|
i.topics.each do |t|
|
|
|
|
t.topic_allowed_users.create(user_id: invited_user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-08 01:45:49 -05:00
|
|
|
def add_user_to_groups
|
2015-02-25 21:05:44 -06:00
|
|
|
new_group_ids = invite.groups.pluck(:id) - invited_user.group_users.pluck(:group_id)
|
|
|
|
new_group_ids.each do |id|
|
|
|
|
invited_user.group_users.create(group_id: id)
|
2014-05-08 01:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 10:33:11 -05:00
|
|
|
def send_welcome_message
|
2013-07-01 13:45:52 -05:00
|
|
|
if Invite.where(['email = ?', invite.email]).update_all(['user_id = ?', invited_user.id]) == 1
|
2013-05-30 10:33:11 -05:00
|
|
|
invited_user.send_welcome_message = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-10 20:21:39 -05:00
|
|
|
def approve_account_if_needed
|
2017-03-05 07:55:21 -06:00
|
|
|
if get_existing_user
|
|
|
|
invited_user.approve(invite.invited_by_id, false)
|
|
|
|
end
|
2013-07-10 20:21:39 -05:00
|
|
|
end
|
|
|
|
|
2013-05-30 10:33:11 -05:00
|
|
|
def notify_invitee
|
2016-07-12 22:58:31 -05:00
|
|
|
if inviter = invite.invited_by
|
|
|
|
inviter.notifications.create(notification_type: Notification.types[:invitee_accepted],
|
|
|
|
data: {display_username: invited_user.username}.to_json)
|
|
|
|
end
|
2013-05-30 10:33:11 -05:00
|
|
|
end
|
2015-03-25 11:55:18 -05:00
|
|
|
|
|
|
|
def delete_duplicate_invites
|
2015-03-25 12:59:37 -05:00
|
|
|
Invite.where('invites.email = ? AND redeemed_at IS NULL AND invites.id != ?', invite.email, invite.id).delete_all
|
2015-03-25 11:55:18 -05:00
|
|
|
end
|
2013-07-10 20:21:39 -05:00
|
|
|
end
|