From 1451f70b9ecebe201f48c051e842e34b4fd52a8e Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 24 Jun 2024 08:33:48 -0400 Subject: [PATCH] Changes plugin interface from 'roles' to 'enabled' method for more flexibility --- app/hooks/useSettingsConfig.ts | 16 ++++++++++------ app/utils/PluginManager.ts | 6 ++++-- plugins/matomo/client/index.tsx | 2 ++ plugins/matomo/plugin.json | 1 - plugins/slack/client/index.tsx | 3 +++ plugins/slack/plugin.json | 1 - 6 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index 7a5960a2b..4e3cf2ba8 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -188,17 +188,21 @@ const useSettingsConfig = () => { // Plugins PluginManager.getHooks(Hook.Settings).forEach((plugin) => { + const group = plugin.value.group ?? "Integrations"; const insertIndex = plugin.value.after ? items.findIndex((i) => i.name === t(plugin.value.after!)) + 1 - : items.findIndex( - (i) => i.group === t(plugin.value.group ?? "Integrations") - ); + : items.findIndex((i) => i.group === t(group)); items.splice(insertIndex, 0, { name: t(plugin.name), - path: integrationSettingsPath(plugin.id), - group: t(plugin.value.group), + path: + group === "Integrations" + ? integrationSettingsPath(plugin.id) + : settingsPath(plugin.id), + group: t(group), component: plugin.value.component, - enabled: plugin.roles?.includes(user.role) || can.update, + enabled: plugin.value.enabled + ? plugin.value.enabled(team, user) + : can.update, icon: plugin.value.icon, } as ConfigItem); }); diff --git a/app/utils/PluginManager.ts b/app/utils/PluginManager.ts index ead87ad92..87a9a76ce 100644 --- a/app/utils/PluginManager.ts +++ b/app/utils/PluginManager.ts @@ -1,6 +1,8 @@ import isArray from "lodash/isArray"; import sortBy from "lodash/sortBy"; import { action, observable } from "mobx"; +import Team from "~/models/Team"; +import User from "~/models/User"; import { useComputed } from "~/hooks/useComputed"; import Logger from "./Logger"; import isCloudHosted from "./isCloudHosted"; @@ -26,6 +28,8 @@ type PluginValueMap = { icon: React.ElementType; /** The settings screen somponent, should be lazy loaded. */ component: React.LazyExoticComponent; + /** Whether the plugin is enabled in the current context. */ + enabled?: (team: Team, user: User) => boolean; }; [Hook.Icon]: React.ElementType; }; @@ -45,8 +49,6 @@ export type Plugin = { priority?: number; /** The deployments this plugin is enabled for (default: all) */ deployments?: string[]; - /** The roles this plugin is enabled for. (default: admin) */ - roles?: string[]; }; /** diff --git a/plugins/matomo/client/index.tsx b/plugins/matomo/client/index.tsx index fbdc2d782..b32c436cf 100644 --- a/plugins/matomo/client/index.tsx +++ b/plugins/matomo/client/index.tsx @@ -1,4 +1,5 @@ import * as React from "react"; +import { UserRole } from "@shared/types"; import { Hook, PluginManager } from "~/utils/PluginManager"; import config from "../plugin.json"; import Icon from "./Icon"; @@ -11,6 +12,7 @@ PluginManager.add([ group: "Integrations", icon: Icon, component: React.lazy(() => import("./Settings")), + enabled: (_, user) => user.role === UserRole.Admin, }, }, ]); diff --git a/plugins/matomo/plugin.json b/plugins/matomo/plugin.json index 62bfa0a66..3fd3ea8c3 100644 --- a/plugins/matomo/plugin.json +++ b/plugins/matomo/plugin.json @@ -1,7 +1,6 @@ { "id": "matomo", "name": "Matomo", - "roles": ["admin"], "priority": 40, "description": "Adds support for reporting analytics to a Matomo server.", "deployments": ["community", "enterprise"] diff --git a/plugins/slack/client/index.tsx b/plugins/slack/client/index.tsx index 6f1797b65..27c1478fa 100644 --- a/plugins/slack/client/index.tsx +++ b/plugins/slack/client/index.tsx @@ -1,4 +1,5 @@ import * as React from "react"; +import { UserRole } from "@shared/types"; import { Hook, PluginManager } from "~/utils/PluginManager"; import config from "../plugin.json"; import Icon from "./Icon"; @@ -11,6 +12,8 @@ PluginManager.add([ group: "Integrations", icon: Icon, component: React.lazy(() => import("./Settings")), + enabled: (_, user) => + [UserRole.Member, UserRole.Admin].includes(user.role), }, }, { diff --git a/plugins/slack/plugin.json b/plugins/slack/plugin.json index 5c79e09e6..760a12b45 100644 --- a/plugins/slack/plugin.json +++ b/plugins/slack/plugin.json @@ -2,6 +2,5 @@ "id": "slack", "name": "Slack", "priority": 20, - "roles": ["admin", "member"], "description": "Adds a Slack authentication provider, support for the /outline slash command, and link unfurling." }