diff --git a/docs/automatic-configuration.md b/docs/automatic-configuration.md index ec5be01..78941db 100644 --- a/docs/automatic-configuration.md +++ b/docs/automatic-configuration.md @@ -26,6 +26,18 @@ For a variable to appear in the configuration form, it MUST be stored in a layer
List of variables that will be displayed in the configuration screen
+## Protecting the configuration screen + +By default, the configuration screen will be accessible to anyone. You will probably want to restrict the access of the +configuration screen to users that have a certain *tag*. + +To do this, simply add a `tag` property to the configuration layer. The value of the property is the name of the tag +that users must have to access the configuration screen. + +
+ +
Here, only users with tag "admin" will have access to the configuration screen
+
## Altering the display of a variable diff --git a/docs/images/configuration_tag.png b/docs/images/configuration_tag.png new file mode 100644 index 0000000..3779cd8 Binary files /dev/null and b/docs/images/configuration_tag.png differ diff --git a/src/Features/configuration.ts b/src/Features/configuration.ts index 68a89bc..ebc743a 100644 --- a/src/Features/configuration.ts +++ b/src/Features/configuration.ts @@ -1,9 +1,22 @@ +import {getLayersMap} from "../LayersFlattener"; +import {Properties} from "../Properties"; + /** * Initialize the configuration button in the menu */ -export function initConfiguration(assetsUrl?: string | undefined): void { - WA.ui.registerMenuCommand("Configure the room", () => { - assetsUrl = assetsUrl ?? process.env.ASSETS_URL ?? ""; - WA.nav.openCoWebSite(assetsUrl + "configuration.html", true); - }); +export async function initConfiguration(assetsUrl?: string | undefined): Promise { + const layers = await getLayersMap(); + + const configurationLayer = layers.get("configuration"); + + if (configurationLayer) { + const properties = new Properties(configurationLayer.properties); + const tag = properties.getString('tag'); + if (!tag || WA.player.tags.includes(tag)) { + WA.ui.registerMenuCommand("Configure the room", () => { + assetsUrl = assetsUrl ?? process.env.ASSETS_URL ?? ""; + WA.nav.openCoWebSite(assetsUrl + "configuration.html", true); + }); + } + } } diff --git a/src/Iframes/Configuration/index.ts b/src/Iframes/Configuration/index.ts index 1be051d..c0c4694 100644 --- a/src/Iframes/Configuration/index.ts +++ b/src/Iframes/Configuration/index.ts @@ -7,7 +7,6 @@ import { getLayersMap } from "../../LayersFlattener"; (async () => { const layers = await getLayersMap(); - console.log("LAYERS", layers); const configurationLayer = layers.get("configuration"); if (configurationLayer === undefined) { diff --git a/src/LayersFlattener.ts b/src/LayersFlattener.ts index 6fccb00..8ac7611 100644 --- a/src/LayersFlattener.ts +++ b/src/LayersFlattener.ts @@ -1,11 +1,20 @@ import type { ITiledMap, ITiledMapLayer } from "@workadventure/tiled-map-type-guard"; +let layersMapPromise: Promise>|undefined = undefined; + /** * Returns a map of all layers in a uni-dimensional map. * Layers are renamed: if they are in a group layer, the name of the group layer is prepended with a "/" as a separator. * Layers are indexed by name. */ export async function getLayersMap(): Promise> { + if (layersMapPromise === undefined) { + layersMapPromise = getLayersMapWithoutCache(); + } + return layersMapPromise; +} + +async function getLayersMapWithoutCache(): Promise> { return flattenGroupLayersMap(await WA.room.getTiledMap()); } diff --git a/src/bootstrap.ts b/src/bootstrap.ts index f96c0ca..b110486 100644 --- a/src/bootstrap.ts +++ b/src/bootstrap.ts @@ -9,7 +9,7 @@ import {initSpecialProperties} from "./Features/special_properties"; WA.onInit().then(() => { initDoors().catch((e) => console.error(e)); initSpecialProperties().catch((e) => console.error(e)); - initConfiguration(); + initConfiguration().catch((e) => console.error(e)); initPropertiesTemplates().catch((e) => console.error(e)); });