FEATURE: Make invites work with existing users (#13532)

* FEATURE: Redirect logged in user to invite topic

Users who were already logged in and were given an invite link to a
topic used to see an error message saying that they already have an
account and cannot redeem the invite. This commit amends that behavior
and redirects the user directly to the topic, if they can see it.

* FEATURE: Add logged in user to invite groups

Users who were already logged in and were given an invite link to a
group used to see an error message saying that they already have an
account and cannot redeem the invite. This commit amends that behavior
and adds the user to the group.
This commit is contained in:
Dan Ungureanu 2021-07-07 19:42:42 +03:00 committed by GitHub
parent dec7e19da3
commit 9969631cef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 1 deletions

View File

@ -12,7 +12,7 @@ class InvitesController < ApplicationController
before_action :ensure_invites_allowed, only: [:show, :perform_accept_invitation]
before_action :ensure_new_registrations_allowed, only: [:show, :perform_accept_invitation]
before_action :ensure_not_logged_in, only: [:show, :perform_accept_invitation]
before_action :ensure_not_logged_in, only: :perform_accept_invitation
def show
expires_now
@ -21,6 +21,32 @@ class InvitesController < ApplicationController
invite = Invite.find_by(invite_key: params[:id])
if invite.present? && invite.redeemable?
if current_user
added_to_group = false
if invite.groups.present?
invite_by_guardian = Guardian.new(invite.invited_by)
new_group_ids = invite.groups.pluck(:id) - current_user.group_users.pluck(:group_id)
new_group_ids.each do |id|
if group = Group.find_by(id: id)
if invite_by_guardian.can_edit_group?(group)
group.add(current_user)
added_to_group = true
end
end
end
end
if topic = invite.topics.first
new_guardian = Guardian.new(current_user)
return redirect_to(topic.url) if new_guardian.can_see?(topic)
elsif added_to_group
return redirect_to(path("/"))
end
return ensure_not_logged_in
end
email = Email.obfuscate(invite.email)
# Show email if the user already authenticated their email

View File

@ -57,6 +57,45 @@ describe InvitesController do
end
end
it 'adds logged in users to invite groups' do
group = Fabricate(:group)
group.add_owner(invite.invited_by)
InvitedGroup.create!(group: group, invite: invite)
sign_in(user)
get "/invites/#{invite.invite_key}"
expect(response).to redirect_to("/")
expect(user.reload.groups).to include(group)
end
it 'redirects logged in users to invite topic if they can see it' do
topic = Fabricate(:topic)
TopicInvite.create!(topic: topic, invite: invite)
sign_in(user)
get "/invites/#{invite.invite_key}"
expect(response).to redirect_to(topic.url)
end
it 'adds logged in user to group and redirects them to invite topic' do
group = Fabricate(:group)
group.add_owner(invite.invited_by)
secured_category = Fabricate(:category)
secured_category.permissions = { group.name => :full }
secured_category.save!
topic = Fabricate(:topic, category: secured_category)
TopicInvite.create!(invite: invite, topic: topic)
InvitedGroup.create!(invite: invite, group: group)
sign_in(user)
get "/invites/#{invite.invite_key}"
expect(user.reload.groups).to include(group)
expect(response).to redirect_to(topic.url)
end
it 'fails for logged in users' do
sign_in(Fabricate(:user))