Skip to content

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

DEV: Docs for theme setting resolve_group_membership (#41537)

Martin Brennan244eb18292b5
diff --git a/docs/developer-guides/docs/03-code-internals/13-plugin-outlet-connectors.md b/docs/developer-guides/docs/03-code-internals/13-plugin-outlet-connectors.md
index 92c8a260f42..5c3acb1129d 100644
--- a/docs/developer-guides/docs/03-code-internals/13-plugin-outlet-connectors.md
+++ b/docs/developer-guides/docs/03-code-internals/13-plugin-outlet-connectors.md
@@ -143,7 +143,9 @@ export default class BrandOfficialTopics extends Component {
}
// ... (any other logic)
- <template>{{! ... }}</template>
+ <template>
+ {{! ... }}
+ </template>
}
```
diff --git a/docs/developer-guides/docs/05-themes-components/09-theme-settings.md b/docs/developer-guides/docs/05-themes-components/09-theme-settings.md
index f2040ec6421..b094dba5bc8 100644
--- a/docs/developer-guides/docs/05-themes-components/09-theme-settings.md
+++ b/docs/developer-guides/docs/05-themes-components/09-theme-settings.md
@@ -220,6 +220,8 @@ export default apiInitializer((api) => {
This `settings` object is also usable as normal within `.gjs` `<template>` tags.
+### Setting CSS variables
+
In CSS, you'll get a variable created for every setting of your theme and each variable will have the same name as the setting it represents.
So if you had a float setting called `global_font_size` and a string setting called `site_background`, you could do something like this in your theme CSS:
@@ -231,6 +233,37 @@ html {
}
```
+### Resolving group membership
+
+Theme components sometimes need to show or hide a feature based on whether the current user is in a configured group. Avoid checking `currentUser.groups` for this because it only includes groups that are visible to the user, and it can miss hidden groups.
+
+For group-backed list settings, add `resolve_group_membership: true` to resolve the check server-side:
+
+```yaml
+copy_button_allowed_groups:
+ default: "1|3"
+ type: list
+ list_type: group
+ resolve_group_membership: true
+```
+
+This option is only valid when the setting has `type: list` and `list_type: group`. When it is enabled, the frontend `settings` object does not include the original group list. Instead, Discourse adds a boolean with the same setting name prefixed by `user_in_`:
+
+```gjs
+// {theme}/javascripts/discourse/api-initializers/init-theme.gjs
+import { apiInitializer } from "discourse/lib/api";
+
+export default apiInitializer((api) => {
+ if (!settings.user_in_copy_button_allowed_groups) {
+ return;
+ }
+
+ // User is in at least one selected group.
+});
+```
+
+The generated boolean also works with automatic groups such as `logged_in_users` and `anonymous_users`.
+
## :link: Related Topics
- https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648
diff --git a/docs/developer-guides/docs/07-theme-developer-tutorial/06-js-api.md b/docs/developer-guides/docs/07-theme-developer-tutorial/06-js-api.md
index 831dd3e7325..7ff542f1478 100644
--- a/docs/developer-guides/docs/07-theme-developer-tutorial/06-js-api.md
+++ b/docs/developer-guides/docs/07-theme-developer-tutorial/06-js-api.md
@@ -12,6 +12,8 @@ In the last couple of chapters, we've explored how to use the JavaScript API to
`api.getCurrentUser()` will return information about the current user, or `null` if nobody is logged in. This can be used for all sorts of things, including per-group logic, or rendering a user's username into the UI.
+For group membership checks backed by theme settings, use the [`resolve_group_membership` option](../05-themes-components/09-theme-settings.md#resolving-group-membership) in your theme's `settings.yml` file instead of checking `currentUser.groups`. This avoids leaking or missing hidden group memberships.
+
```gjs
import { apiInitializer } from "discourse/lib/api";