Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export default Controller.extend(
accountEmail: alias("email"),
existingUserId: readOnly("model.existing_user_id"),
existingUserCanRedeem: readOnly("model.existing_user_can_redeem"),
existingUserCanRedeemError: readOnly(
"model.existing_user_can_redeem_error"
),
existingUserRedeeming: bool("existingUserId"),
hiddenEmail: alias("model.hidden_email"),
emailVerifiedByLink: alias("model.email_verified_by_link"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
{{#if this.existingUserCanRedeem}}
<DButton @class="btn-primary" @action={{action "submit"}} @type="submit" @disabled={{this.submitDisabled}} @label="invites.accept_invite" />
{{else}}
<div class="alert alert-error">{{i18n "invites.existing_user_cannot_redeem"}}</div>
<div class="alert alert-error">{{this.existingUserCanRedeemError}}</div>
{{/if}}
{{/if}}
{{/if}}
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/invites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ def show_invite(invite)
if current_user
info[:existing_user_id] = current_user.id
info[:existing_user_can_redeem] = invite.can_be_redeemed_by?(current_user)
info[:existing_user_can_redeem_error] = existing_user_can_redeem_error(invite)
info[:email] = current_user.email
info[:username] = current_user.username
end
Expand Down Expand Up @@ -493,4 +494,13 @@ def create_topic_invite_notifications(invite, user)
end
end
end

def existing_user_can_redeem_error(invite)
return if invite.can_be_redeemed_by?(current_user)
if invite.invited_users.exists?(user: current_user)
I18n.t("invite.existing_user_already_redemeed")
else
I18n.t("invite.existing_user_cannot_redeem")
end
end
end
6 changes: 5 additions & 1 deletion app/models/invite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def redeemable?
!redeemed? && !expired? && !deleted_at? && !destroyed? && link_valid?
end

def redeemed_by_user?(redeeming_user)
self.invited_users.exists?(user: redeeming_user)
end

def redeemed?
if is_invite_link?
redemption_count >= max_redemptions_allowed
Expand All @@ -109,7 +113,7 @@ def domain_matches?(email)

def can_be_redeemed_by?(user)
return false if !self.redeemable?
return true if self.email.blank? && self.domain.blank?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with the code here, but isn't the blank email/domain use case separate from the already redeemed one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good question -- I've replaced this because the previous meaning of this was "if it's an invite link that is not tied to an email or domain, then of course the user can redeem". With the fix in this PR that is not the case, if the user has already redeemed an invite at least once then they can never redeem it again. So that InvitedUser check trumps all.

return false if redeemed_by_user?(user)
return true if self.email.present? && email_matches?(user.email)
self.domain.present? && domain_matches?(user.email)
end
Expand Down
1 change: 0 additions & 1 deletion config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,6 @@ en:
name_label: "Name"
password_label: "Password"
existing_user_can_redeem: "Redeem your invitation to a topic or group."
existing_user_cannot_redeem: "This invitation cannot be redeemed. Please ask the person who invited you to send you a new invitation."

password_reset:
continue: "Continue to %{site_name}"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ en:
<p>Otherwise please <a href="%{base_url}/password-reset">Reset Password</a>.</p>
not_found_template_link: |
<p>This invitation to <a href="%{base_url}">%{site_name}</a> can no longer be redeemed. Please ask the person who invited you to send you a new invitation.</p>
existing_user_cannot_redeem: "This invitation cannot be redeemed. Please ask the person who invited you to send you a new invitation."
existing_user_already_redemeed: "You have already redeemed this invite link."
user_exists: "There's no need to invite <b>%{email}</b>, they already have an account!"
invite_exists: "You already invited <b>%{email}</b>."
invalid_email: "%{email} isn't a valid email address."
Expand Down
17 changes: 17 additions & 0 deletions spec/requests/invites_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
json = JSON.parse(element.current_scope.attribute('data-preloaded').value)
invite_info = JSON.parse(json['invite_info'])
expect(invite_info['existing_user_can_redeem']).to eq(false)
expect(invite_info['existing_user_can_redeem_error']).to eq(I18n.t("invite.existing_user_cannot_redeem"))
end
end

Expand All @@ -141,6 +142,22 @@
expect(invite_info['existing_user_can_redeem']).to eq(false)
end
end

it "does not allow the user to accept the invite when a multi-use invite link has already been redeemed by the user" do
invite.update!(email: nil, max_redemptions_allowed: 10)
expect(invite.redeem(redeeming_user: user)).not_to eq(nil)

get "/invites/#{invite.invite_key}"
expect(response.status).to eq(200)

expect(response.body).to have_tag('div#data-preloaded') do |element|
json = JSON.parse(element.current_scope.attribute('data-preloaded').value)
invite_info = JSON.parse(json['invite_info'])
expect(invite_info['existing_user_id']).to eq(user.id)
expect(invite_info['existing_user_can_redeem']).to eq(false)
expect(invite_info['existing_user_can_redeem_error']).to eq(I18n.t("invite.existing_user_already_redemeed"))
end
end
end

it 'fails if invite does not exist' do
Expand Down