From e27af6ee1a57485f70f0b6ab4f8c37f7bac76c6e Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Mon, 5 Sep 2016 00:34:31 +0200 Subject: [PATCH] Redirect logged in users to inviters page when following an invitation link closes #7061 --- Changelog.md | 1 + .../invitation_codes_controller.rb | 11 +++++--- config/locales/diaspora/en.yml | 1 + .../invitation_codes_controller_spec.rb | 27 +++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 spec/controllers/invitation_codes_controller_spec.rb diff --git a/Changelog.md b/Changelog.md index 162cf51d1..50732375d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -17,6 +17,7 @@ * Deleted comments will be removed when loading more comments [#7045](https://github.com/diaspora/diaspora/pull/7045) * The "subscribe" indicator on a post now gets toggled when you like or rehsare a post [#7040](https://github.com/diaspora/diaspora/pull/7040) * Add OpenGraph video support [#7043](https://github.com/diaspora/diaspora/pull/7043) +* You'll now get redirected to the invites page if you follow an invitation but you're already logged inĀ [#7061](https://github.com/diaspora/diaspora/pull/7061) # 0.6.0.0 diff --git a/app/controllers/invitation_codes_controller.rb b/app/controllers/invitation_codes_controller.rb index cefac3b5b..0f4302953 100644 --- a/app/controllers/invitation_codes_controller.rb +++ b/app/controllers/invitation_codes_controller.rb @@ -5,9 +5,14 @@ class InvitationCodesController < ApplicationController redirect_to root_url, :notice => I18n.t('invitation_codes.not_valid') end - def show - sign_out(current_user) if user_signed_in? - redirect_to new_user_registration_path(:invite => {:token => params[:id]}) + def show + if user_signed_in? + invite = InvitationCode.find_by_token!(params[:id]) + flash[:notice] = I18n.t("invitation_codes.already_logged_in", inviter: invite.user.name) + redirect_to person_path(invite.user.person) + else + redirect_to new_user_registration_path(invite: {token: params[:id]}) + end end private diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 62d697149..eab3953a0 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -553,6 +553,7 @@ en: invitation_codes: not_valid: "That invite code is no longer valid" + already_logged_in: "You have been invited by %{inviter} to join this pod but you are already logged in." invitations: create: diff --git a/spec/controllers/invitation_codes_controller_spec.rb b/spec/controllers/invitation_codes_controller_spec.rb new file mode 100644 index 000000000..36d72f13b --- /dev/null +++ b/spec/controllers/invitation_codes_controller_spec.rb @@ -0,0 +1,27 @@ +require "spec_helper" + +describe InvitationCodesController, type: :controller do + describe "#show" do + it "redirects to the root page if the invitation code is invalid" do + get :show, id: "InvalidInvitationCode" + expect(response).to redirect_to root_path + expect(flash[:notice]).to eq(I18n.t("invitation_codes.not_valid")) + end + + context "valid invitation code" do + let(:invitation_token) { alice.invitation_code.token } + + it "redirects logged out users to the sign in page" do + post :show, id: invitation_token + expect(response).to redirect_to new_user_registration_path(invite: {token: invitation_token}) + end + + it "redirects logged in users the the inviters page" do + sign_in bob + post :show, id: invitation_token + expect(response).to redirect_to person_path(alice.person) + expect(flash[:notice]).to eq(I18n.t("invitation_codes.already_logged_in", inviter: alice.name)) + end + end + end +end