2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-14 05:15:56 -04:00
|
|
|
InviteRedeemer = Struct.new(:invite, :email, :username, :name, :password, :user_custom_fields, :ip_address, :session, :email_token, keyword_init: true) do
|
2013-05-30 11:33:11 -04:00
|
|
|
|
|
|
|
def redeem
|
|
|
|
Invite.transaction do
|
2014-07-01 12:52:52 -04:00
|
|
|
if invite_was_redeemed?
|
|
|
|
process_invitation
|
2021-04-21 05:36:32 -04:00
|
|
|
invited_user
|
2014-07-01 12:52:52 -04:00
|
|
|
end
|
2013-05-30 11:33:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-28 03:18:31 -04:00
|
|
|
# extracted from User cause it is very specific to invites
|
2021-04-14 05:15:56 -04:00
|
|
|
def self.create_user_from_invite(email:, invite:, username: nil, name: nil, password: nil, user_custom_fields: nil, ip_address: nil, session: nil, email_token: nil)
|
2021-07-11 17:57:38 -04:00
|
|
|
if username && UsernameValidator.new(username).valid_format? && User.username_available?(username, email)
|
2014-07-14 11:56:26 -04:00
|
|
|
available_username = username
|
|
|
|
else
|
2020-06-09 11:19:32 -04:00
|
|
|
available_username = UserNameSuggester.suggest(email)
|
2014-07-14 11:56:26 -04:00
|
|
|
end
|
2013-08-28 03:18:31 -04:00
|
|
|
|
2021-12-08 10:06:57 -05:00
|
|
|
if email.present? && invite.domain.present?
|
|
|
|
username, domain = email.split('@')
|
|
|
|
if domain.present? && invite.domain != domain
|
|
|
|
raise ActiveRecord::RecordNotSaved.new(I18n.t('invite.domain_not_allowed'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-11 17:57:38 -04:00
|
|
|
user = User.where(staged: true).with_email(email.strip.downcase).first
|
|
|
|
user.unstage! if user
|
|
|
|
user ||= User.new
|
|
|
|
|
2020-03-17 11:48:24 -04:00
|
|
|
user.attributes = {
|
2020-06-09 11:19:32 -04:00
|
|
|
email: email,
|
2018-01-19 09:29:15 -05:00
|
|
|
username: available_username,
|
2020-03-17 11:48:24 -04:00
|
|
|
name: name || available_username,
|
2018-12-10 17:24:02 -05:00
|
|
|
active: false,
|
2019-04-13 03:34:25 -04:00
|
|
|
trust_level: SiteSetting.default_invitee_trust_level,
|
|
|
|
ip_address: ip_address,
|
|
|
|
registration_ip_address: ip_address
|
2018-01-19 09:29:15 -05:00
|
|
|
}
|
|
|
|
|
2020-03-31 14:29:15 -04:00
|
|
|
if !SiteSetting.must_approve_users? ||
|
|
|
|
(SiteSetting.must_approve_users? && invite.invited_by.staff?) ||
|
|
|
|
EmailValidator.can_auto_approve_user?(user.email)
|
2019-01-03 12:03:01 -05:00
|
|
|
ReviewableUser.set_approved_fields!(user, invite.invited_by)
|
2017-03-05 08:55:21 -05:00
|
|
|
end
|
|
|
|
|
2017-06-08 15:10:43 -04: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-06 18:02:47 -04:00
|
|
|
fields["#{User::USER_FIELD_PREFIX}#{f.id}"] = field_val[0...UserField.max_length] unless field_val.blank?
|
2017-06-08 15:10:43 -04:00
|
|
|
end
|
|
|
|
user.custom_fields = fields
|
|
|
|
end
|
|
|
|
|
2016-09-20 13:12:00 -04:00
|
|
|
user.moderator = true if invite.moderator? && invite.invited_by.staff?
|
2013-08-28 03:18:31 -04:00
|
|
|
|
2017-11-23 11:39:24 -05:00
|
|
|
if password
|
|
|
|
user.password = password
|
|
|
|
user.password_required!
|
|
|
|
end
|
|
|
|
|
2021-03-02 02:13:04 -05:00
|
|
|
authenticator = UserAuthenticator.new(user, session, require_password: false)
|
|
|
|
|
|
|
|
if !authenticator.has_authenticator? && !SiteSetting.enable_local_logins
|
|
|
|
raise ActiveRecord::RecordNotSaved.new(I18n.t("login.incorrect_username_email_or_password"))
|
|
|
|
end
|
|
|
|
|
|
|
|
authenticator.start
|
|
|
|
|
|
|
|
if authenticator.email_valid? && !authenticator.authenticated?
|
|
|
|
raise ActiveRecord::RecordNotSaved.new(I18n.t("login.incorrect_username_email_or_password"))
|
|
|
|
end
|
|
|
|
|
2017-11-23 11:39:24 -05:00
|
|
|
user.save!
|
2021-03-02 02:13:04 -05:00
|
|
|
authenticator.finish
|
2018-12-10 17:24:02 -05:00
|
|
|
|
2021-04-14 05:15:56 -04:00
|
|
|
if invite.emailed_status != Invite.emailed_status_types[:not_required] && email == invite.email && invite.email_token.present? && email_token == invite.email_token
|
2021-11-25 02:34:39 -05:00
|
|
|
user.email_tokens.create!(email: user.email, scope: EmailToken.scopes[:signup])
|
2018-12-10 17:24:02 -05:00
|
|
|
user.activate
|
|
|
|
end
|
|
|
|
|
2017-11-23 11:39:24 -05:00
|
|
|
User.find(user.id)
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|
|
|
|
|
2013-05-30 11:33:11 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def invited_user
|
|
|
|
@invited_user ||= get_invited_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_invitation
|
2017-03-05 08:55:21 -05:00
|
|
|
approve_account_if_needed
|
2013-05-30 11:33:11 -04:00
|
|
|
add_to_private_topics_if_invited
|
2014-05-08 02:45:49 -04:00
|
|
|
add_user_to_groups
|
2013-05-30 11:33:11 -04:00
|
|
|
send_welcome_message
|
|
|
|
notify_invitee
|
|
|
|
end
|
|
|
|
|
|
|
|
def invite_was_redeemed?
|
2020-06-09 11:19:32 -04:00
|
|
|
mark_invite_redeemed
|
2013-05-30 11:33:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def mark_invite_redeemed
|
2020-06-09 11:19:32 -04:00
|
|
|
if !invite.is_invite_link? && InvitedUser.exists?(invite_id: invite.id)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
existing_user = get_existing_user
|
|
|
|
if existing_user.present? && InvitedUser.exists?(user_id: existing_user.id, invite_id: invite.id)
|
|
|
|
return false
|
|
|
|
end
|
2019-05-13 03:42:13 -04:00
|
|
|
|
2020-06-09 11:19:32 -04:00
|
|
|
@invited_user_record = InvitedUser.create!(invite_id: invite.id, redeemed_at: Time.zone.now)
|
2021-01-20 03:50:02 -05:00
|
|
|
if @invited_user_record.present?
|
2020-06-09 11:19:32 -04:00
|
|
|
Invite.increment_counter(:redemption_count, invite.id)
|
2019-05-13 03:42:13 -04:00
|
|
|
delete_duplicate_invites
|
|
|
|
end
|
|
|
|
|
2020-06-09 11:19:32 -04:00
|
|
|
@invited_user_record.present?
|
2013-05-30 11:33:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_invited_user
|
|
|
|
result = get_existing_user
|
2021-03-02 02:13:04 -05:00
|
|
|
result ||= InviteRedeemer.create_user_from_invite(
|
|
|
|
email: email,
|
|
|
|
invite: invite,
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
user_custom_fields: user_custom_fields,
|
|
|
|
ip_address: ip_address,
|
2021-04-14 05:15:56 -04:00
|
|
|
session: session,
|
|
|
|
email_token: email_token
|
2021-03-02 02:13:04 -05:00
|
|
|
)
|
2013-05-30 11:33:11 -04:00
|
|
|
result.send_welcome_message = false
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_existing_user
|
2020-06-09 11:19:32 -04:00
|
|
|
User.where(admin: false, staged: false).find_by_email(email)
|
2013-05-30 11:33:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_to_private_topics_if_invited
|
2020-06-09 11:19:32 -04:00
|
|
|
topic_ids = Topic.where(archetype: Archetype::private_message).includes(:invites).where(invites: { email: email }).pluck(:id)
|
2019-03-29 12:03:33 -04:00
|
|
|
topic_ids.each do |id|
|
2020-06-01 22:33:30 -04:00
|
|
|
TopicAllowedUser.create!(user_id: invited_user.id, topic_id: id) unless TopicAllowedUser.exists?(user_id: invited_user.id, topic_id: id)
|
2013-05-30 11:33:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-08 02:45:49 -04:00
|
|
|
def add_user_to_groups
|
2020-06-15 05:13:56 -04:00
|
|
|
guardian = Guardian.new(invite.invited_by)
|
2015-02-25 22:05:44 -05:00
|
|
|
new_group_ids = invite.groups.pluck(:id) - invited_user.group_users.pluck(:group_id)
|
|
|
|
new_group_ids.each do |id|
|
2020-06-15 05:13:56 -04:00
|
|
|
group = Group.find_by(id: id)
|
|
|
|
if guardian.can_edit_group?(group)
|
|
|
|
invited_user.group_users.create!(group_id: group.id)
|
|
|
|
DiscourseEvent.trigger(:user_added_to_group, invited_user, group, automatic: false)
|
|
|
|
end
|
2014-05-08 02:45:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 11:33:11 -04:00
|
|
|
def send_welcome_message
|
2020-06-09 11:19:32 -04:00
|
|
|
@invited_user_record.update!(user_id: invited_user.id)
|
|
|
|
invited_user.send_welcome_message = true
|
2013-05-30 11:33:11 -04:00
|
|
|
end
|
|
|
|
|
2013-07-10 21:21:39 -04:00
|
|
|
def approve_account_if_needed
|
2020-12-02 08:08:26 -05:00
|
|
|
if invited_user.present? && reviewable_user = ReviewableUser.find_by(target: invited_user, status: Reviewable.statuses[:pending])
|
2019-01-03 12:03:01 -05:00
|
|
|
reviewable_user.perform(
|
|
|
|
invite.invited_by,
|
2019-04-17 11:26:43 -04:00
|
|
|
:approve_user,
|
2019-01-03 12:03:01 -05:00
|
|
|
send_email: false,
|
|
|
|
approved_by_invite: true
|
|
|
|
)
|
2017-03-05 08:55:21 -05:00
|
|
|
end
|
2013-07-10 21:21:39 -04:00
|
|
|
end
|
|
|
|
|
2013-05-30 11:33:11 -04:00
|
|
|
def notify_invitee
|
2016-07-12 23:58:31 -04:00
|
|
|
if inviter = invite.invited_by
|
2020-06-01 22:45:18 -04:00
|
|
|
inviter.notifications.create!(
|
|
|
|
notification_type: Notification.types[:invitee_accepted],
|
|
|
|
data: { display_username: invited_user.username }.to_json
|
|
|
|
)
|
2016-07-12 23:58:31 -04:00
|
|
|
end
|
2013-05-30 11:33:11 -04:00
|
|
|
end
|
2015-03-25 12:55:18 -04:00
|
|
|
|
|
|
|
def delete_duplicate_invites
|
2021-03-03 04:45:29 -05:00
|
|
|
Invite
|
|
|
|
.where('invites.max_redemptions_allowed = 1')
|
2020-06-09 11:19:32 -04:00
|
|
|
.joins("LEFT JOIN invited_users ON invites.id = invited_users.invite_id")
|
|
|
|
.where('invited_users.user_id IS NULL')
|
|
|
|
.where('invites.email = ? AND invites.id != ?', email, invite.id)
|
|
|
|
.delete_all
|
2015-03-25 12:55:18 -04:00
|
|
|
end
|
2013-07-10 21:21:39 -04:00
|
|
|
end
|