FIX: Hide 'show associated accounts' button if moderators can't see emails

FIX: Hide ‘show associated accounts’ button if moderators can’t see emails

And minor prettier code alignments

diff --git a/app/assets/javascripts/discourse/controllers/preferences/account.js.es6 b/app/assets/javascripts/discourse/controllers/preferences/account.js.es6
index 1bd1beb..495d119 100644
--- a/app/assets/javascripts/discourse/controllers/preferences/account.js.es6
+++ b/app/assets/javascripts/discourse/controllers/preferences/account.js.es6
@@ -92,9 +92,9 @@ export default Ember.Controller.extend(
       return userId !== this.get("currentUser.id");
     },
 
-    @computed("model.second_factor_enabled")
-    canUpdateAssociatedAccounts(secondFactorEnabled) {
-      if (secondFactorEnabled) {
+    @computed("model.second_factor_enabled", "CanCheckEmails")
+    canUpdateAssociatedAccounts(secondFactorEnabled, canCheckEmails) {
+      if (secondFactorEnabled || !canCheckEmails) {
         return false;
       }
 
@@ -106,8 +106,13 @@ export default Ember.Controller.extend(
 
     @computed("showAllAuthTokens", "model.user_auth_tokens")
     authTokens(showAllAuthTokens, tokens) {
-      tokens.sort((a, b) =>
-        a.is_active ? -1 : b.is_active ? 1 : b.seen_at.localeCompare(a.seen_at)
+      tokens.sort(
+        (a, b) =>
+          a.is_active
+            ? -1
+            : b.is_active
+              ? 1
+              : b.seen_at.localeCompare(a.seen_at)
       );
 
       return showAllAuthTokens
diff --git a/test/javascripts/controllers/preferences-account-test.js.es6 b/test/javascripts/controllers/preferences-account-test.js.es6
index ed5044e..59216a7 100644
--- a/test/javascripts/controllers/preferences-account-test.js.es6
+++ b/test/javascripts/controllers/preferences-account-test.js.es6
@@ -17,5 +17,9 @@ QUnit.test("updating of associated accounts", function(assert) {
 
   controller.set("model.second_factor_enabled", false);
 
+  assert.equal(controller.get("canUpdateAssociatedAccounts"), false);
+
+  controller.set("CanCheckEmails", true);
+
   assert.equal(controller.get("canUpdateAssociatedAccounts"), true);
 });

GitHub sha: ed012017

This is really hard to read. Can you change it so it uses if statements instead?

This commit has been mentioned on Discourse Meta. There might be relevant details there:

@jjaffeux fixed the prettier in this commit to get back to the original code formatting. Do you think still we need if statement instead?

Yeah, nested ternaries are very hard to read.

2 Likes

It is now replaced with the if condition.

2 Likes