A platform for community discussion. Free, open, simple.
FIX: Treat an unknown trust level as failing block user conditions (#44334)
Sérgio Saquetim093c417621a0
diff --git a/frontend/discourse/app/blocks/conditions/user.ts b/frontend/discourse/app/blocks/conditions/user.tsindex ae5b971dba9..606a602eb1c 100644--- a/frontend/discourse/app/blocks/conditions/user.ts+++ b/frontend/discourse/app/blocks/conditions/user.ts@@ -40,7 +40,7 @@ interface UserLike {admin?: boolean;moderator?: boolean;staff?: boolean;- trust_level?: number;+ trust_level?: number | null;groups?: Array<{ name?: string }>;}@@ -276,11 +276,18 @@ export default class BlockUserCondition extends BlockCondition {return false;}- // Check trust level range- if (minTrustLevel !== undefined && user.trust_level < minTrustLevel) {+ // Check trust level range. An unknown level satisfies no bound, like a missing user.+ const trustLevel = user.trust_level;+ if (+ minTrustLevel !== undefined &&+ (trustLevel == null || trustLevel < minTrustLevel)+ ) {return false;}- if (maxTrustLevel !== undefined && user.trust_level > maxTrustLevel) {+ if (+ maxTrustLevel !== undefined &&+ (trustLevel == null || trustLevel > maxTrustLevel)+ ) {return false;}diff --git a/frontend/discourse/tests/unit/blocks/conditions/user-test.js b/frontend/discourse/tests/unit/blocks/conditions/user-test.jsindex 24538c2c42b..2a68a4dae4c 100644--- a/frontend/discourse/tests/unit/blocks/conditions/user-test.js+++ b/frontend/discourse/tests/unit/blocks/conditions/user-test.js@@ -352,6 +352,34 @@ module("Unit | Blocks | Condition | user", function (hooks) {this.condition.evaluate({ minTrustLevel: 3, maxTrustLevel: 4 }));});++ // The loosest bounds (0 and 4) also catch an unknown level being treated as some+ // real level, such as TL0.+ test("an unknown trust level (undefined) satisfies no bound", function (assert) {+ this.condition.currentUser.trust_level = undefined;++ assert.false(+ this.condition.evaluate({ minTrustLevel: 0 }),+ "fails the loosest minimum"+ );+ assert.false(+ this.condition.evaluate({ maxTrustLevel: 4 }),+ "fails the loosest maximum"+ );+ });++ test("an unknown trust level (null) satisfies no bound", function (assert) {+ this.condition.currentUser.trust_level = null;++ assert.false(+ this.condition.evaluate({ minTrustLevel: 0 }),+ "fails the loosest minimum"+ );+ assert.false(+ this.condition.evaluate({ maxTrustLevel: 4 }),+ "fails the loosest maximum"+ );+ });});module("group conditions", function () {