@nexys/user-management-web 0.3.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.
- package/README.md +61 -0
- package/components.json +21 -0
- package/package.json +66 -0
- package/src/components/audit/audit-log-table.test.tsx +98 -0
- package/src/components/audit/audit-log-table.tsx +227 -0
- package/src/components/auth/accept-invitation-card.tsx +104 -0
- package/src/components/auth/forgot-password-form.tsx +93 -0
- package/src/components/auth/reset-password-form.tsx +144 -0
- package/src/components/auth/sign-in-form.test.tsx +164 -0
- package/src/components/auth/sign-in-form.tsx +504 -0
- package/src/components/auth/sign-up-form.tsx +124 -0
- package/src/components/layout/app-shell.tsx +189 -0
- package/src/components/layout/auth-shell.tsx +28 -0
- package/src/components/layout/theme-toggle.tsx +55 -0
- package/src/components/layout/user-menu.tsx +84 -0
- package/src/components/primitives/empty-state.tsx +40 -0
- package/src/components/primitives/page-header.tsx +27 -0
- package/src/components/primitives/spinner.tsx +28 -0
- package/src/components/profile/api-tokens-card.tsx +238 -0
- package/src/components/profile/change-password-form.tsx +159 -0
- package/src/components/profile/passkeys-card.tsx +186 -0
- package/src/components/profile/profile-card.tsx +107 -0
- package/src/components/profile/refresh-tokens-list.tsx +176 -0
- package/src/components/profile/two-factor-card.tsx +230 -0
- package/src/components/projects/project-consents.tsx +577 -0
- package/src/components/projects/project-create-dialog.tsx +193 -0
- package/src/components/projects/project-detail-tabs.tsx +64 -0
- package/src/components/projects/project-email-templates.tsx +285 -0
- package/src/components/projects/project-notices.tsx +461 -0
- package/src/components/projects/project-settings.tsx +1059 -0
- package/src/components/projects/project-tenants.tsx +108 -0
- package/src/components/projects/projects-table.test.tsx +109 -0
- package/src/components/projects/projects-table.tsx +240 -0
- package/src/components/roles/role-badge.tsx +91 -0
- package/src/components/roles/role-select.tsx +76 -0
- package/src/components/tenants/tenant-create-dialog.tsx +164 -0
- package/src/components/tenants/tenant-detail-tabs.tsx +51 -0
- package/src/components/tenants/tenant-members.tsx +587 -0
- package/src/components/tenants/tenant-roles.test.tsx +107 -0
- package/src/components/tenants/tenant-roles.tsx +418 -0
- package/src/components/tenants/tenant-settings.tsx +392 -0
- package/src/components/tenants/tenants-table.test.tsx +81 -0
- package/src/components/tenants/tenants-table.tsx +247 -0
- package/src/components/ui/alert.tsx +52 -0
- package/src/components/ui/avatar.tsx +46 -0
- package/src/components/ui/badge.tsx +32 -0
- package/src/components/ui/button.tsx +64 -0
- package/src/components/ui/card.tsx +65 -0
- package/src/components/ui/dialog.tsx +99 -0
- package/src/components/ui/dropdown-menu.tsx +97 -0
- package/src/components/ui/field.tsx +44 -0
- package/src/components/ui/input.tsx +23 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/select.tsx +87 -0
- package/src/components/ui/separator.tsx +22 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/table.tsx +87 -0
- package/src/components/ui/tabs.tsx +51 -0
- package/src/components/ui/textarea.tsx +22 -0
- package/src/components/ui/tooltip.tsx +27 -0
- package/src/components/users/user-create-dialog.tsx +229 -0
- package/src/components/users/user-delete-dialog.tsx +88 -0
- package/src/components/users/user-detail.tsx +818 -0
- package/src/components/users/users-table.test.tsx +107 -0
- package/src/components/users/users-table.tsx +386 -0
- package/src/context/auth-context.tsx +127 -0
- package/src/context/theme-context.test.tsx +78 -0
- package/src/context/theme-context.tsx +110 -0
- package/src/hooks/use-call.ts +60 -0
- package/src/index.ts +160 -0
- package/src/lib/error-message.ts +16 -0
- package/src/lib/utils.test.ts +61 -0
- package/src/lib/utils.ts +79 -0
- package/src/lib/webauthn.ts +36 -0
- package/src/styles.css +107 -0
- package/src/test-setup.ts +5 -0
- package/tailwind.preset.cjs +15 -0
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addMember,
|
|
3
|
+
adminListUsers,
|
|
4
|
+
assignMemberRole,
|
|
5
|
+
getFullTenant,
|
|
6
|
+
inviteMember,
|
|
7
|
+
listTenantRoles,
|
|
8
|
+
removeMember,
|
|
9
|
+
removeMemberRole,
|
|
10
|
+
updateMemberRole,
|
|
11
|
+
type Invitation,
|
|
12
|
+
type Member,
|
|
13
|
+
type RoleRow,
|
|
14
|
+
type TenantRole,
|
|
15
|
+
} from "@nexys/user-management-client";
|
|
16
|
+
import {
|
|
17
|
+
AlertCircle,
|
|
18
|
+
Mail,
|
|
19
|
+
Plus,
|
|
20
|
+
Trash2,
|
|
21
|
+
UserPlus,
|
|
22
|
+
Users,
|
|
23
|
+
X,
|
|
24
|
+
} from "lucide-react";
|
|
25
|
+
import { useMemo, useRef, useState, type FormEvent } from "react";
|
|
26
|
+
|
|
27
|
+
import { useUmClient } from "../../context/auth-context.js";
|
|
28
|
+
import { useCall } from "../../hooks/use-call.js";
|
|
29
|
+
import { errorMessage } from "../../lib/error-message.js";
|
|
30
|
+
import { formatRelative, initialsOf } from "../../lib/utils.js";
|
|
31
|
+
import { EmptyState } from "../primitives/empty-state.js";
|
|
32
|
+
import { TenantRoleBadge } from "../roles/role-badge.js";
|
|
33
|
+
import { TenantRoleSelect } from "../roles/role-select.js";
|
|
34
|
+
import { Alert, AlertDescription } from "../ui/alert.js";
|
|
35
|
+
import { Avatar, AvatarFallback } from "../ui/avatar.js";
|
|
36
|
+
import { Badge } from "../ui/badge.js";
|
|
37
|
+
import { Button } from "../ui/button.js";
|
|
38
|
+
import {
|
|
39
|
+
DropdownMenu,
|
|
40
|
+
DropdownMenuContent,
|
|
41
|
+
DropdownMenuItem,
|
|
42
|
+
DropdownMenuLabel,
|
|
43
|
+
DropdownMenuTrigger,
|
|
44
|
+
} from "../ui/dropdown-menu.js";
|
|
45
|
+
import {
|
|
46
|
+
Card,
|
|
47
|
+
CardContent,
|
|
48
|
+
CardDescription,
|
|
49
|
+
CardHeader,
|
|
50
|
+
CardTitle,
|
|
51
|
+
} from "../ui/card.js";
|
|
52
|
+
import { Field } from "../ui/field.js";
|
|
53
|
+
import { Input } from "../ui/input.js";
|
|
54
|
+
import {
|
|
55
|
+
Select,
|
|
56
|
+
SelectContent,
|
|
57
|
+
SelectItem,
|
|
58
|
+
SelectTrigger,
|
|
59
|
+
SelectValue,
|
|
60
|
+
} from "../ui/select.js";
|
|
61
|
+
import { Skeleton } from "../ui/skeleton.js";
|
|
62
|
+
import {
|
|
63
|
+
Table,
|
|
64
|
+
TableBody,
|
|
65
|
+
TableCell,
|
|
66
|
+
TableHead,
|
|
67
|
+
TableHeader,
|
|
68
|
+
TableRow,
|
|
69
|
+
} from "../ui/table.js";
|
|
70
|
+
|
|
71
|
+
export interface TenantMembersProps {
|
|
72
|
+
organizationId: string;
|
|
73
|
+
/** Allow current user to edit member roles and invites. */
|
|
74
|
+
canManage?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Pick existing users from a dropdown instead of typing an email.
|
|
77
|
+
* Needs the system-wide user list, so superadmin/useradmin only.
|
|
78
|
+
*/
|
|
79
|
+
userPicker?: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const TenantMembers = ({
|
|
83
|
+
organizationId,
|
|
84
|
+
canManage = true,
|
|
85
|
+
userPicker = false,
|
|
86
|
+
}: TenantMembersProps) => {
|
|
87
|
+
const client = useUmClient();
|
|
88
|
+
const { data, error, loading, refresh } = useCall(getFullTenant, { organizationId });
|
|
89
|
+
const [busy, setBusy] = useState<string | null>(null);
|
|
90
|
+
const [actionError, setActionError] = useState<string | null>(null);
|
|
91
|
+
|
|
92
|
+
const runAction = async <O, E>(
|
|
93
|
+
key: string,
|
|
94
|
+
op: () => Promise<{ ok: true; data: O } | { ok: false; error: E }>
|
|
95
|
+
) => {
|
|
96
|
+
setBusy(key);
|
|
97
|
+
setActionError(null);
|
|
98
|
+
const res = await op();
|
|
99
|
+
setBusy(null);
|
|
100
|
+
if (res.ok) {
|
|
101
|
+
await refresh();
|
|
102
|
+
} else {
|
|
103
|
+
setActionError(errorMessage(res.error as never, "Action failed."));
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const setRole = (m: Member, role: TenantRole) =>
|
|
108
|
+
runAction(`role-${m.id}`, () =>
|
|
109
|
+
client.call(updateMemberRole, { memberId: m.id, role, organizationId })
|
|
110
|
+
);
|
|
111
|
+
const remove = (m: Member) =>
|
|
112
|
+
runAction(`remove-${m.id}`, () =>
|
|
113
|
+
client.call(removeMember, { memberId: m.id, organizationId })
|
|
114
|
+
);
|
|
115
|
+
const assignRole = (m: Member, roleId: string) =>
|
|
116
|
+
runAction(`assign-${m.id}`, () =>
|
|
117
|
+
client.call(assignMemberRole, { orgId: organizationId, memberId: m.id, roleId })
|
|
118
|
+
);
|
|
119
|
+
const unassignRole = (m: Member, roleId: string) =>
|
|
120
|
+
runAction(`unassign-${m.id}-${roleId}`, () =>
|
|
121
|
+
client.call(removeMemberRole, { orgId: organizationId, memberId: m.id, roleId })
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
// Custom-role definitions are fetched lazily (first menu open) —
|
|
125
|
+
// regular members never hit the admin-only roles endpoint.
|
|
126
|
+
const [allRoles, setAllRoles] = useState<RoleRow[] | null>(null);
|
|
127
|
+
const rolesRequested = useRef(false);
|
|
128
|
+
const loadRoles = async () => {
|
|
129
|
+
if (rolesRequested.current) return;
|
|
130
|
+
rolesRequested.current = true;
|
|
131
|
+
const res = await client.call(listTenantRoles, { orgId: organizationId });
|
|
132
|
+
if (res.ok) setAllRoles(res.data.roles);
|
|
133
|
+
else rolesRequested.current = false;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<Card>
|
|
138
|
+
<CardHeader className="space-y-0">
|
|
139
|
+
<div className="flex items-center justify-between gap-3">
|
|
140
|
+
<div>
|
|
141
|
+
<CardTitle className="flex items-center gap-2">
|
|
142
|
+
<Users className="size-4 text-muted-foreground" />
|
|
143
|
+
Members
|
|
144
|
+
</CardTitle>
|
|
145
|
+
<CardDescription>
|
|
146
|
+
{data
|
|
147
|
+
? `${data.members.length} member${data.members.length === 1 ? "" : "s"} in this tenant`
|
|
148
|
+
: "Manage members and tenant-level roles."}
|
|
149
|
+
</CardDescription>
|
|
150
|
+
</div>
|
|
151
|
+
</div>
|
|
152
|
+
</CardHeader>
|
|
153
|
+
<CardContent className="grid gap-5">
|
|
154
|
+
{error ? (
|
|
155
|
+
<Alert variant="destructive">
|
|
156
|
+
<AlertCircle />
|
|
157
|
+
<AlertDescription>{errorMessage(error)}</AlertDescription>
|
|
158
|
+
</Alert>
|
|
159
|
+
) : null}
|
|
160
|
+
{actionError ? (
|
|
161
|
+
<Alert variant="destructive">
|
|
162
|
+
<AlertCircle />
|
|
163
|
+
<AlertDescription>{actionError}</AlertDescription>
|
|
164
|
+
</Alert>
|
|
165
|
+
) : null}
|
|
166
|
+
|
|
167
|
+
{canManage && data ? (
|
|
168
|
+
<>
|
|
169
|
+
{userPicker ? (
|
|
170
|
+
<PickUserRow
|
|
171
|
+
organizationId={organizationId}
|
|
172
|
+
memberUserIds={data.members.map((m) => m.userId)}
|
|
173
|
+
onChanged={refresh}
|
|
174
|
+
/>
|
|
175
|
+
) : null}
|
|
176
|
+
<AddOrInviteRow organizationId={organizationId} onChanged={refresh} />
|
|
177
|
+
</>
|
|
178
|
+
) : null}
|
|
179
|
+
|
|
180
|
+
<div className="overflow-hidden rounded-lg border">
|
|
181
|
+
<Table>
|
|
182
|
+
<TableHeader>
|
|
183
|
+
<TableRow>
|
|
184
|
+
<TableHead>Member</TableHead>
|
|
185
|
+
<TableHead>Role</TableHead>
|
|
186
|
+
<TableHead>Custom roles</TableHead>
|
|
187
|
+
<TableHead>Joined</TableHead>
|
|
188
|
+
{canManage && <TableHead className="text-right">Actions</TableHead>}
|
|
189
|
+
</TableRow>
|
|
190
|
+
</TableHeader>
|
|
191
|
+
<TableBody>
|
|
192
|
+
{loading ? (
|
|
193
|
+
Array.from({ length: 3 }).map((_, i) => (
|
|
194
|
+
<TableRow key={`sk-${i}`}>
|
|
195
|
+
<TableCell><Skeleton className="h-5 w-40" /></TableCell>
|
|
196
|
+
<TableCell><Skeleton className="h-5 w-20" /></TableCell>
|
|
197
|
+
<TableCell><Skeleton className="h-5 w-28" /></TableCell>
|
|
198
|
+
<TableCell><Skeleton className="h-5 w-24" /></TableCell>
|
|
199
|
+
{canManage && (
|
|
200
|
+
<TableCell className="text-right"><Skeleton className="ml-auto h-8 w-8 rounded" /></TableCell>
|
|
201
|
+
)}
|
|
202
|
+
</TableRow>
|
|
203
|
+
))
|
|
204
|
+
) : !data || data.members.length === 0 ? (
|
|
205
|
+
<TableRow>
|
|
206
|
+
<TableCell colSpan={canManage ? 5 : 4} className="p-0">
|
|
207
|
+
<EmptyState
|
|
208
|
+
icon={Users}
|
|
209
|
+
title="No members yet"
|
|
210
|
+
description="Invite teammates to get started."
|
|
211
|
+
className="border-0 rounded-none bg-transparent"
|
|
212
|
+
/>
|
|
213
|
+
</TableCell>
|
|
214
|
+
</TableRow>
|
|
215
|
+
) : (
|
|
216
|
+
data.members.map((m) => (
|
|
217
|
+
<TableRow key={m.id}>
|
|
218
|
+
<TableCell>
|
|
219
|
+
<div className="flex items-center gap-3">
|
|
220
|
+
<Avatar>
|
|
221
|
+
<AvatarFallback>
|
|
222
|
+
{initialsOf(m.user?.name, m.user?.email ?? m.userId)}
|
|
223
|
+
</AvatarFallback>
|
|
224
|
+
</Avatar>
|
|
225
|
+
<div className="grid leading-tight">
|
|
226
|
+
<span className="text-sm font-medium">
|
|
227
|
+
{m.user?.name || m.user?.email || m.userId}
|
|
228
|
+
</span>
|
|
229
|
+
<span className="text-xs text-muted-foreground">
|
|
230
|
+
{m.user?.email}
|
|
231
|
+
</span>
|
|
232
|
+
</div>
|
|
233
|
+
</div>
|
|
234
|
+
</TableCell>
|
|
235
|
+
<TableCell>
|
|
236
|
+
{canManage ? (
|
|
237
|
+
<TenantRoleSelect
|
|
238
|
+
value={m.role}
|
|
239
|
+
onChange={(next) => setRole(m, next)}
|
|
240
|
+
disabled={busy === `role-${m.id}`}
|
|
241
|
+
/>
|
|
242
|
+
) : (
|
|
243
|
+
<TenantRoleBadge role={m.role} />
|
|
244
|
+
)}
|
|
245
|
+
</TableCell>
|
|
246
|
+
<TableCell>
|
|
247
|
+
<MemberCustomRoles
|
|
248
|
+
member={m}
|
|
249
|
+
canManage={canManage}
|
|
250
|
+
allRoles={allRoles}
|
|
251
|
+
onOpenAssign={loadRoles}
|
|
252
|
+
onAssign={(roleId) => assignRole(m, roleId)}
|
|
253
|
+
onUnassign={(roleId) => unassignRole(m, roleId)}
|
|
254
|
+
busy={busy?.startsWith(`assign-${m.id}`) || busy?.startsWith(`unassign-${m.id}`) || false}
|
|
255
|
+
/>
|
|
256
|
+
</TableCell>
|
|
257
|
+
<TableCell
|
|
258
|
+
title={m.createdAt}
|
|
259
|
+
className="text-sm text-muted-foreground"
|
|
260
|
+
>
|
|
261
|
+
{formatRelative(m.createdAt)}
|
|
262
|
+
</TableCell>
|
|
263
|
+
{canManage && (
|
|
264
|
+
<TableCell className="text-right">
|
|
265
|
+
<Button
|
|
266
|
+
variant="ghost"
|
|
267
|
+
size="icon"
|
|
268
|
+
onClick={() => remove(m)}
|
|
269
|
+
loading={busy === `remove-${m.id}`}
|
|
270
|
+
aria-label="Remove member"
|
|
271
|
+
>
|
|
272
|
+
<Trash2 className="text-destructive" />
|
|
273
|
+
</Button>
|
|
274
|
+
</TableCell>
|
|
275
|
+
)}
|
|
276
|
+
</TableRow>
|
|
277
|
+
))
|
|
278
|
+
)}
|
|
279
|
+
</TableBody>
|
|
280
|
+
</Table>
|
|
281
|
+
</div>
|
|
282
|
+
|
|
283
|
+
{data ? (
|
|
284
|
+
<PendingInvitations invitations={data.invitations ?? []} />
|
|
285
|
+
) : null}
|
|
286
|
+
</CardContent>
|
|
287
|
+
</Card>
|
|
288
|
+
);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Badges for a member's assigned custom roles, with an assign menu and
|
|
293
|
+
* per-badge remove when the viewer can manage the tenant.
|
|
294
|
+
*/
|
|
295
|
+
const MemberCustomRoles = ({
|
|
296
|
+
member,
|
|
297
|
+
canManage,
|
|
298
|
+
allRoles,
|
|
299
|
+
onOpenAssign,
|
|
300
|
+
onAssign,
|
|
301
|
+
onUnassign,
|
|
302
|
+
busy,
|
|
303
|
+
}: {
|
|
304
|
+
member: Member;
|
|
305
|
+
canManage: boolean;
|
|
306
|
+
allRoles: RoleRow[] | null;
|
|
307
|
+
onOpenAssign: () => void;
|
|
308
|
+
onAssign: (roleId: string) => void;
|
|
309
|
+
onUnassign: (roleId: string) => void;
|
|
310
|
+
busy: boolean;
|
|
311
|
+
}) => {
|
|
312
|
+
const assigned = member.roles ?? [];
|
|
313
|
+
const assignedIds = new Set(assigned.map((r) => r.id));
|
|
314
|
+
const available = (allRoles ?? []).filter((r) => !assignedIds.has(r.id));
|
|
315
|
+
|
|
316
|
+
return (
|
|
317
|
+
<div className="flex flex-wrap items-center gap-1">
|
|
318
|
+
{assigned.length === 0 && !canManage ? (
|
|
319
|
+
<span className="text-sm text-muted-foreground">{"—"}</span>
|
|
320
|
+
) : (
|
|
321
|
+
assigned.map((r) => (
|
|
322
|
+
<Badge key={r.id} variant="secondary" className="gap-1 font-mono text-xs">
|
|
323
|
+
{r.name}
|
|
324
|
+
{canManage && (
|
|
325
|
+
<button
|
|
326
|
+
type="button"
|
|
327
|
+
onClick={() => onUnassign(r.id)}
|
|
328
|
+
disabled={busy}
|
|
329
|
+
aria-label={`Remove role ${r.name}`}
|
|
330
|
+
className="rounded-full opacity-60 transition-opacity hover:opacity-100 disabled:pointer-events-none"
|
|
331
|
+
>
|
|
332
|
+
<X className="size-3" />
|
|
333
|
+
</button>
|
|
334
|
+
)}
|
|
335
|
+
</Badge>
|
|
336
|
+
))
|
|
337
|
+
)}
|
|
338
|
+
{canManage && (
|
|
339
|
+
<DropdownMenu>
|
|
340
|
+
<DropdownMenuTrigger asChild>
|
|
341
|
+
<Button
|
|
342
|
+
variant="ghost"
|
|
343
|
+
size="icon"
|
|
344
|
+
className="size-6"
|
|
345
|
+
onClick={onOpenAssign}
|
|
346
|
+
disabled={busy}
|
|
347
|
+
aria-label="Assign role"
|
|
348
|
+
>
|
|
349
|
+
<Plus className="size-3.5" />
|
|
350
|
+
</Button>
|
|
351
|
+
</DropdownMenuTrigger>
|
|
352
|
+
<DropdownMenuContent align="start">
|
|
353
|
+
<DropdownMenuLabel>Assign role</DropdownMenuLabel>
|
|
354
|
+
{allRoles === null ? (
|
|
355
|
+
<DropdownMenuItem disabled>Loading roles…</DropdownMenuItem>
|
|
356
|
+
) : available.length === 0 ? (
|
|
357
|
+
<DropdownMenuItem disabled>
|
|
358
|
+
{allRoles.length === 0
|
|
359
|
+
? "No roles defined — create one in the Roles tab"
|
|
360
|
+
: "All roles assigned"}
|
|
361
|
+
</DropdownMenuItem>
|
|
362
|
+
) : (
|
|
363
|
+
available.map((r) => (
|
|
364
|
+
<DropdownMenuItem key={r.id} onSelect={() => onAssign(r.id)}>
|
|
365
|
+
{r.name}
|
|
366
|
+
</DropdownMenuItem>
|
|
367
|
+
))
|
|
368
|
+
)}
|
|
369
|
+
</DropdownMenuContent>
|
|
370
|
+
</DropdownMenu>
|
|
371
|
+
)}
|
|
372
|
+
</div>
|
|
373
|
+
);
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const AddOrInviteRow = ({
|
|
377
|
+
organizationId,
|
|
378
|
+
onChanged,
|
|
379
|
+
}: {
|
|
380
|
+
organizationId: string;
|
|
381
|
+
onChanged: () => void;
|
|
382
|
+
}) => {
|
|
383
|
+
const client = useUmClient();
|
|
384
|
+
const [email, setEmail] = useState("");
|
|
385
|
+
const [role, setRole] = useState<TenantRole>("member");
|
|
386
|
+
const [busy, setBusy] = useState<"add" | "invite" | null>(null);
|
|
387
|
+
const [error, setError] = useState<string | null>(null);
|
|
388
|
+
|
|
389
|
+
// "Add" assigns an existing user directly; "Invite" emails a join
|
|
390
|
+
// link for people without an account yet.
|
|
391
|
+
const run = async (kind: "add" | "invite") => {
|
|
392
|
+
setBusy(kind);
|
|
393
|
+
setError(null);
|
|
394
|
+
const input = { email: email.trim(), role, organizationId };
|
|
395
|
+
const res =
|
|
396
|
+
kind === "add"
|
|
397
|
+
? await client.call(addMember, input)
|
|
398
|
+
: await client.call(inviteMember, input);
|
|
399
|
+
setBusy(null);
|
|
400
|
+
if (res.ok) {
|
|
401
|
+
setEmail("");
|
|
402
|
+
onChanged();
|
|
403
|
+
} else {
|
|
404
|
+
setError(
|
|
405
|
+
errorMessage(
|
|
406
|
+
res.error as never,
|
|
407
|
+
kind === "add" ? "Couldn't add member." : "Couldn't send invitation."
|
|
408
|
+
)
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const submit = (e: FormEvent) => {
|
|
414
|
+
e.preventDefault();
|
|
415
|
+
void run("add");
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
return (
|
|
419
|
+
<form
|
|
420
|
+
onSubmit={submit}
|
|
421
|
+
className="grid items-end gap-3 rounded-lg border border-dashed bg-muted/30 p-4 sm:grid-cols-[1fr_auto_auto_auto]"
|
|
422
|
+
>
|
|
423
|
+
<Field label="Invite someone" htmlFor="invite-email">
|
|
424
|
+
<div className="relative">
|
|
425
|
+
<Mail className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
|
|
426
|
+
<Input
|
|
427
|
+
id="invite-email"
|
|
428
|
+
type="email"
|
|
429
|
+
required
|
|
430
|
+
value={email}
|
|
431
|
+
onChange={(e) => setEmail(e.target.value)}
|
|
432
|
+
className="pl-9"
|
|
433
|
+
placeholder="teammate@example.com"
|
|
434
|
+
/>
|
|
435
|
+
</div>
|
|
436
|
+
</Field>
|
|
437
|
+
<Field label="Role" htmlFor="invite-role">
|
|
438
|
+
<TenantRoleSelect value={role} onChange={setRole} />
|
|
439
|
+
</Field>
|
|
440
|
+
<Button type="submit" loading={busy === "add"} disabled={!email.trim() || busy !== null}>
|
|
441
|
+
<UserPlus />
|
|
442
|
+
Add member
|
|
443
|
+
</Button>
|
|
444
|
+
<Button
|
|
445
|
+
type="button"
|
|
446
|
+
variant="outline"
|
|
447
|
+
loading={busy === "invite"}
|
|
448
|
+
disabled={!email.trim() || busy !== null}
|
|
449
|
+
onClick={() => void run("invite")}
|
|
450
|
+
>
|
|
451
|
+
<Mail />
|
|
452
|
+
Send invite
|
|
453
|
+
</Button>
|
|
454
|
+
{error ? (
|
|
455
|
+
<div className="sm:col-span-4">
|
|
456
|
+
<Alert variant="destructive">
|
|
457
|
+
<AlertCircle />
|
|
458
|
+
<AlertDescription>{error}</AlertDescription>
|
|
459
|
+
</Alert>
|
|
460
|
+
</div>
|
|
461
|
+
) : null}
|
|
462
|
+
</form>
|
|
463
|
+
);
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Superadmin variant of the add row: pick any existing user from a
|
|
468
|
+
* dropdown and add them directly — no invitation round-trip.
|
|
469
|
+
*/
|
|
470
|
+
const PickUserRow = ({
|
|
471
|
+
organizationId,
|
|
472
|
+
memberUserIds,
|
|
473
|
+
onChanged,
|
|
474
|
+
}: {
|
|
475
|
+
organizationId: string;
|
|
476
|
+
memberUserIds: string[];
|
|
477
|
+
onChanged: () => void;
|
|
478
|
+
}) => {
|
|
479
|
+
const client = useUmClient();
|
|
480
|
+
const users = useCall(adminListUsers, { limit: 500 });
|
|
481
|
+
const [userId, setUserId] = useState("");
|
|
482
|
+
const [role, setRole] = useState<TenantRole>("member");
|
|
483
|
+
const [loading, setLoading] = useState(false);
|
|
484
|
+
const [error, setError] = useState<string | null>(null);
|
|
485
|
+
|
|
486
|
+
const candidates = useMemo(() => {
|
|
487
|
+
const members = new Set(memberUserIds);
|
|
488
|
+
return (users.data?.rows ?? []).filter(
|
|
489
|
+
(u) => !members.has(u.id) && u.type !== "service"
|
|
490
|
+
);
|
|
491
|
+
}, [users.data, memberUserIds]);
|
|
492
|
+
|
|
493
|
+
const submit = async (e: FormEvent) => {
|
|
494
|
+
e.preventDefault();
|
|
495
|
+
if (!userId) return;
|
|
496
|
+
setLoading(true);
|
|
497
|
+
setError(null);
|
|
498
|
+
const res = await client.call(addMember, { organizationId, userId, role });
|
|
499
|
+
setLoading(false);
|
|
500
|
+
if (res.ok) {
|
|
501
|
+
setUserId("");
|
|
502
|
+
setRole("member");
|
|
503
|
+
onChanged();
|
|
504
|
+
} else {
|
|
505
|
+
setError(errorMessage(res.error, "Couldn't add member."));
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
return (
|
|
510
|
+
<form
|
|
511
|
+
onSubmit={submit}
|
|
512
|
+
className="grid items-end gap-3 rounded-lg border border-dashed bg-muted/30 p-4 sm:grid-cols-[1fr_auto_auto]"
|
|
513
|
+
>
|
|
514
|
+
<Field label="Add an existing user" htmlFor="pick-user">
|
|
515
|
+
<Select value={userId} onValueChange={setUserId}>
|
|
516
|
+
<SelectTrigger id="pick-user" className="h-9">
|
|
517
|
+
<SelectValue
|
|
518
|
+
placeholder={users.loading ? "Loading users…" : "Pick a user…"}
|
|
519
|
+
/>
|
|
520
|
+
</SelectTrigger>
|
|
521
|
+
<SelectContent>
|
|
522
|
+
{candidates.map((u) => (
|
|
523
|
+
<SelectItem key={u.id} value={u.id}>
|
|
524
|
+
{u.name ? `${u.name} (${u.email})` : u.email}
|
|
525
|
+
</SelectItem>
|
|
526
|
+
))}
|
|
527
|
+
</SelectContent>
|
|
528
|
+
</Select>
|
|
529
|
+
</Field>
|
|
530
|
+
<Field label="Role" htmlFor="pick-user-role">
|
|
531
|
+
<TenantRoleSelect value={role} onChange={setRole} />
|
|
532
|
+
</Field>
|
|
533
|
+
<Button type="submit" loading={loading} disabled={!userId || loading}>
|
|
534
|
+
<UserPlus />
|
|
535
|
+
Add member
|
|
536
|
+
</Button>
|
|
537
|
+
{error ? (
|
|
538
|
+
<div className="sm:col-span-3">
|
|
539
|
+
<Alert variant="destructive">
|
|
540
|
+
<AlertCircle />
|
|
541
|
+
<AlertDescription>{error}</AlertDescription>
|
|
542
|
+
</Alert>
|
|
543
|
+
</div>
|
|
544
|
+
) : null}
|
|
545
|
+
</form>
|
|
546
|
+
);
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const PendingInvitations = ({ invitations }: { invitations: Invitation[] }) => {
|
|
550
|
+
const open = invitations.filter(
|
|
551
|
+
(i) => i.status === "pending" || i.status === "expired"
|
|
552
|
+
);
|
|
553
|
+
if (open.length === 0) return null;
|
|
554
|
+
return (
|
|
555
|
+
<div className="grid gap-2">
|
|
556
|
+
<p className="text-sm font-medium text-muted-foreground">
|
|
557
|
+
Pending invitations
|
|
558
|
+
</p>
|
|
559
|
+
<div className="overflow-hidden rounded-lg border">
|
|
560
|
+
<Table>
|
|
561
|
+
<TableBody>
|
|
562
|
+
{open.map((i) => (
|
|
563
|
+
<TableRow key={i.id}>
|
|
564
|
+
<TableCell>
|
|
565
|
+
<div className="flex items-center gap-2">
|
|
566
|
+
<Mail className="size-4 text-muted-foreground" />
|
|
567
|
+
<span className="text-sm">{i.email}</span>
|
|
568
|
+
</div>
|
|
569
|
+
</TableCell>
|
|
570
|
+
<TableCell>
|
|
571
|
+
<TenantRoleBadge role={i.role} />
|
|
572
|
+
</TableCell>
|
|
573
|
+
<TableCell className="text-right text-xs text-muted-foreground">
|
|
574
|
+
{i.status === "expired"
|
|
575
|
+
? "Expired"
|
|
576
|
+
: i.expiresAt
|
|
577
|
+
? `Expires ${formatRelative(i.expiresAt)}`
|
|
578
|
+
: "Pending"}
|
|
579
|
+
</TableCell>
|
|
580
|
+
</TableRow>
|
|
581
|
+
))}
|
|
582
|
+
</TableBody>
|
|
583
|
+
</Table>
|
|
584
|
+
</div>
|
|
585
|
+
</div>
|
|
586
|
+
);
|
|
587
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { afterEach, describe, expect, mock, test } from "bun:test";
|
|
3
|
+
|
|
4
|
+
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
5
|
+
|
|
6
|
+
import { createClient } from "@nexys/user-management-client";
|
|
7
|
+
|
|
8
|
+
import { AuthProvider } from "../../context/auth-context.js";
|
|
9
|
+
import { TenantRoles } from "./tenant-roles.js";
|
|
10
|
+
|
|
11
|
+
const ROLES = [
|
|
12
|
+
{
|
|
13
|
+
id: "r1",
|
|
14
|
+
name: "billing-admin",
|
|
15
|
+
description: "Manages billing",
|
|
16
|
+
permissions: ["invoices:read", "invoices:write"],
|
|
17
|
+
createdAt: "2026-01-01T00:00:00Z",
|
|
18
|
+
updatedAt: "2026-01-01T00:00:00Z",
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
document.body.innerHTML = "";
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("<TenantRoles />", () => {
|
|
27
|
+
test("renders roles with their permission badges", async () => {
|
|
28
|
+
const fetcher = mock(async (input: RequestInfo | URL) => {
|
|
29
|
+
const url = input.toString();
|
|
30
|
+
if (url.endsWith("/api/me")) {
|
|
31
|
+
return new Response(JSON.stringify({ code: "unauth" }), { status: 401 });
|
|
32
|
+
}
|
|
33
|
+
if (url.endsWith("/api/tenants/org-1/roles")) {
|
|
34
|
+
return new Response(JSON.stringify({ roles: ROLES }), {
|
|
35
|
+
status: 200,
|
|
36
|
+
headers: { "content-type": "application/json" },
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return new Response("nope", { status: 404 });
|
|
40
|
+
});
|
|
41
|
+
const client = createClient({
|
|
42
|
+
baseUrl: "https://auth.example.com",
|
|
43
|
+
fetch: fetcher as unknown as typeof fetch,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
render(
|
|
47
|
+
<AuthProvider client={client} refreshIntervalMs={0}>
|
|
48
|
+
<TenantRoles organizationId="org-1" />
|
|
49
|
+
</AuthProvider>
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
await waitFor(() => {
|
|
53
|
+
expect(screen.getByText("billing-admin")).toBeTruthy();
|
|
54
|
+
});
|
|
55
|
+
expect(screen.getByText("invoices:read")).toBeTruthy();
|
|
56
|
+
expect(screen.getByText("invoices:write")).toBeTruthy();
|
|
57
|
+
expect(screen.getByText("Manages billing")).toBeTruthy();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("creates a role with comma-separated permissions parsed to a list", async () => {
|
|
61
|
+
let createdBody: unknown = null;
|
|
62
|
+
const fetcher = mock(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
63
|
+
const url = input.toString();
|
|
64
|
+
if (url.endsWith("/api/me")) {
|
|
65
|
+
return new Response(JSON.stringify({ code: "unauth" }), { status: 401 });
|
|
66
|
+
}
|
|
67
|
+
if (url.endsWith("/api/tenants/org-1/roles") && init?.method === "POST") {
|
|
68
|
+
createdBody = JSON.parse(String(init.body));
|
|
69
|
+
return new Response(
|
|
70
|
+
JSON.stringify({ ...ROLES[0], id: "r2", name: "support" }),
|
|
71
|
+
{ status: 201, headers: { "content-type": "application/json" } }
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (url.endsWith("/api/tenants/org-1/roles")) {
|
|
75
|
+
return new Response(JSON.stringify({ roles: [] }), {
|
|
76
|
+
status: 200,
|
|
77
|
+
headers: { "content-type": "application/json" },
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return new Response("nope", { status: 404 });
|
|
81
|
+
});
|
|
82
|
+
const client = createClient({
|
|
83
|
+
baseUrl: "https://auth.example.com",
|
|
84
|
+
fetch: fetcher as unknown as typeof fetch,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
render(
|
|
88
|
+
<AuthProvider client={client} refreshIntervalMs={0}>
|
|
89
|
+
<TenantRoles organizationId="org-1" canManage />
|
|
90
|
+
</AuthProvider>
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
fireEvent.change(screen.getByLabelText(/role name/i), {
|
|
94
|
+
target: { value: "support" },
|
|
95
|
+
});
|
|
96
|
+
fireEvent.change(screen.getByLabelText(/permissions/i), {
|
|
97
|
+
target: { value: "tickets:read, tickets:write" },
|
|
98
|
+
});
|
|
99
|
+
fireEvent.click(screen.getByRole("button", { name: /add role/i }));
|
|
100
|
+
|
|
101
|
+
await waitFor(() => expect(createdBody).not.toBeNull());
|
|
102
|
+
expect(createdBody).toEqual({
|
|
103
|
+
name: "support",
|
|
104
|
+
permissions: ["tickets:read", "tickets:write"],
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|