From 9969631cef89290b00edcf236df1ddd8fc756bdd Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Wed, 7 Jul 2021 19:42:42 +0300 Subject: [PATCH] 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. --- app/controllers/invites_controller.rb | 28 ++++++++++++++++- spec/requests/invites_controller_spec.rb | 39 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb index acd5b49f4fd..5d84267d841 100644 --- a/app/controllers/invites_controller.rb +++ b/app/controllers/invites_controller.rb @@ -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 diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rb index 6d74c455e0c..972715b6449 100644 --- a/spec/requests/invites_controller_spec.rb +++ b/spec/requests/invites_controller_spec.rb @@ -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))