特性机制与 CE 项目中的 EE 功能推广实现)
Strapi Admin 管理面板企业版EE特性机制与 CE 项目中的 EE 功能推广实现【免费下载链接】strapi Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.项目地址: https://gitcode.com/GitHub_Trending/st/strapi本文围绕 Strapi 官方文档《Admin Enterprise Edition》原文档展开结合当前仓库源码深入讲解 Strapi 管理面板中 Enterprise Edition企业版特性的整体架构以及社区版CE项目里 EE 功能自我推广机制的完整实现链路从window.strapi全局许可状态的初始化到设置菜单中推广条目的条件注入、licenseOnly标记渲染与权限过滤。读完后你将能够理解 Strapi 如何在同一份代码库中同时支撑 CE 与 EE 两种发行形态并在 CE 项目中安全地展示 EE 功能的购买入口。1. Admin Enterprise Edition 文档定位官方文档中Admin Enterprise Edition 章节是对管理面板所有企业版特性的总览入口原文通过 Docusaurus 的DocCardList渲染该分类下的全部子文档卡片即 SSO、审计日志、内容历史等各类 EE 特性的说明页。除此之外该文档还专门阐述了一个面向 Strapi 贡献者的工程约定每当 Strapi 新增一个 EE 特性时应当让它在 CE 项目的设置菜单中自动自我推广从而让社区版用户感知到企业版能力的存在。这个约定在当前仓库中有完整、可验证的实现。下面按许可状态来源 → 推广条件模式 → 菜单合并与权限过滤的顺序逐层解析。2. 许可状态的唯一事实来源window.strapiEE 推广逻辑的所有判断都依赖一个前端全局对象window.strapi。它的初始化与水合hydrate过程发生在管理面板的渲染入口 render.ts 中。2.1 默认值按无许可处理在 render.ts 中renderAdmin首先向window.strapi写入一组保守的默认值window.strapi { backendURL: createAbsoluteUrl(process.env.STRAPI_ADMIN_BACKEND_URL), isEE: false, isTrial: false, telemetryDisabled: process.env.STRAPI_TELEMETRY_DISABLED true, future: { isEnabled: (name) features?.future?.[name] true, }, features: { SSO: sso, AUDIT_LOGS: audit-logs, REVIEW_WORKFLOWS: review-workflows, /** * If we dont get the license then we know its not EE * so no feature is enabled. */ isEnabled: () false, }, projectType: Community, flags: { nps: false, promoteEE: true, docLinks: true, }, ai: { enabled: true }, };从源码结构看这里有两个关键设计特性名常量表SSO: sso、AUDIT_LOGS: audit-logs、REVIEW_WORKFLOWS: review-workflows把驼峰命名的常量与后端许可系统中使用的特性字符串绑定在一起。业务代码通过window.strapi.features.SSO取到字符串sso后再交给isEnabled判断避免在多处硬编码特性名。默认isEnabled: () false源码注释明确指出拿不到 license 就说明不是 EE因此没有任何特性被启用。也就是说在许可接口返回之前前端一律按社区版行为降级运行。其中flags.promoteEE默认值为true这正是第 3 节推广逻辑的总开关。2.2 通过/admin/project-type接口水合真实许可状态随后render.ts 会请求后端接口获取真实许可状态并覆盖默认值const { data: { data: { isEE, isTrial, features, flags, ai, planPriceId }, }, } await get{ data: ProjectType }(/admin/project-type); window.strapi.isEE isEE; window.strapi.isTrialLicense isTrial; window.strapi.flags flags; window.strapi.features { ...window.strapi.features, isEnabled: (featureName: string | undefined) features.some((feature) feature.name featureName), }; window.strapi.projectType getProjectType({ isEE, planPriceId });可以看到isEnabled被替换为一个真正的谓词判断后端返回的features列表中是否包含该特性名。若该请求失败源码中的注释给出了明确策略——简单地不激活任何 EE 特性console.error(err)后继续以默认值运行保证管理面板在许可服务异常时仍然可用。接口响应中的planPriceId用于区分 Growth 计划与其他企业计划源码注释写明它是 license registry 在 EE 中发送的licensed plan price idgetProjectType据此将projectType从Community更新为对应的 EE 计划类型。3. CE 项目中的 EE 功能推广条件注入模式文档给出的核心约定是每当新增一个 EE 特性就应在设置菜单中加入如下条件判断确保该特性在 CE 项目中自我推广原文示例指向设置菜单钩子其对应实现路径当前为 useSettingsMenu.ts而具体的推广条目集中在 constants.ts...(!window.strapi.features.isEnabled(window.strapi.features.NEW_EE_FEATURE) window.strapi?.flags?.promoteEE ? [ { intlLabel: { id: Settings.new-ee-feature.page.title, defaultMessage: NEW EE FEATURE, }, to: /settings/purchase-new-ee-feature, id: new-ee-feature, licenseOnly: true, }, ] : []),拆解这个模式它由三个条件与三个字段组成!window.strapi.features.isEnabled(...)仅当该 EE 特性未被许可时才注入推广条目。已购买该特性的用户会直接看到功能本体由 EE 链接提供菜单中不再出现购买页。window.strapi?.flags?.promoteEE总开关。constants.ts 中的源码注释明确说明在项目的./config/admin.js中加入promoteEE: false即可关闭推广行为即运营上不希望向用户展示 EE 购买入口的项目可整体禁用。licenseOnly: true标记该链接需要企业许可。前端菜单组件会据此渲染专属徽标——例如 SettingsNav.tsx 在渲染链接时对link?.licenseOnly做判断并附加闪电Lightning图标徽标MainNavLinks.tsx 中同样有link?.licenseOnly ? Lightning fillprimary600 / : undefined的逻辑让用户一眼识别出这是企业版功能。to: /settings/purchase-...推广条目的跳转目标是专门设置的购买页路由而非功能页本身。intlLabel与其他菜单项一致走 i18n 文案系统id用于翻译文件检索defaultMessage作为缺省文案。3.1 仓库中已落地的三个真实推广条目当前仓库的 constants.ts 中SETTINGS_LINKS_CE()函数完整实现了上述模式。以下按实际代码逐一说明。SSO单点登录——位于全局设置区global链接数组// If the Enterprise/Cloud feature is not enabled and if the config doesnt disable it, // we promote the Enterprise/Cloud feature by displaying them in the settings menu. // Disable this by adding promoteEE: false to your ./config/admin.js file ...(!window.strapi.features.isEnabled(window.strapi.features.SSO) window.strapi?.flags?.promoteEE ? [ { intlLabel: { id: Settings.sso.title, defaultMessage: Single Sign-On }, to: /settings/purchase-single-sign-on, id: sso-purchase-page, licenseOnly: true, }, ] : []),Content History内容历史——同样位于global区注意此处特性名直接使用字符串字面量cms-content-historyconstants.ts...(!window.strapi.features.isEnabled(cms-content-history) window.strapi?.flags?.promoteEE ? [ { intlLabel: { id: Settings.content-history.title, defaultMessage: Content History }, to: /settings/purchase-content-history, id: content-history-purchase-page, licenseOnly: true, }, ] : []),Audit Logs审计日志——位于管理面板设置区admin链接数组特性常量来自window.strapi.features.AUDIT_LOGS对应字符串audit-logs...(!window.strapi.features.isEnabled(window.strapi.features.AUDIT_LOGS) window.strapi?.flags?.promoteEE ? [ { intlLabel: { id: global.auditLogs, defaultMessage: Audit Logs }, to: /settings/purchase-audit-logs, id: auditLogs-purchase-page, licenseOnly: true, }, ] : []),这三个条目的结构完全一致id以*-purchase-page结尾、to指向/settings/purchase-*购买页路由、licenseOnly: true。这正是文档中NEW_EE_FEATURE模板的实例化也是后续新增 EE 特性时应当遵循的复制范式。3.2 类型层面的约束推广链接的 TypeScript 类型在 constants.ts 中定义export interface SettingsMenuLink extends OmitStrapiAppSettingLink, Component | permissions | licenseOnly { licenseOnly?: boolean; }而在 useSettingsMenu.ts 中来自 Strapi App注册机制的链接类型被显式约束为licenseOnly?: never——即**licenseOnly标记是 CE 侧推广条目的专属属性**插件/应用注册的链接不允许携带该标记。这种类型隔离从编译期保证了推广条目的语义不被外部扩展污染。4. CE 与 EE 菜单的合并useSettingsMenu钩子推广条目最终如何进入用户可见的菜单由设置页钩子 useSettingsMenu.ts 控制。4.1 CE 链接与 EE 链接的组合钩子内部以SETTINGS_LINKS_CE()为基线再通过useEnterprise钩子动态加载 EE 侧的链接常量useSettingsMenu.tsconst ceLinks React.useMemo(() SETTINGS_LINKS_CE(), []); const { admin: adminLinks, global: globalLinks } useEnterprise( ceLinks, async () (await import(../../../ee/admin/src/constants)).SETTINGS_LINKS_EE(), { combine(ceLinks, eeLinks) { return { admin: [...eeLinks.admin, ...ceLinks.admin], global: [...ceLinks.global, ...eeLinks.global], }; }, defaultValue: { admin: [], global: [] }, } );几个值得注意的细节EE 常量位于packages/core/admin/ee/admin/目录下与 CE 代码物理隔离EE 目录整体在 packages/core/admin/ee含admin与server两部分。这正是 Strapi CE/EE 分开发行的仓库组织方式构建社区版产物时不包含 EE 代码构建企业版产物时通过动态import注入。useEnterprise的第二个参数是异步工厂函数返回Promise因此 EE 代码可以按动态 chunk 的形式按需加载defaultValue保证在 EE 不可用时钩子不会阻塞。组合顺序上admin区 EE 链接排在 CE 链接之前global区 CE 链接排在 EE 链接之前——EE 与 CE 条目在最终菜单中是并列呈现、而非互相覆盖的。4.2 权限过滤与isDisplayed合并后的每个链接会被附加权限要求permissions.settings[link.id]然后在useEffect中通过checkUserHasPermission逐个异步判定当前用户是否拥有至少一项对应权限并据此为每个链接计算isDisplayeduseSettingsMenu.ts。最终返回时菜单会过滤掉未显示的链接return { isLoading, menu: menu.map((menuItem) ({ ...menuItem, links: menuItem.links.filter((link) link.isDisplayed), })), };此外钩子还会对后端下发的设置数据做防御性归一化normalizeSettings/normalizeSettingsLink丢弃缺少id、to、intlLabel等必备字段的非法链接——这与第 3.1 节中推广条目必须带id且intlLabel完整的约定相呼应钩子中的addPermissions在链接缺少id时甚至会直接抛出错误The settings menu item must have an id attribute.。5. 完整链路与新增 EE 特性的操作清单把各部分串起来一条 EE 功能在 CE 项目中的完整展示链路为启动renderAdmin写入window.strapi默认值isEE: false、isEnabled: () false、flags.promoteEE: true——render.ts水合请求/admin/project-type用后端返回的features列表替换isEnabled决定哪些 EE 特性已启用——render.ts注入推广条目SETTINGS_LINKS_CE()依据!isEnabled(feature) flags.promoteEE条件把购买页链接注入global/admin链接数组——constants.ts合并 CE/EE 链接useSettingsMenu通过useEnterprise动态加载SETTINGS_LINKS_EE并与 CE 链接组合——useSettingsMenu.ts权限过滤逐项检查权限未通过的链接被isDisplayed: false过滤掉渲染徽标设置导航对licenseOnly: true的链接附加闪电徽标指向/settings/purchase-*购买页——SettingsNav.tsx。对于 Strapi 贡献者新增一个 EE 特性时的落地清单也随之明确在window.strapi.features常量表中登记特性字符串如SSO: sso在SETTINGS_LINKS_CE()对应分区中按文档模板追加!isEnabled promoteEE ? [{ intlLabel, to, id, licenseOnly: true }] : []推广条目to指向新的/settings/purchase-feature购买页路由为intlLabel.id补充各语言的翻译条目确保该特性链接已纳入管理端权限映射permissions.settings[link.id]否则即使具备许可菜单项也会因无权限而被隐藏。6. 小结Strapi Admin 的 Enterprise Edition 体系建立在两条清晰的原则之上许可状态以后端接口水合 前端默认降级的方式集中承载于window.strapi而CE 项目中的 EE 推广则是一套可复制的条件注入模式——特性未启用且promoteEE开关允许时注入licenseOnly购买页链接由useSettingsMenu完成 CE/EE 合并与权限过滤最终由设置导航以徽标形式呈现。理解这条链路不仅能读懂现有 SSO、Content History、Audit Logs 三个推广条目的行为也为在 Strapi 中新增 EE 特性提供了从类型定义、菜单注入到权限映射的完整参照。【免费下载链接】strapi Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.项目地址: https://gitcode.com/GitHub_Trending/st/strapi创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考