Skip to content

A platform for community discussion. Free, open, simple.

FIX: invites to private topic when using discourse connect (#35303)

Régis Hanol9e3caa7282b6
diff --git a/app/assets/javascripts/discourse/app/controllers/invites/show.js b/app/assets/javascripts/discourse/app/controllers/invites/show.js
index b9113b72e06..aa503eefdde 100644
--- a/app/assets/javascripts/discourse/app/controllers/invites/show.js
+++ b/app/assets/javascripts/discourse/app/controllers/invites/show.js
@@ -133,11 +133,21 @@ export default class InvitesShowController extends Controller {
externalAuthsOnly() {
return (
!this.siteSettings.enable_local_logins &&
- this.externalAuthsEnabled &&
- !this.siteSettings.enable_discourse_connect
+ !this.siteSettings.enable_discourse_connect &&
+ this.externalAuthsEnabled
);
}
+ @discourseComputed("existingUserId")
+ showWelcomeHeader(existingUserId) {
+ return !existingUserId;
+ }
+
+ @discourseComputed("externalAuthsOnly", "discourseConnectEnabled")
+ showSignupProgressBar(externalAuthsOnly, discourseConnectEnabled) {
+ return !(externalAuthsOnly || discourseConnectEnabled);
+ }
+
@discourseComputed(
"emailValidation.failed",
"usernameValidation.failed",
diff --git a/app/assets/javascripts/discourse/app/templates/invites/show.gjs b/app/assets/javascripts/discourse/app/templates/invites/show.gjs
index 79e71527ab3..cb9ddaad079 100644
--- a/app/assets/javascripts/discourse/app/templates/invites/show.gjs
+++ b/app/assets/javascripts/discourse/app/templates/invites/show.gjs
@@ -2,7 +2,7 @@ import { Input } from "@ember/component";
import { on } from "@ember/modifier";
import { htmlSafe } from "@ember/template";
import RouteTemplate from "ember-route-template";
-import { and, not, or } from "truth-helpers";
+import { and, not } from "truth-helpers";
import DButton from "discourse/components/d-button";
import FullnameInput from "discourse/components/fullname-input";
import InputTip from "discourse/components/input-tip";
@@ -28,14 +28,14 @@ export default RouteTemplate(
{{hideApplicationSidebar}}
<section>
<div class="container invites-show clearfix">
- {{#unless
- (or @controller.externalAuthsOnly @controller.existingUserId)
- }}
- <SignupProgressBar
- @step={{if @controller.successMessage "activate" "signup"}}
- />
- {{/unless}}
- <WelcomeHeader @header={{@controller.welcomeTitle}} />
+ {{#if @controller.showWelcomeHeader}}
+ {{#if @controller.showSignupProgressBar}}
+ <SignupProgressBar
+ @step={{if @controller.successMessage "activate" "signup"}}
+ />
+ <WelcomeHeader @header={{@controller.welcomeTitle}} />
+ {{/if}}
+ {{/if}}
<div
class={{if @controller.successMessage "invite-success" "invite-form"}}
@@ -257,21 +257,24 @@ export default RouteTemplate(
{{/if}}
</form>
{{/if}}
- {{#if @controller.existingUserRedeeming}}
- {{#if @controller.existingUserCanRedeem}}
- <DButton
- @action={{@controller.submit}}
- @disabled={{@controller.submitDisabled}}
- @label="invites.accept_invite"
- type="submit"
- class="btn-primary"
- />
- {{else}}
- <div
- class="alert alert-error"
- >{{@controller.existingUserCanRedeemError}}</div>
+ {{! When using DiscourseConnect, all invite acceptance has to go through the SSO flow }}
+ {{#unless @controller.discourseConnectEnabled}}
+ {{#if @controller.existingUserRedeeming}}
+ {{#if @controller.existingUserCanRedeem}}
+ <DButton
+ @action={{@controller.submit}}
+ @disabled={{@controller.submitDisabled}}
+ @label="invites.accept_invite"
+ type="submit"
+ class="btn-primary accept-invitation"
+ />
+ {{else}}
+ <div
+ class="alert alert-error"
+ >{{@controller.existingUserCanRedeemError}}</div>
+ {{/if}}
{{/if}}
- {{/if}}
+ {{/unless}}
{{/if}}
</div>
</div>
diff --git a/app/assets/javascripts/discourse/tests/acceptance/invite-accept-test.js b/app/assets/javascripts/discourse/tests/acceptance/invite-accept-test.js
index ae260fa7d52..e20941703ce 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/invite-accept-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/invite-accept-test.js
@@ -25,6 +25,7 @@ function preloadInvite({
email_verified_by_link = false,
different_external_email = false,
hidden_email = false,
+ existing_user = false,
} = {}) {
const info = {
invited_by: {
@@ -48,6 +49,11 @@ function preloadInvite({
info.is_invite_link = false;
}
+ if (existing_user) {
+ info.existing_user_id = 456;
+ info.existing_user_can_redeem = true;
+ }
+
PreloadStore.store("invite_info", info);
}
@@ -242,6 +248,29 @@ acceptance(
"does not show the email message with the prefilled email"
);
assert.dom(".discourse-connect").exists("shows the Continue button");
+ assert
+ .dom(".accept-invitation")
+ .doesNotExist("does not show the Accept Invite button");
+ });
+
+ test("invite link (as logged in user)", async function (assert) {
+ preloadInvite({ link: true, existing_user: true });
+
+ await visit("/invites/my-valid-invite-token");
+
+ assert
+ .dom(".btn-social.facebook")
+ .doesNotExist("does not show Facebook login button");
+ assert.dom("form").doesNotExist("does not display the form");
+ assert
+ .dom(".email-message")
+ .doesNotExist(
+ "does not show the email message with the prefilled email"
+ );
+ assert.dom(".discourse-connect").exists("shows the Continue button");
+ assert
+ .dom(".accept-invitation")
+ .doesNotExist("does not show the Accept Invite button");
});
test("email invite link", async function (assert) {
@@ -258,6 +287,28 @@ acceptance(
.exists("shows the email message with the prefilled email");
assert.dom(".discourse-connect").exists("shows the Continue button");
assert.dom(".email-message").includesText("[email protected]");
+ assert
+ .dom(".accept-invitation")
+ .doesNotExist("does not show the Accept Invite button");
+ });
+
+ test("email invite link (as logged in user)", async function (assert) {
+ preloadInvite({ existing_user: true });
+
+ await visit("/invites/my-valid-invite-token");
+
+ assert
+ .dom(".btn-social.facebook")
+ .doesNotExist("does not show Facebook login button");
+ assert.dom("form").doesNotExist("does not display the form");
+ assert
+ .dom(".email-message")
+ .exists("shows the email message with the prefilled email");
+ assert.dom(".discourse-connect").exists("shows the Continue button");
+ assert.dom(".email-message").includesText("[email protected]");
+ assert
+ .dom(".accept-invitation")
+ .doesNotExist("does not show the Accept Invite button");
});
}
);
@@ -275,6 +326,9 @@ acceptance(
await visit("/invites/my-valid-invite-token");
assert.dom("form").doesNotExist("does not display the form");
+ assert
+ .dom(".accept-invitation")
+ .doesNotExist("does not show the Accept Invite button");
});
}
);
diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index 68f816965c0..41e9d79267d 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -329,9 +329,11 @@ class InvitesController < ApplicationController
render json: success_json
end
- # For DiscourseConnect SSO, all invite acceptance is done
- # via the SessionController#sso_login route
def perform_accept_invitation
+ # When DiscourseConnect is enabled, all invite acceptance is done
+ # via the SSO flow (SessionController#sso_login)
+ raise Discourse::NotFound if SiteSetting.enable_discourse_connect
+
params.require(:id)
params.permit(
:email,
@@ -344,8 +346,6 @@ class InvitesController < ApplicationController
},
)
- raise Discourse::NotFound if SiteSetting.enable_discourse_connect
-
invite = Invite.find_by(invite_key: params[:id])
redeeming_user = current_user
@@ -613,20 +613,20 @@ class InvitesController < ApplicationController
end
def ensure_invites_allowed
- if (
- !SiteSetting.enable_local_logins && Discourse.enabled_auth_providers.count == 0 &&
- !SiteSetting.enable_discourse_connect
- )
- raise Discourse::NotFound
- end
+ return if SiteSetting.enable_local_logins
+ return if SiteSetting.enable_discourse_connect
+ return if Discourse.enabled_auth_providers.present?
+
+ raise Discourse::NotFound
end
def ensure_new_registrations_allowed
- unless SiteSetting.allow_new_registrations
- flash[:error] = I18n.t("login.new_registrations_disabled")
- render layout: "no_ember"
- false
- end
+ return if SiteSetting.allow_new_registrations
+
+ flash[:error] = I18n.t("login.new_registrations_disabled")
+ render layout: "no_ember"
+
+ false
end
def groups_can_see_topic?(groups, topic)
diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb
index bd0192dcca0..e36aa9d993f 100644
--- a/app/controllers/session_controller.rb
+++ b/app/controllers/session_controller.rb
@@ -205,7 +205,7 @@ class SessionController < ApplicationController
sso.expire_nonce!
begin
- invite = validate_invitiation!(sso)
+ invite = validate_invitation!(sso)
if user = sso.lookup_or_create_user(request.remote_ip)
raise Discourse::ReadOnly if @staff_writes_only_mode && !user&.staff?
@@ -223,6 +223,7 @@ class SessionController < ApplicationController
else
render_sso_error(text: I18n.t("discourse_connect.account_not_approved"), status: 403)
end
+
return
# we only want to redeem the invite if
@@ -857,14 +858,14 @@ class SessionController < ApplicationController
# not want to complete the SSO process of creating a user
# and redeeming the invite if the invite is not redeemable or
# for the wrong user
- def validate_invitiation!(sso)
- invite_key = server_session["invite-key"]
- return if invite_key.blank?
+ def validate_invitation!(sso)
+ return unless invite_key = server_session["invite-key"]
- invite = Invite.find_by(invite_key: invite_key)
+ base_url = Discourse.base_url
+ site_name = SiteSetting.title
- if invite.blank?
- raise Invite::ValidationFailed.new(I18n.t("invite.not_found", base_url: Discourse.base_url))
+ unless invite = Invite.find_by(invite_key:)
+ raise Invite::ValidationFailed.new(I18n.t("invite.not_found", base_url:))
end
if invite.redeemable?
@@ -872,15 +873,9 @@ class SessionController < ApplicationController
raise Invite::ValidationFailed.new(I18n.t("invite.not_matching_email"))
end
elsif invite.expired?
- raise Invite::ValidationFailed.new(I18n.t("invite.expired", base_url: Discourse.base_url))
+ raise Invite::ValidationFailed.new(I18n.t("invite.expired", base_url:))
elsif invite.redeemed?
- raise Invite::ValidationFailed.new(
- I18n.t(
- "invite.not_found_template",
- site_name: SiteSetting.title,
- base_url: Discourse.base_url,
- ),
- )
+ raise Invite::ValidationFailed.new(I18n.t("invite.not_found_template", site_name:, base_url:))
end
invite
@@ -888,14 +883,15 @@ class SessionController < ApplicationController
def redeem_invitation(invite, sso, redeeming_user)
InviteRedeemer.new(
- invite: invite,
+ invite:,
username: sso.username,
name: sso.name,
ip_address: request.remote_ip,
session: server_session,
email: sso.email,
- redeeming_user: redeeming_user,
+ redeeming_user:,
).redeem
+
server_session.delete("invite-key")
# note - more specific errors are handled in the sso_login method