2017-06-09 03:10:43 +08:00
|
|
|
InviteRedeemer = Struct.new(:invite, :username, :name, :password, :user_custom_fields) do
|
2013-05-30 23:33:11 +08:00
|
|
|
|
|
|
|
def redeem
|
|
|
|
Invite.transaction do
|
2014-07-02 00:52:52 +08:00
|
|
|
if invite_was_redeemed?
|
|
|
|
process_invitation
|
|
|
|
return invited_user
|
|
|
|
end
|
2013-05-30 23:33:11 +08:00
|
|
|
end
|
|
|
|
|
2014-07-02 00:52:52 +08:00
|
|
|
nil
|
2013-05-30 23:33:11 +08:00
|
|
|
end
|
|
|
|
|
2013-08-28 15:18:31 +08:00
|
|
|
# extracted from User cause it is very specific to invites
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.create_user_from_invite(invite, username, name, password = nil, user_custom_fields = nil)
|
2014-07-16 03:48:29 +08:00
|
|
|
if username && UsernameValidator.new(username).valid_format? && User.username_available?(username)
|
2014-07-14 23:56:26 +08:00
|
|
|
available_username = username
|
|
|
|
else
|
|
|
|
available_username = UserNameSuggester.suggest(invite.email)
|
|
|
|
end
|
|
|
|
available_name = name || available_username
|
2013-08-28 15:18:31 +08:00
|
|
|
|
2018-01-19 22:29:15 +08:00
|
|
|
user_params = {
|
|
|
|
email: invite.email,
|
|
|
|
username: available_username,
|
|
|
|
name: available_name,
|
2018-12-11 06:24:02 +08:00
|
|
|
active: false,
|
2018-01-19 22:29:15 +08:00
|
|
|
trust_level: SiteSetting.default_invitee_trust_level
|
|
|
|
}
|
|
|
|
|
|
|
|
user = User.unstage(user_params)
|
|
|
|
user = User.new(user_params) if user.nil?
|
2016-09-21 01:12:00 +08:00
|
|
|
|
2017-03-05 21:55:21 +08:00
|
|
|
if !SiteSetting.must_approve_users? || (SiteSetting.must_approve_users? && invite.invited_by.staff?)
|
2019-01-04 01:03:01 +08:00
|
|
|
ReviewableUser.set_approved_fields!(user, invite.invited_by)
|
2017-03-05 21:55:21 +08:00
|
|
|
end
|
|
|
|
|
2017-06-09 03:10:43 +08:00
|
|
|
user_fields = UserField.all
|
|
|
|
if user_custom_fields.present? && user_fields.present?
|
|
|
|
field_params = user_custom_fields || {}
|
|
|
|
fields = user.custom_fields
|
|
|
|
|
|
|
|
user_fields.each do |f|
|
|
|
|
field_val = field_params[f.id.to_s]
|
2018-09-07 06:02:47 +08:00
|
|
|
fields["#{User::USER_FIELD_PREFIX}#{f.id}"] = field_val[0...UserField.max_length] unless field_val.blank?
|
2017-06-09 03:10:43 +08:00
|
|
|
end
|
|
|
|
user.custom_fields = fields
|
|
|
|
end
|
|
|
|
|
2016-09-21 01:12:00 +08:00
|
|
|
user.moderator = true if invite.moderator? && invite.invited_by.staff?
|
2013-08-28 15:18:31 +08:00
|
|
|
|
2017-11-24 00:39:24 +08:00
|
|
|
if password
|
|
|
|
user.password = password
|
|
|
|
user.password_required!
|
|
|
|
end
|
|
|
|
|
|
|
|
user.save!
|
2018-12-11 06:24:02 +08:00
|
|
|
|
|
|
|
if invite.via_email
|
2018-12-12 01:04:07 +08:00
|
|
|
user.email_tokens.create!(email: user.email)
|
2018-12-11 06:24:02 +08:00
|
|
|
user.activate
|
|
|
|
end
|
|
|
|
|
2017-11-24 00:39:24 +08:00
|
|
|
User.find(user.id)
|
2013-08-28 15:18:31 +08:00
|
|
|
end
|
|
|
|
|
2013-05-30 23:33:11 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def invited_user
|
|
|
|
@invited_user ||= get_invited_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_invitation
|
2017-03-05 21:55:21 +08:00
|
|
|
approve_account_if_needed
|
2013-05-30 23:33:11 +08:00
|
|
|
add_to_private_topics_if_invited
|
2014-05-08 14:45:49 +08:00
|
|
|
add_user_to_groups
|
2013-05-30 23:33:11 +08:00
|
|
|
send_welcome_message
|
|
|
|
notify_invitee
|
2015-03-26 00:55:18 +08:00
|
|
|
delete_duplicate_invites
|
2013-05-30 23:33:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def invite_was_redeemed?
|
|
|
|
# Return true if a row was updated
|
|
|
|
mark_invite_redeemed == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_invite_redeemed
|
2018-12-11 06:24:02 +08: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 23:33:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_invited_user
|
|
|
|
result = get_existing_user
|
2017-06-09 03:10:43 +08:00
|
|
|
result ||= InviteRedeemer.create_user_from_invite(invite, username, name, password, user_custom_fields)
|
2013-05-30 23:33:11 +08:00
|
|
|
result.send_welcome_message = false
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_existing_user
|
2018-01-19 22:29:15 +08:00
|
|
|
User.where(admin: false, staged: false).find_by_email(invite.email)
|
2013-05-30 23:33:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_to_private_topics_if_invited
|
2019-03-30 00:03:33 +08:00
|
|
|
topic_ids = Topic.where(archetype: Archetype::private_message).includes(:invites).where(invites: { email: invite.email }).pluck(:id)
|
|
|
|
topic_ids.each do |id|
|
|
|
|
TopicAllowedUser.create(user_id: invited_user.id, topic_id: id) unless TopicAllowedUser.exists?(user_id: invited_user.id, topic_id: id)
|
2013-05-30 23:33:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-08 14:45:49 +08:00
|
|
|
def add_user_to_groups
|
2015-02-26 11:05:44 +08: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 14:45:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 23:33:11 +08:00
|
|
|
def send_welcome_message
|
2018-12-11 06:24:02 +08:00
|
|
|
if Invite.where('email = ?', invite.email).update_all(['user_id = ?', invited_user.id]) == 1
|
2013-05-30 23:33:11 +08:00
|
|
|
invited_user.send_welcome_message = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-11 09:21:39 +08:00
|
|
|
def approve_account_if_needed
|
2019-01-04 01:03:01 +08:00
|
|
|
if invited_user.present? && reviewable_user = ReviewableUser.find_by(target: invited_user)
|
|
|
|
reviewable_user.perform(
|
|
|
|
invite.invited_by,
|
|
|
|
:approve,
|
|
|
|
send_email: false,
|
|
|
|
approved_by_invite: true
|
|
|
|
)
|
2017-03-05 21:55:21 +08:00
|
|
|
end
|
2013-07-11 09:21:39 +08:00
|
|
|
end
|
|
|
|
|
2013-05-30 23:33:11 +08:00
|
|
|
def notify_invitee
|
2016-07-13 11:58:31 +08:00
|
|
|
if inviter = invite.invited_by
|
2017-07-28 09:20:09 +08:00
|
|
|
inviter.notifications.create(notification_type: Notification.types[:invitee_accepted],
|
|
|
|
data: { display_username: invited_user.username }.to_json)
|
2016-07-13 11:58:31 +08:00
|
|
|
end
|
2013-05-30 23:33:11 +08:00
|
|
|
end
|
2015-03-26 00:55:18 +08:00
|
|
|
|
|
|
|
def delete_duplicate_invites
|
2015-03-26 01:59:37 +08:00
|
|
|
Invite.where('invites.email = ? AND redeemed_at IS NULL AND invites.id != ?', invite.email, invite.id).delete_all
|
2015-03-26 00:55:18 +08:00
|
|
|
end
|
2013-07-11 09:21:39 +08:00
|
|
|
end
|