Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 2x 2x 2x 2x 2x 26x 68x 68x 68x 4x 4x 1x 1x 69x 69x 69x 2x 2x 69x 1x 68x 48x 1x 1x 14x 5x | "use client";
/**
* SuperAdminManagement - Admin interface for managing Super Admins
* @see /tests/features/authentication/super-admin-authorization.feature
* @see JCN-8
*
* Super Admin management features:
* - View list of all Super Admins
* - Create new Super Admin (sends invitation)
* - Revoke Super Admin privileges
* - Cannot revoke own access or last admin
*/
import { useState } from "react";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Badge } from "@/components/ui/badge";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import {
Loader2,
AlertCircle,
Shield,
UserPlus,
UserMinus,
CheckCircle2,
Info,
} from "lucide-react";
export interface SuperAdmin {
id: string;
email: string;
status: "Active" | "Pending";
createdAt: string;
isCurrentUser?: boolean;
}
interface SuperAdminListProps {
admins: SuperAdmin[];
currentUserEmail?: string;
onRevoke?: (admin: SuperAdmin) => void;
isLoading?: boolean;
isOnlyAdmin?: boolean;
}
export function SuperAdminList({
admins,
currentUserEmail,
onRevoke,
isLoading,
isOnlyAdmin,
}: SuperAdminListProps) {
const [revokeTarget, setRevokeTarget] = useState<SuperAdmin | null>(null);
const [isRevoking, setIsRevoking] = useState(false);
const handleRevoke = async () => {
Iif (!revokeTarget) return;
setIsRevoking(true);
await onRevoke?.(revokeTarget);
setIsRevoking(false);
setRevokeTarget(null);
};
return (
<Card className="w-full" data-testid="super-admin-list">
<CardHeader>
<div className="flex items-center gap-2">
<Shield className="h-6 w-6 text-primary" />
<CardTitle>Super Administrators</CardTitle>
</div>
<CardDescription>
Manage platform-level administrator access
</CardDescription>
</CardHeader>
<CardContent>
{isOnlyAdmin && (
<Alert className="mb-4">
<Info className="h-4 w-4" />
<AlertTitle>Single Administrator</AlertTitle>
<AlertDescription>
At least one Super Admin must exist. Add another admin before
revoking access.
</AlertDescription>
</Alert>
)}
<Table>
<TableHeader>
<TableRow>
<TableHead>Email</TableHead>
<TableHead>Status</TableHead>
<TableHead>Created</TableHead>
<TableHead className="text-right">Actions</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{isLoading ? (
<TableRow>
<TableCell colSpan={4} className="text-center py-8">
<Loader2 className="h-6 w-6 animate-spin mx-auto" />
</TableCell>
</TableRow>
) : admins.length === 0 ? (
<TableRow>
<TableCell
colSpan={4}
className="text-center py-8 text-muted-foreground"
>
No Super Admins found
</TableCell>
</TableRow>
) : (
admins.map((admin) => {
const isCurrentUser = admin.email === currentUserEmail;
const canRevoke = !isCurrentUser && !isOnlyAdmin;
return (
<TableRow key={admin.id}>
<TableCell className="font-medium">
{admin.email}
{isCurrentUser && (
<Badge variant="outline" className="ml-2">
You
</Badge>
)}
</TableCell>
<TableCell>
<Badge
variant={
admin.status === "Active" ? "default" : "secondary"
}
>
{admin.status}
</Badge>
</TableCell>
<TableCell className="text-muted-foreground">
{admin.createdAt}
</TableCell>
<TableCell className="text-right">
<Dialog
open={revokeTarget?.id === admin.id}
onOpenChange={(open) =>
!open && setRevokeTarget(null)
}
>
<DialogTrigger asChild>
<Button
variant="destructive"
size="sm"
disabled={!canRevoke}
onClick={() => setRevokeTarget(admin)}
title={
isCurrentUser
? "Cannot revoke your own access"
: isOnlyAdmin
? "At least one Super Admin must exist"
: "Revoke admin access"
}
data-testid={`revoke-admin-${admin.email.replace("@", "-at-")}`}
>
<UserMinus className="h-4 w-4 mr-1" />
Revoke
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Revoke Super Admin Access</DialogTitle>
<DialogDescription>
Are you sure you want to revoke Super Admin access
for <strong>{admin.email}</strong>? They will no
longer be able to manage tenants or platform
settings.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button
variant="outline"
onClick={() => setRevokeTarget(null)}
>
Cancel
</Button>
<Button
variant="destructive"
onClick={handleRevoke}
disabled={isRevoking}
>
{isRevoking && (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
)}
Revoke Access
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</TableCell>
</TableRow>
);
})
)}
</TableBody>
</Table>
</CardContent>
</Card>
);
}
interface AddSuperAdminFormProps {
onSubmit?: (email: string) => void;
isLoading?: boolean;
error?: string;
success?: boolean;
}
export function AddSuperAdminForm({
onSubmit,
isLoading,
error,
success,
}: AddSuperAdminFormProps) {
const [email, setEmail] = useState("");
const [isOpen, setIsOpen] = useState(false);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
onSubmit?.(email);
};
if (success) {
return (
<Card className="w-full max-w-md">
<CardHeader className="text-center">
<CheckCircle2 className="h-12 w-12 text-green-500 mx-auto mb-4" />
<CardTitle>Invitation Sent</CardTitle>
<CardDescription>
An invitation email has been sent to the new Super Admin.
</CardDescription>
</CardHeader>
</Card>
);
}
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button data-testid="add-super-admin-button">
<UserPlus className="h-4 w-4 mr-2" />
Add Super Admin
</Button>
</DialogTrigger>
<DialogContent>
<form onSubmit={handleSubmit}>
<DialogHeader>
<DialogTitle>Add New Super Admin</DialogTitle>
<DialogDescription>
Send an invitation to create a new Super Admin account. They will
receive an email to set up their credentials.
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<div className="space-y-2">
<Label htmlFor="admin-email">Email Address</Label>
<Input
id="admin-email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="admin@platform.com"
required
data-testid="add-admin-email-input"
/>
</div>
{error && (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertTitle>Error</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setIsOpen(false)}
>
Cancel
</Button>
<Button type="submit" disabled={isLoading || !email} data-testid="add-admin-submit">
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Send Invitation
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}
interface SuperAdminManagementPageProps {
admins: SuperAdmin[];
currentUserEmail: string;
onAddAdmin?: (email: string) => void;
onRevokeAdmin?: (admin: SuperAdmin) => void;
isLoading?: boolean;
addError?: string;
addSuccess?: boolean;
}
export function SuperAdminManagementPage({
admins,
currentUserEmail,
onAddAdmin,
onRevokeAdmin,
isLoading,
addError,
addSuccess,
}: SuperAdminManagementPageProps) {
const isOnlyAdmin = admins.filter((a) => a.status === "Active").length <= 1;
return (
<div className="space-y-6 w-full max-w-4xl mx-auto" data-testid="admin-dashboard">
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-bold">Platform Administration</h1>
<p className="text-muted-foreground">
Manage Super Admin accounts and platform access
</p>
</div>
<AddSuperAdminForm
onSubmit={onAddAdmin}
isLoading={isLoading}
error={addError}
success={addSuccess}
/>
</div>
<SuperAdminList
admins={admins}
currentUserEmail={currentUserEmail}
onRevoke={onRevokeAdmin}
isLoading={isLoading}
isOnlyAdmin={isOnlyAdmin}
/>
</div>
);
}
|