@openpeepshq/common 0.1.46 → 0.3.0
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.
- package/dist/lib/adminSections.js +1 -1
- package/dist/lib/capabilitiesHelpers.js +30 -40
- package/dist/lib/csvHelpers.js +1 -1
- package/dist/lib/eventIcs.js +26 -32
- package/dist/lib/eventRecurrence.d.ts +17 -0
- package/dist/lib/eventRecurrence.js +75 -18
- package/dist/lib/eventTime.js +9 -11
- package/dist/lib/groupHelpers.js +13 -34
- package/dist/lib/groupTemplateHelpers.js +6 -10
- package/dist/lib/jamHelpers.js +6 -9
- package/dist/lib/markdownHelpers.d.ts +37 -0
- package/dist/lib/markdownHelpers.js +151 -3
- package/dist/lib/postHelpers.js +70 -97
- package/dist/lib/profileHelpers.js +17 -24
- package/dist/lib/pushHelpers.js +3 -4
- package/dist/lib/scopeHelpers.js +6 -9
- package/dist/lib/ssoLoginUrl.js +4 -6
- package/dist/lib/ssoProviders.js +35 -39
- package/dist/lib/tokenHelpers.js +5 -5
- package/dist/lib/utils.js +6 -7
- package/dist/types/admin-db.js +1 -1
- package/dist/types/api.d.ts +1 -0
- package/dist/types/api.js +1 -1
- package/dist/types/config.d.ts +3 -0
- package/dist/types/config.js +3 -0
- package/dist/types/models.js +1 -1
- package/dist/types/plugins.js +2 -2
- package/dist/zod.d.ts +1 -0
- package/dist/zod.js +1 -0
- package/package.json +36 -6
- package/src/index.ts +4 -0
- package/src/lib/adminSections.ts +84 -0
- package/src/lib/attachmentHelpers.ts +14 -0
- package/src/lib/capabilitiesHelpers.ts +532 -0
- package/src/lib/colors.ts +20 -0
- package/src/lib/csvHelpers.ts +31 -0
- package/src/lib/eventIcs.ts +265 -0
- package/src/lib/eventRecurrence.ts +495 -0
- package/src/lib/eventTime.ts +206 -0
- package/src/lib/federationHosts.ts +22 -0
- package/src/lib/feedCursor.ts +27 -0
- package/src/lib/feedFormat.ts +24 -0
- package/src/lib/groupHelpers.ts +421 -0
- package/src/lib/groupTemplateHelpers.ts +83 -0
- package/src/lib/index.ts +29 -0
- package/src/lib/jamHelpers.ts +164 -0
- package/src/lib/legacyThemeRoot.ts +14 -0
- package/src/lib/markdownHelpers.ts +290 -0
- package/src/lib/mediaDimensions.ts +21 -0
- package/src/lib/postHelpers.ts +783 -0
- package/src/lib/profileHelpers.ts +220 -0
- package/src/lib/pushHelpers.ts +34 -0
- package/src/lib/reactions.ts +52 -0
- package/src/lib/reportHelpers.ts +7 -0
- package/src/lib/scopeHelpers.ts +176 -0
- package/src/lib/ssoLoginUrl.ts +93 -0
- package/src/lib/ssoProviders.ts +472 -0
- package/src/lib/text.ts +26 -0
- package/src/lib/timeZone.ts +52 -0
- package/src/lib/tokenHelpers.ts +119 -0
- package/src/lib/utils.ts +322 -0
- package/src/types/admin-db.ts +74 -0
- package/src/types/admin-email.ts +55 -0
- package/src/types/admin-jobs.ts +17 -0
- package/src/types/admin-performance.ts +20 -0
- package/src/types/analytics.ts +318 -0
- package/src/types/api.ts +871 -0
- package/src/types/apple-app-site-association.ts +54 -0
- package/src/types/auth.ts +22 -0
- package/src/types/capabilities.ts +280 -0
- package/src/types/config.ts +546 -0
- package/src/types/db.ts +138 -0
- package/src/types/export.ts +10 -0
- package/src/types/index.ts +20 -0
- package/src/types/internal.ts +338 -0
- package/src/types/models.ts +939 -0
- package/src/types/notifications.ts +92 -0
- package/src/types/plugins.ts +150 -0
- package/src/types/roleDefaults.ts +132 -0
- package/src/types/stats.ts +195 -0
- package/src/types/stripe.ts +45 -0
- package/src/types/utils.ts +100 -0
- package/src/zod.ts +1 -0
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AccessTokenRelationship,
|
|
3
|
+
AccountWithMeta,
|
|
4
|
+
AuthorizationData,
|
|
5
|
+
Capabilities,
|
|
6
|
+
CapabilitiesConfig,
|
|
7
|
+
GroupRelationship,
|
|
8
|
+
PostRelationship,
|
|
9
|
+
ProfileRelationship,
|
|
10
|
+
ProfileWithMeta,
|
|
11
|
+
PublicPost,
|
|
12
|
+
PublicPostInput,
|
|
13
|
+
PublicProfile,
|
|
14
|
+
PublicReport,
|
|
15
|
+
ReportRelationship,
|
|
16
|
+
Role,
|
|
17
|
+
AccessTokenWithMeta,
|
|
18
|
+
GroupWithMeta,
|
|
19
|
+
Resource,
|
|
20
|
+
Scope,
|
|
21
|
+
} from '../types';
|
|
22
|
+
import {
|
|
23
|
+
calculateRequiredScope,
|
|
24
|
+
scopeMatches,
|
|
25
|
+
withPublicPostReadScopes,
|
|
26
|
+
withPublicGroupReadScopes,
|
|
27
|
+
withPublicProfileReadScopes,
|
|
28
|
+
} from './scopeHelpers';
|
|
29
|
+
import { viewerBlockedTarget, targetBlockedViewer } from './profileHelpers';
|
|
30
|
+
|
|
31
|
+
export const checkCapabilities = (
|
|
32
|
+
neededCapabilities: string[],
|
|
33
|
+
presentCapabilities: { add?: string[]; remove?: string[] } = {
|
|
34
|
+
add: [],
|
|
35
|
+
remove: [],
|
|
36
|
+
},
|
|
37
|
+
) => {
|
|
38
|
+
const containsCapability = (c: string, clist: string[]) =>
|
|
39
|
+
!!clist?.find(
|
|
40
|
+
(pc) => pc === c || (pc.includes('*') && c.startsWith(pc.split('*')[0])),
|
|
41
|
+
);
|
|
42
|
+
const missingCapabilities = neededCapabilities.filter(
|
|
43
|
+
(c) =>
|
|
44
|
+
containsCapability(c, presentCapabilities.remove ?? []) ||
|
|
45
|
+
!containsCapability(c, presentCapabilities.add ?? []),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
success: missingCapabilities.length === 0,
|
|
50
|
+
missingCapabilities,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const mergeCapabilities = (
|
|
55
|
+
capabilities?: Partial<Capabilities>[],
|
|
56
|
+
): Capabilities => {
|
|
57
|
+
return {
|
|
58
|
+
add: [...new Set(capabilities?.map((c) => c?.add || []).flat() ?? [])],
|
|
59
|
+
remove: [
|
|
60
|
+
...new Set(capabilities?.map((c) => c?.remove || []).flat() ?? []),
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** Order-independent equality check for two capability add/remove lists. */
|
|
66
|
+
export const capabilitySetsEqual = (
|
|
67
|
+
a: string[] | undefined,
|
|
68
|
+
b: string[] | undefined,
|
|
69
|
+
) => {
|
|
70
|
+
const sa = new Set(a ?? []);
|
|
71
|
+
const sb = new Set(b ?? []);
|
|
72
|
+
if (sa.size !== sb.size) return false;
|
|
73
|
+
return [...sa].every((value) => sb.has(value));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const checkRoleCapabilities = (
|
|
77
|
+
roles: Role[] = [],
|
|
78
|
+
neededCapabilities: string[],
|
|
79
|
+
) =>
|
|
80
|
+
checkCapabilities(
|
|
81
|
+
neededCapabilities,
|
|
82
|
+
mergeCapabilities(roles.map((r) => r.capabilities)),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
/** Any of these matching `roles` shows the Administration row (submenu is separate UX). */
|
|
86
|
+
export const ADMIN_SIDEBAR_MENU_CAPABILITIES = [
|
|
87
|
+
'admin',
|
|
88
|
+
'core-roles-read',
|
|
89
|
+
'core-logs-read',
|
|
90
|
+
'core-accounts-list',
|
|
91
|
+
'core-backups-read',
|
|
92
|
+
'core-analytics-read',
|
|
93
|
+
'core-inviteLinks-read',
|
|
94
|
+
'core-maintenance-restart',
|
|
95
|
+
'core-config-read',
|
|
96
|
+
'core-customization-read',
|
|
97
|
+
] as const;
|
|
98
|
+
|
|
99
|
+
export const hasAdminSidebarAccess = (roles: Role[] = []) =>
|
|
100
|
+
ADMIN_SIDEBAR_MENU_CAPABILITIES.some(
|
|
101
|
+
(cap) => checkRoleCapabilities(roles, [cap]).success,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
export const isLocal = (profile: ProfileWithMeta) =>
|
|
105
|
+
checkRoleCapabilities(profile.roles, ['core-local']).success;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns `['local', 'none']` when the caller is "trusted local" — either a local
|
|
109
|
+
* profile or an authenticated service — and `['none']` otherwise.
|
|
110
|
+
*/
|
|
111
|
+
export const localOrNone = (
|
|
112
|
+
authData: AuthorizationData,
|
|
113
|
+
): ['local', 'none'] | ['none'] => {
|
|
114
|
+
if (authData.service) {
|
|
115
|
+
return ['local', 'none'];
|
|
116
|
+
}
|
|
117
|
+
if (authData.profile && isLocal(authData.profile)) {
|
|
118
|
+
return ['local', 'none'];
|
|
119
|
+
}
|
|
120
|
+
return ['none'];
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const getGroupRelationships = (
|
|
124
|
+
authData: AuthorizationData,
|
|
125
|
+
group: GroupWithMeta,
|
|
126
|
+
): [GroupRelationship, ...GroupRelationship[]] => {
|
|
127
|
+
const membershipRoles =
|
|
128
|
+
authData.profile?.memberships.find((m) => m.group.id === group.id)?.roles ??
|
|
129
|
+
[];
|
|
130
|
+
|
|
131
|
+
if (authData.profile || authData.service) {
|
|
132
|
+
return [...localOrNone(authData), ...membershipRoles] as [
|
|
133
|
+
GroupRelationship,
|
|
134
|
+
...GroupRelationship[],
|
|
135
|
+
];
|
|
136
|
+
}
|
|
137
|
+
return ['none'];
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export const getProfileRelationships = (
|
|
141
|
+
authData: AuthorizationData,
|
|
142
|
+
otherProfile: PublicProfile,
|
|
143
|
+
): ProfileRelationship[] => {
|
|
144
|
+
const { profile } = authData;
|
|
145
|
+
if (profile && profile.id === otherProfile?.id) {
|
|
146
|
+
return ['self', ...localOrNone(authData)];
|
|
147
|
+
}
|
|
148
|
+
const relationships: ProfileRelationship[] = localOrNone(authData);
|
|
149
|
+
if (profile?.followers.map((f) => f.id).includes(otherProfile?.id)) {
|
|
150
|
+
relationships.push('followed-by');
|
|
151
|
+
}
|
|
152
|
+
if (profile?.following.map((f) => f.id).includes(otherProfile?.id)) {
|
|
153
|
+
relationships.push('following');
|
|
154
|
+
}
|
|
155
|
+
if (viewerBlockedTarget(profile, otherProfile?.id)) {
|
|
156
|
+
relationships.push('blocked');
|
|
157
|
+
}
|
|
158
|
+
if (targetBlockedViewer(profile, otherProfile?.id)) {
|
|
159
|
+
relationships.push('blocked-by');
|
|
160
|
+
}
|
|
161
|
+
return relationships;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export const getPostRelationships = (
|
|
165
|
+
authData: AuthorizationData,
|
|
166
|
+
post: PublicPostInput,
|
|
167
|
+
): PostRelationship[] => {
|
|
168
|
+
const { profile } = authData;
|
|
169
|
+
const relationships: PostRelationship[] = [];
|
|
170
|
+
|
|
171
|
+
if (['public', 'local'].includes(post.visibility)) {
|
|
172
|
+
relationships.push(...localOrNone(authData));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (profile && post.audience?.some((a) => a.id === profile.id)) {
|
|
176
|
+
relationships.push('audience');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (profile && post.mentions?.some((m) => m.profile.id === profile.id)) {
|
|
180
|
+
relationships.push('mentioned');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (
|
|
184
|
+
profile &&
|
|
185
|
+
post.entries?.some(
|
|
186
|
+
(e) =>
|
|
187
|
+
e.type === 'rsvp' &&
|
|
188
|
+
e.profile.id === profile.id &&
|
|
189
|
+
['yes', 'tentative'].includes(e.data.response),
|
|
190
|
+
)
|
|
191
|
+
) {
|
|
192
|
+
relationships.push('attendee');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return relationships;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export const getReportRelationships = (
|
|
199
|
+
authData: AuthorizationData,
|
|
200
|
+
report: PublicReport,
|
|
201
|
+
): ReportRelationship[] => {
|
|
202
|
+
const { profile } = authData;
|
|
203
|
+
if (profile && report.reporterProfile.id === profile.id) {
|
|
204
|
+
return [...localOrNone(authData), 'reporter'];
|
|
205
|
+
}
|
|
206
|
+
if (profile && report.reportedProfile.id === profile.id) {
|
|
207
|
+
return [...localOrNone(authData), 'reported'];
|
|
208
|
+
}
|
|
209
|
+
return localOrNone(authData);
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export const getAccessTokenRelationships = (
|
|
213
|
+
authData: AuthorizationData,
|
|
214
|
+
accessToken: AccessTokenWithMeta,
|
|
215
|
+
): AccessTokenRelationship[] => {
|
|
216
|
+
const { profile } = authData;
|
|
217
|
+
const relationships: AccessTokenRelationship[] = [...localOrNone(authData)];
|
|
218
|
+
const ownerProfileId = accessToken.ownedBy?.id;
|
|
219
|
+
if (ownerProfileId && profile?.id === ownerProfileId) {
|
|
220
|
+
relationships.push('owner');
|
|
221
|
+
}
|
|
222
|
+
return relationships;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const getConfigCapabilities = (
|
|
226
|
+
object: 'post' | 'profile' | 'report' | 'accessToken',
|
|
227
|
+
relationships: (
|
|
228
|
+
| PostRelationship
|
|
229
|
+
| ProfileRelationship
|
|
230
|
+
| ReportRelationship
|
|
231
|
+
| AccessTokenRelationship
|
|
232
|
+
)[],
|
|
233
|
+
config: CapabilitiesConfig,
|
|
234
|
+
): Capabilities =>
|
|
235
|
+
mergeCapabilities(
|
|
236
|
+
relationships.map((r) => {
|
|
237
|
+
const group = config[object] as Partial<
|
|
238
|
+
Record<string, Capabilities | undefined>
|
|
239
|
+
>;
|
|
240
|
+
return group[r] ?? { add: [], remove: [] };
|
|
241
|
+
}),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
const getGroupRelationshipCapabilities = (
|
|
245
|
+
authData: AuthorizationData,
|
|
246
|
+
group?: GroupWithMeta | null,
|
|
247
|
+
): Capabilities =>
|
|
248
|
+
mergeCapabilities(
|
|
249
|
+
group
|
|
250
|
+
? getGroupRelationships(authData, group).map(
|
|
251
|
+
(r) => group.capabilities?.[r] ?? { add: [], remove: [] },
|
|
252
|
+
)
|
|
253
|
+
: [],
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
export const getGroupCapabilitiesByRoles = (
|
|
257
|
+
roles: string[] | undefined | null,
|
|
258
|
+
group?: GroupWithMeta | null,
|
|
259
|
+
) =>
|
|
260
|
+
mergeCapabilities(
|
|
261
|
+
(roles ?? []).map(
|
|
262
|
+
(r) => group?.capabilities?.[r] ?? { add: [], remove: [] },
|
|
263
|
+
),
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
// Per-group powers (add member, post in group, moderate, …) come from the
|
|
267
|
+
// caller's relationship to the group (edge roles plus none/local), never from
|
|
268
|
+
// their instance role. Instance roles only gate group *creation*
|
|
269
|
+
// (`core-groups-create`), checked separately at the creation entry points.
|
|
270
|
+
export const getGroupCapabilities = (
|
|
271
|
+
authData: AuthorizationData,
|
|
272
|
+
group?: GroupWithMeta | null,
|
|
273
|
+
): Capabilities => getGroupRelationshipCapabilities(authData, group);
|
|
274
|
+
|
|
275
|
+
const getRoleCapabilities = (profile?: ProfileWithMeta): Capabilities =>
|
|
276
|
+
mergeCapabilities(profile?.roles.map((r) => r.capabilities) ?? []);
|
|
277
|
+
|
|
278
|
+
export const getProfileCapabilities = (
|
|
279
|
+
authData: AuthorizationData,
|
|
280
|
+
targetProfile: PublicProfile,
|
|
281
|
+
config: CapabilitiesConfig,
|
|
282
|
+
): Capabilities =>
|
|
283
|
+
getConfigCapabilities(
|
|
284
|
+
'profile',
|
|
285
|
+
getProfileRelationships(authData, targetProfile),
|
|
286
|
+
config,
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
const getPostVisibilityCapabilities = (
|
|
290
|
+
post: PublicPost,
|
|
291
|
+
profile: ProfileWithMeta | undefined,
|
|
292
|
+
) => {
|
|
293
|
+
switch (post.visibility) {
|
|
294
|
+
case 'public':
|
|
295
|
+
return { add: ['core-posts-read'], remove: [] };
|
|
296
|
+
case 'direct':
|
|
297
|
+
return {};
|
|
298
|
+
// Group posts derive their capabilities from the group relationship
|
|
299
|
+
// (`getGroupCapabilities`) and the author's `self` relationship only —
|
|
300
|
+
// never from instance roles. This keeps per-group powers consistent with
|
|
301
|
+
// every other group check (see getGroupCapabilities); instance roles only
|
|
302
|
+
// gate group creation.
|
|
303
|
+
case 'group':
|
|
304
|
+
return {};
|
|
305
|
+
default:
|
|
306
|
+
return getRoleCapabilities(profile);
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const getScopedPostReadCapabilities = (
|
|
311
|
+
authData: AuthorizationData,
|
|
312
|
+
neededCapabilities: string[],
|
|
313
|
+
post: PublicPost,
|
|
314
|
+
): Capabilities => {
|
|
315
|
+
if (
|
|
316
|
+
authData.profile ||
|
|
317
|
+
post.visibility !== 'local' ||
|
|
318
|
+
neededCapabilities.length !== 1 ||
|
|
319
|
+
neededCapabilities[0] !== 'core-posts-read' ||
|
|
320
|
+
!scopeMatches({
|
|
321
|
+
scopes: authData.scopes,
|
|
322
|
+
requiredScope: {
|
|
323
|
+
scopeLevel: 'admin',
|
|
324
|
+
resource: { type: 'posts', id: post.id },
|
|
325
|
+
},
|
|
326
|
+
})
|
|
327
|
+
) {
|
|
328
|
+
return { add: [], remove: [] };
|
|
329
|
+
}
|
|
330
|
+
return { add: ['core-posts-read'], remove: [] };
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
export const getPostCapabilities = (
|
|
334
|
+
authData: AuthorizationData,
|
|
335
|
+
post: PublicPost,
|
|
336
|
+
config: CapabilitiesConfig,
|
|
337
|
+
): Capabilities =>
|
|
338
|
+
mergeCapabilities([
|
|
339
|
+
getPostVisibilityCapabilities(post, authData.profile),
|
|
340
|
+
getConfigCapabilities('post', getPostRelationships(authData, post), config),
|
|
341
|
+
getConfigCapabilities(
|
|
342
|
+
'profile',
|
|
343
|
+
getProfileRelationships(authData, post.profile),
|
|
344
|
+
config,
|
|
345
|
+
),
|
|
346
|
+
getGroupCapabilities(authData, post.group),
|
|
347
|
+
]);
|
|
348
|
+
|
|
349
|
+
const getReportCapabilities = (
|
|
350
|
+
authData: AuthorizationData,
|
|
351
|
+
report: PublicReport,
|
|
352
|
+
config: CapabilitiesConfig,
|
|
353
|
+
): Capabilities =>
|
|
354
|
+
mergeCapabilities([
|
|
355
|
+
getConfigCapabilities(
|
|
356
|
+
'report',
|
|
357
|
+
getReportRelationships(authData, report),
|
|
358
|
+
config,
|
|
359
|
+
),
|
|
360
|
+
getRoleCapabilities(authData.profile),
|
|
361
|
+
]);
|
|
362
|
+
|
|
363
|
+
const getAccessTokenCapabilities = (
|
|
364
|
+
authData: AuthorizationData,
|
|
365
|
+
accessToken: AccessTokenWithMeta,
|
|
366
|
+
config: CapabilitiesConfig,
|
|
367
|
+
): Capabilities =>
|
|
368
|
+
mergeCapabilities([
|
|
369
|
+
getConfigCapabilities(
|
|
370
|
+
'accessToken',
|
|
371
|
+
getAccessTokenRelationships(authData, accessToken),
|
|
372
|
+
config,
|
|
373
|
+
),
|
|
374
|
+
getRoleCapabilities(authData.profile),
|
|
375
|
+
]);
|
|
376
|
+
|
|
377
|
+
const getAccountCapabilities = (
|
|
378
|
+
authData: AuthorizationData,
|
|
379
|
+
targetAccount: AccountWithMeta,
|
|
380
|
+
): Capabilities =>
|
|
381
|
+
mergeCapabilities([
|
|
382
|
+
authData.account?.id === targetAccount.id
|
|
383
|
+
? { add: ['core-accounts-update'], remove: [] }
|
|
384
|
+
: { add: [], remove: [] },
|
|
385
|
+
getRoleCapabilities(authData.profile),
|
|
386
|
+
]);
|
|
387
|
+
|
|
388
|
+
const checkCapabilitiesWithCalculatedScope = (
|
|
389
|
+
capabilities: Capabilities,
|
|
390
|
+
authData: AuthorizationData,
|
|
391
|
+
neededCapabilities: string[],
|
|
392
|
+
resource: Resource,
|
|
393
|
+
) => {
|
|
394
|
+
const requiredScope = calculateRequiredScope({
|
|
395
|
+
resource,
|
|
396
|
+
requiredCapabilities: neededCapabilities,
|
|
397
|
+
});
|
|
398
|
+
return checkCapabilitiesWithExplicitScope(
|
|
399
|
+
capabilities,
|
|
400
|
+
authData,
|
|
401
|
+
neededCapabilities,
|
|
402
|
+
requiredScope,
|
|
403
|
+
);
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
const checkCapabilitiesWithExplicitScope = (
|
|
407
|
+
capabilities: Capabilities,
|
|
408
|
+
authData: AuthorizationData,
|
|
409
|
+
neededCapabilities: string[],
|
|
410
|
+
requiredScope: Scope,
|
|
411
|
+
) => {
|
|
412
|
+
const capabilitiesResult = checkCapabilities(
|
|
413
|
+
neededCapabilities,
|
|
414
|
+
capabilities,
|
|
415
|
+
);
|
|
416
|
+
const scopeResult = scopeMatches({ scopes: authData.scopes, requiredScope });
|
|
417
|
+
return {
|
|
418
|
+
success: capabilitiesResult.success && scopeResult,
|
|
419
|
+
missingCapabilities: capabilitiesResult.missingCapabilities,
|
|
420
|
+
missingScope: scopeResult ? undefined : requiredScope,
|
|
421
|
+
};
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
export const checkGroupCapabilities = (
|
|
425
|
+
authData: AuthorizationData,
|
|
426
|
+
neededCapabilities: string[],
|
|
427
|
+
group: GroupWithMeta,
|
|
428
|
+
) =>
|
|
429
|
+
checkCapabilitiesWithCalculatedScope(
|
|
430
|
+
getGroupCapabilities(authData, group),
|
|
431
|
+
withPublicGroupReadScopes(authData, group, neededCapabilities),
|
|
432
|
+
neededCapabilities,
|
|
433
|
+
{ type: 'groups', id: group.id },
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
export const checkPostCapabilities = (
|
|
437
|
+
authData: AuthorizationData,
|
|
438
|
+
neededCapabilities: string[],
|
|
439
|
+
post: PublicPost,
|
|
440
|
+
config: CapabilitiesConfig,
|
|
441
|
+
) => {
|
|
442
|
+
const capabilities = mergeCapabilities([
|
|
443
|
+
getPostCapabilities(authData, post, config),
|
|
444
|
+
getScopedPostReadCapabilities(authData, neededCapabilities, post),
|
|
445
|
+
]);
|
|
446
|
+
|
|
447
|
+
return checkCapabilitiesWithCalculatedScope(
|
|
448
|
+
capabilities,
|
|
449
|
+
withPublicPostReadScopes(authData, post, neededCapabilities),
|
|
450
|
+
neededCapabilities,
|
|
451
|
+
{ type: 'posts', id: post.id },
|
|
452
|
+
);
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
export const checkProfileCapabilities = (
|
|
456
|
+
authData: AuthorizationData,
|
|
457
|
+
neededCapabilities: string[],
|
|
458
|
+
targetProfile: PublicProfile,
|
|
459
|
+
config: CapabilitiesConfig,
|
|
460
|
+
) =>
|
|
461
|
+
checkCapabilitiesWithCalculatedScope(
|
|
462
|
+
getProfileCapabilities(authData, targetProfile, config),
|
|
463
|
+
withPublicProfileReadScopes(
|
|
464
|
+
authData,
|
|
465
|
+
targetProfile,
|
|
466
|
+
neededCapabilities,
|
|
467
|
+
config,
|
|
468
|
+
),
|
|
469
|
+
neededCapabilities,
|
|
470
|
+
{ type: 'profiles', id: targetProfile.id },
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
export const checkReportCapabilities = (
|
|
474
|
+
authData: AuthorizationData,
|
|
475
|
+
neededCapabilities: string[],
|
|
476
|
+
report: PublicReport,
|
|
477
|
+
config: CapabilitiesConfig,
|
|
478
|
+
) =>
|
|
479
|
+
checkCapabilitiesWithCalculatedScope(
|
|
480
|
+
getReportCapabilities(authData, report, config),
|
|
481
|
+
authData,
|
|
482
|
+
neededCapabilities,
|
|
483
|
+
{ type: 'reports', id: report.id },
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
export const checkAccessTokenCapabilities = (
|
|
487
|
+
authData: AuthorizationData,
|
|
488
|
+
neededCapabilities: string[],
|
|
489
|
+
accessToken: AccessTokenWithMeta,
|
|
490
|
+
config: CapabilitiesConfig,
|
|
491
|
+
) =>
|
|
492
|
+
checkCapabilitiesWithExplicitScope(
|
|
493
|
+
getAccessTokenCapabilities(authData, accessToken, config),
|
|
494
|
+
authData,
|
|
495
|
+
neededCapabilities,
|
|
496
|
+
{ scopeLevel: 'admin', resource: { type: 'self' } },
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
export const checkAccountCapabilities = (
|
|
500
|
+
authData: AuthorizationData,
|
|
501
|
+
neededCapabilities: string[],
|
|
502
|
+
targetAccount: AccountWithMeta,
|
|
503
|
+
) =>
|
|
504
|
+
checkCapabilitiesWithExplicitScope(
|
|
505
|
+
getAccountCapabilities(authData, targetAccount),
|
|
506
|
+
authData,
|
|
507
|
+
neededCapabilities,
|
|
508
|
+
{ scopeLevel: 'admin', resource: { type: 'self' } },
|
|
509
|
+
);
|
|
510
|
+
|
|
511
|
+
/** Capability required to create accounts when public sign-ups are closed. */
|
|
512
|
+
export const ACCOUNT_CREATE_CAPABILITY = 'core-accounts-create';
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Whether the caller may register a new account/profile while sign-ups are closed.
|
|
516
|
+
* Profile tokens need the role capability; service tokens are treated as having it
|
|
517
|
+
* and are gated by write scope on profiles (same as other create operations).
|
|
518
|
+
*/
|
|
519
|
+
export const checkAccountCreateAuthorization = (
|
|
520
|
+
authData: AuthorizationData,
|
|
521
|
+
) => {
|
|
522
|
+
const neededCapabilities = [ACCOUNT_CREATE_CAPABILITY];
|
|
523
|
+
const capabilities = authData.service
|
|
524
|
+
? { add: neededCapabilities, remove: [] }
|
|
525
|
+
: getRoleCapabilities(authData.profile);
|
|
526
|
+
return checkCapabilitiesWithCalculatedScope(
|
|
527
|
+
capabilities,
|
|
528
|
+
authData,
|
|
529
|
+
neededCapabilities,
|
|
530
|
+
{ type: 'profiles', id: '*' },
|
|
531
|
+
);
|
|
532
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const hexToRgb = (hex: string) => {
|
|
2
|
+
// Remove the hash at the start if it's there
|
|
3
|
+
hex = hex.replace(/^#/, '');
|
|
4
|
+
|
|
5
|
+
// Expand 3-character hex to 6-character
|
|
6
|
+
if (hex.length === 3) {
|
|
7
|
+
hex = hex
|
|
8
|
+
.split('')
|
|
9
|
+
.map((char) => char + char)
|
|
10
|
+
.join('');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Parse the r, g, b values
|
|
14
|
+
const bigint = parseInt(hex, 16);
|
|
15
|
+
const r = (bigint >> 16) & 255;
|
|
16
|
+
const g = (bigint >> 8) & 255;
|
|
17
|
+
const b = bigint & 255;
|
|
18
|
+
|
|
19
|
+
return { r, g, b };
|
|
20
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const escapeCsvCell = (
|
|
2
|
+
value: string | number | boolean | null | undefined,
|
|
3
|
+
): string => {
|
|
4
|
+
if (value == null) {
|
|
5
|
+
return '';
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const str = String(value);
|
|
9
|
+
|
|
10
|
+
if (/[",\n\r]/.test(str)) {
|
|
11
|
+
return `"${str.replace(/"/g, '""')}"`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return str;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const toCsvRow = (
|
|
18
|
+
values: Array<string | number | null | undefined | boolean>,
|
|
19
|
+
): string => values.map(escapeCsvCell).join(',');
|
|
20
|
+
|
|
21
|
+
export const formatMemberExportCustomFields = (
|
|
22
|
+
fields: ReadonlyArray<{ name: string; value: string }> | undefined,
|
|
23
|
+
): string => {
|
|
24
|
+
if (!fields?.length) {
|
|
25
|
+
return '';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return JSON.stringify(
|
|
29
|
+
fields.map(({ name, value }) => ({ name, value })),
|
|
30
|
+
);
|
|
31
|
+
};
|