docusaurus-roles-plugin 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ import DocSidebar, { type Props } from "@theme-init/DocSidebar";
2
+ import { getRoleFrontMatter } from "../../options";
3
+ import { isAllowed, useRoles } from "../../runtime";
4
+ import type {
5
+ PropSidebarItem,
6
+ PropSidebarItemCategory,
7
+ } from "@docusaurus/plugin-content-docs";
8
+
9
+ const allowedOnly = (userRoles: string[]) => {
10
+ return (item: PropSidebarItem) => {
11
+ const frontMatter = getRoleFrontMatter(item.customProps);
12
+ const category = item as PropSidebarItemCategory;
13
+ if (category.items) {
14
+ category.items = category.items.filter(allowedOnly(userRoles));
15
+ }
16
+ const { allowed } = isAllowed(
17
+ userRoles,
18
+ frontMatter.required_roles ?? [],
19
+ frontMatter.required_roles_mode ?? "all",
20
+ );
21
+ return allowed;
22
+ };
23
+ };
24
+
25
+ export default function RolesDocSidebar({ sidebar, ...props }: Props) {
26
+ const { roles } = useRoles();
27
+ const filteredSidebar = sidebar.filter(allowedOnly(roles));
28
+ return <DocSidebar {...props} sidebar={filteredSidebar} />;
29
+ }
@@ -0,0 +1,16 @@
1
+ import NavbarItem, { Props } from "@theme-init/NavbarItem";
2
+ import { useRoles, isAllowed } from "docusaurus-roles-plugin/runtime";
3
+ import { RoleRequirements } from "../../options";
4
+
5
+ export default function RolesNavbarItem({
6
+ requiredRoles = [],
7
+ requiredRolesMode = "all",
8
+ ...props
9
+ }: Props & Partial<RoleRequirements>) {
10
+ const { roles } = useRoles();
11
+
12
+ const { allowed } = isAllowed(roles, requiredRoles, requiredRolesMode);
13
+ if (!allowed) return;
14
+
15
+ return <NavbarItem {...props} />;
16
+ }
@@ -0,0 +1,53 @@
1
+ import React from "react";
2
+ import { Redirect } from "@docusaurus/router";
3
+ import { usePluginData } from "@docusaurus/useGlobalData";
4
+ import {
5
+ useRoles,
6
+ isAllowed,
7
+ type RoleMode,
8
+ } from "docusaurus-roles-plugin/runtime";
9
+ import RootWarning from "./RootWarning";
10
+ import { PluginOptions } from "../options";
11
+
12
+ type Props = {
13
+ requiredRoles?: string[];
14
+ mode?: RoleMode;
15
+ children: React.ReactNode;
16
+ forbidden: React.ElementType<{ missingRoles: string[] }>;
17
+ };
18
+
19
+ export function getPluginConfig() {
20
+ return usePluginData("docusaurus-roles-plugin") as PluginOptions;
21
+ }
22
+
23
+ export default function RoleGate({
24
+ requiredRoles,
25
+ mode,
26
+ children,
27
+ forbidden: Forbidden,
28
+ }: Props) {
29
+ const pluginConfig = getPluginConfig();
30
+ const { roles, loading, hasProvider } = useRoles();
31
+
32
+ if (loading) {
33
+ return <>loading...</>;
34
+ }
35
+
36
+ const required = requiredRoles ?? [];
37
+ if (required.length === 0) return children;
38
+
39
+ const effectiveMode: RoleMode = mode ?? "any";
40
+
41
+ if (process.env.NODE_ENV !== "production" && !hasProvider) {
42
+ return <RootWarning />;
43
+ }
44
+
45
+ const { allowed, missing } = isAllowed(roles, required, effectiveMode);
46
+ if (allowed) return children;
47
+
48
+ if (pluginConfig?.unauthorizedBehavior === "redirect") {
49
+ return <Redirect to={pluginConfig?.redirectTo ?? "/access-denied"} />;
50
+ }
51
+
52
+ return <Forbidden missingRoles={missing} />;
53
+ }
@@ -0,0 +1,18 @@
1
+ import React from "react";
2
+
3
+ export default function RootWarning() {
4
+ return (
5
+ <div style={{ padding: 16 }}>
6
+ <h2>RolesProvider is missing</h2>
7
+ <p>
8
+ You installed <code>docusaurus-plugin-roles</code> but did not wrap your
9
+ site Root with
10
+ <code> RolesProvider</code>.
11
+ </p>
12
+ <p>
13
+ Swizzle Root and wrap it with <code>RolesProvider</code> from{" "}
14
+ <code>docusaurus-plugin-roles/runtime</code>.
15
+ </p>
16
+ </div>
17
+ );
18
+ }