Skip to content

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

FEATURE: Add settings for connect/revoke capability in Login with Amazon (#35387)

Chris Alberti56aef84ac4d8
diff --git a/plugins/discourse-login-with-amazon/config/locales/server.en.yml b/plugins/discourse-login-with-amazon/config/locales/server.en.yml
index e10689a1cb0..1e8f22770e4 100644
--- a/plugins/discourse-login-with-amazon/config/locales/server.en.yml
+++ b/plugins/discourse-login-with-amazon/config/locales/server.en.yml
@@ -5,6 +5,8 @@ en:
login_with_amazon_client_secret: "Login with Amazon client secret"
login_with_amazon_scope: "Login with Amazon OAuth2 scopes (space separated)"
login_with_amazon_overrides_email: "Overrides the email address on the Discourse user account."
+ login_with_amazon_user_can_connect: "Allow existing users to connect their Amazon account in preferences."
+ login_with_amazon_user_can_revoke: "Allow users to revoke their associated Amazon account in preferences."
errors:
login_with_amazon_client_id_is_blank: "You must set 'login with amazon client id' before enabling this setting."
diff --git a/plugins/discourse-login-with-amazon/config/settings.yml b/plugins/discourse-login-with-amazon/config/settings.yml
index fef58e11d61..f209ebf3d52 100644
--- a/plugins/discourse-login-with-amazon/config/settings.yml
+++ b/plugins/discourse-login-with-amazon/config/settings.yml
@@ -21,3 +21,9 @@ plugins:
hidden: true
default: true
area: "authenticators"
+ login_with_amazon_user_can_connect:
+ default: true
+ area: "authenticators"
+ login_with_amazon_user_can_revoke:
+ default: true
+ area: "authenticators"
diff --git a/plugins/discourse-login-with-amazon/lib/auth/login_with_amazon_authenticator.rb b/plugins/discourse-login-with-amazon/lib/auth/login_with_amazon_authenticator.rb
index ad0ff2abc39..60bb5fdceb7 100644
--- a/plugins/discourse-login-with-amazon/lib/auth/login_with_amazon_authenticator.rb
+++ b/plugins/discourse-login-with-amazon/lib/auth/login_with_amazon_authenticator.rb
@@ -29,4 +29,12 @@ class Auth::LoginWithAmazonAuthenticator < ::Auth::ManagedAuthenticator
def primary_email_verified?(auth)
SiteSetting.login_with_amazon_email_verified
end
+
+ def can_connect_existing_user?
+ SiteSetting.login_with_amazon_user_can_connect
+ end
+
+ def can_revoke?
+ SiteSetting.login_with_amazon_user_can_revoke
+ end
end
diff --git a/plugins/discourse-login-with-amazon/spec/system/associated_account_prefs_spec.rb b/plugins/discourse-login-with-amazon/spec/system/associated_account_prefs_spec.rb
new file mode 100644
index 00000000000..762f7842401
--- /dev/null
+++ b/plugins/discourse-login-with-amazon/spec/system/associated_account_prefs_spec.rb
@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+RSpec.describe "Amazon Associated Account Preferences", type: :system do
+ fab!(:user)
+
+ let!(:user_account_preferences_page) { PageObjects::Pages::UserPreferencesAccount.new }
+
+ before do
+ SiteSetting.login_with_amazon_client_id = "somekey"
+ SiteSetting.login_with_amazon_client_secret = "somesecretkey"
+ enable_current_plugin
+ end
+
+ describe "with user connect enabled" do
+ before { SiteSetting.login_with_amazon_user_can_connect = true }
+
+ it "shows the connect button" do
+ sign_in(user)
+ user_account_preferences_page.visit(user)
+ expect(page).to have_css(".associated-accounts .amazon")
+ expect(page).to have_css(".associated-accounts .amazon .associated-account__actions button")
+ expect(find(".associated-accounts .amazon .associated-account__actions button")).to have_text(
+ I18n.t("js.user.associated_accounts.connect"),
+ )
+ end
+ end
+
+ describe "with user connect disabled" do
+ before { SiteSetting.login_with_amazon_user_can_connect = false }
+
+ it "does not show the connect button" do
+ sign_in(user)
+ user_account_preferences_page.visit(user)
+ expect(page).not_to have_css(".associated-accounts .amazon")
+ end
+ end
+
+ describe "with already associated account" do
+ fab!(:user_associated_account) do
+ UserAssociatedAccount.create!(provider_name: "amazon", provider_uid: "1234", user_id: user.id)
+ end
+
+ describe "with user revoke enabled" do
+ before { SiteSetting.login_with_amazon_user_can_revoke = true }
+ it "shows the revoke button" do
+ sign_in(user)
+ user_account_preferences_page.visit(user)
+ expect(page).to have_css(".associated-accounts .amazon")
+ expect(page).to have_css(".associated-accounts .amazon .associated-account__actions button")
+ expect(page).to have_css(
+ ".associated-accounts .amazon .associated-account__actions button svg.d-icon-trash-can",
+ )
+ end
+ end
+
+ describe "with user revoke disabled" do
+ before { SiteSetting.login_with_amazon_user_can_revoke = false }
+ it "shows the revoke button" do
+ sign_in(user)
+ user_account_preferences_page.visit(user)
+ expect(page).to have_css(".associated-accounts .amazon")
+ expect(page).not_to have_css(
+ ".associated-accounts .amazon .associated-account__actions button",
+ )
+ end
+ end
+ end
+end