-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathbrand.ts
More file actions
167 lines (151 loc) · 4.92 KB
/
Copy pathbrand.ts
File metadata and controls
167 lines (151 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/** Brand preference, SaaS policy context, and settings tab visibility. */
export type Brand = "oss" | "ibm";
export const IBM_THEME_DEV = process.env.NEXT_PUBLIC_IBM_THEME_DEV === "true";
/**
* Local dev: show the IBM COS connector in the Connectors tab / permission
* admin without IBM_AUTH_ENABLED. Mirrors the backend `OPENRAG_DEV_IBM_COS`
* bypass (see `enhancements/connectors/ibm_cos/connector.py`). Never enable
* in production.
*/
export const DEV_IBM_COS = process.env.NEXT_PUBLIC_DEV_IBM_COS === "true";
/** Default when no localStorage — must match `BrandProvider` initial state. */
export const DEFAULT_BRAND: Brand = IBM_THEME_DEV ? "ibm" : "oss";
/** Normalize stored brand value; unknown/missing uses `DEFAULT_BRAND`. */
export function resolveBrand(brand: Brand | string | undefined): Brand {
if (brand === "ibm" || brand === "oss") return brand;
return DEFAULT_BRAND;
}
/** IBM/SaaS UI styling from context brand only (see `BrandProvider`). */
export function isCloudBrand(brand: Brand | string | undefined): boolean {
if (!IBM_THEME_DEV) return resolveBrand(brand) === "ibm";
return resolveBrand(brand) !== "oss";
}
/**
* SaaS policy context (connector access tab, RBAC-gated settings).
* Production: IBM auth or backend `cloud_context`.
* Local: IBM theme dev + IBM brand mirrors SaaS settings UX (client brand only).
*/
export function isSaasPolicyContext({
isIbmAuthMode,
cloudContext = false,
brand,
/** RSC cannot read localStorage — skip dev brand toggle on server guards. */
skipDevBrand = false,
}: {
isIbmAuthMode: boolean;
cloudContext?: boolean;
brand?: Brand | string | undefined;
skipDevBrand?: boolean;
}): boolean {
if (isIbmAuthMode || cloudContext) return true;
if (!IBM_THEME_DEV) return false;
// RSC cannot read localStorage — mirror BrandProvider's default brand.
const effectiveBrand = skipDevBrand ? DEFAULT_BRAND : resolveBrand(brand);
return effectiveBrand !== "oss";
}
/** False when the admin explicitly disabled this connector for the workspace. */
function isConnectorAllowedByWorkspace(
type: string,
storedAccess: Record<string, boolean>,
): boolean {
return storedAccess[type] !== false;
}
/**
* Connectors tab visibility: workspace policy first, then deployment rules.
* Explicit `storedAccess[type] === true` overrides deployment filters except
* OneDrive outside OSS brand (hidden in SaaS/cloud UI).
*/
export function isConnectorShownInWorkspace(
type: string,
storedAccess: Record<string, boolean>,
{
isCloudBrand: cloudBrand,
isIbmAuthMode,
}: { isCloudBrand: boolean; isIbmAuthMode: boolean },
): boolean {
if (!isConnectorAllowedByWorkspace(type, storedAccess)) return false;
if (storedAccess[type] === true && !(type === "onedrive" && cloudBrand)) {
return true;
}
return isConnectorTypeVisible(type, {
isCloudBrand: cloudBrand,
isIbmAuthMode,
});
}
/** Deployment filter shared by Connectors tab, permission admin, and upload menus. */
export function isConnectorTypeVisible(
type: string,
{
isCloudBrand: cloudBrand,
isIbmAuthMode,
}: { isCloudBrand: boolean; isIbmAuthMode: boolean },
): boolean {
if (type === "ibm_cos") return isIbmAuthMode || DEV_IBM_COS;
if (type === "aws_s3") return isIbmAuthMode;
if (cloudBrand && type === "onedrive") return false;
return true;
}
// --- Settings tab access (nav, RSC guards, auth-context `can()`) ---
export type SettingsTabAccessContext = {
isSaasPolicy: boolean;
isNoAuthMode: boolean;
rbacEnforced: boolean;
permissions: Set<string>;
};
export function buildSettingsTabAccess({
isIbmAuthMode,
cloudContext,
isNoAuthMode,
permissions,
rbacEnforced,
brand = DEFAULT_BRAND,
useClientBrandPolicy = true,
}: {
isIbmAuthMode: boolean;
cloudContext: boolean;
isNoAuthMode: boolean;
permissions: Set<string>;
rbacEnforced: boolean;
brand?: Brand | string;
/** False on RSC tab guards — brand lives in localStorage, not on the server. */
useClientBrandPolicy?: boolean;
}): SettingsTabAccessContext {
return {
isSaasPolicy: isSaasPolicyContext({
isIbmAuthMode,
cloudContext,
brand,
skipDevBrand: !useClientBrandPolicy,
}),
isNoAuthMode,
rbacEnforced,
permissions,
};
}
export function hasRbacPermission(
perm: string,
{
isNoAuthMode,
rbacEnforced,
permissions,
}: Pick<
SettingsTabAccessContext,
"isNoAuthMode" | "rbacEnforced" | "permissions"
>,
): boolean {
if (isNoAuthMode || !rbacEnforced) return true;
return permissions.has(perm);
}
/** RBAC applies in SaaS policy context only; OSS shows all standard tabs. */
export function canShowRbacGatedSettingsTab(
perm: string,
ctx: SettingsTabAccessContext,
): boolean {
if (!ctx.isSaasPolicy) return true;
return hasRbacPermission(perm, ctx);
}
export function canAccessConnectorAccessTab(
ctx: SettingsTabAccessContext,
): boolean {
return ctx.isSaasPolicy && hasRbacPermission("connectors:manage:access", ctx);
}