Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 43,320 Bytes
5c7468a f229803 221809c f229803 5c7468a 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c 122d3d1 48b272d 122d3d1 221809c 122d3d1 48b272d 122d3d1 221809c 122d3d1 221809c 122d3d1 221809c 122d3d1 221809c 122d3d1 48b272d 122d3d1 48b272d 122d3d1 48b272d 122d3d1 221809c 122d3d1 221809c 122d3d1 48b272d 122d3d1 221809c 122d3d1 221809c 122d3d1 221809c 122d3d1 221809c 122d3d1 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c 5c7468a 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c 122d3d1 f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c a3b69b8 221809c 8c25550 221809c c2447a4 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 221809c f229803 | 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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 | /* eslint-disable react-hooks/set-state-in-effect, react-hooks/exhaustive-deps, react-hooks/immutability */
import { useState, useEffect, useCallback } from 'react';
import { useAuth } from '../components/AuthContext';
import { CustomModal } from '../components/CustomModal';
import { useNavigate } from 'react-router-dom';
import {
Shield,
Users,
Building2,
Activity,
ShieldAlert,
Plus,
Edit3,
Key,
Search,
RefreshCw,
Lock,
Unlock,
CheckCircle2,
AlertCircle,
Sliders,
FileText,
BarChart3
} from 'lucide-react';
import { ErrorBoundary } from '../components/ErrorBoundary';
const AdminPageContent = () => {
const { token } = useAuth();
const navigate = useNavigate();
const [activeTab, setActiveTab] = useState('members'); // 'members', 'orgs', 'scan_access', 'audit'
const [users, setUsers] = useState([]);
const [organizations, setOrganizations] = useState([]);
const [scanAccess, setScanAccess] = useState([]);
const [auditLogs, setAuditLogs] = useState([]);
const [metrics, setMetrics] = useState(null);
const [loading, setLoading] = useState(true);
const [syncing, setSyncing] = useState(false);
const [error, setError] = useState('');
const [message, setMessage] = useState('');
// Search & Filter States
const [userSearch, setUserSearch] = useState('');
const [userRoleFilter, setUserRoleFilter] = useState('all');
const [userOrgFilter, setUserOrgFilter] = useState('all');
const [orgSearch, setOrgSearch] = useState('');
// Sorting States
const [sortUserCol, setSortUserCol] = useState('Email');
const [sortUserDir, setSortUserDir] = useState('asc');
const [sortOrgCol, setSortOrgCol] = useState('Tenant Name');
const [sortOrgDir, setSortOrgDir] = useState('asc');
// Modal States
const [promptModal, setPromptModal] = useState({ isOpen: false, title: '', desc: '', inputs: [], onConfirm: null });
const [promptValues, setPromptValues] = useState({});
const closePrompt = () => {
setPromptModal({ ...promptModal, isOpen: false });
setPromptValues({});
};
const handlePromptChange = (key, val) => {
setPromptValues(prev => ({ ...prev, [key]: val }));
};
const fetchAllData = useCallback(async () => {
setSyncing(true);
try {
const activeToken = localStorage.getItem('wss_token') || token;
if (!activeToken) {
setLoading(false);
setSyncing(false);
return;
}
const [statsRes, accessRes] = await Promise.all([
fetch('/api/global-stats', { headers: { 'Authorization': `Bearer ${activeToken}` } }),
fetch('/api/admin/scan-access', { headers: { 'Authorization': `Bearer ${activeToken}` } })
]);
if (statsRes.ok) {
const statsData = await statsRes.json();
setMetrics(statsData.metrics || null);
setOrganizations(statsData.tenants || []);
setUsers(statsData.users || []);
setAuditLogs(statsData.audit_logs || []);
}
if (accessRes.ok) {
const accessData = await accessRes.json();
setScanAccess(accessData.controls || []);
}
} catch (err) {
console.error('Failed to load admin data:', err);
setError('Could not connect to backend server.');
} finally {
setLoading(false);
setSyncing(false);
}
}, [token]);
useEffect(() => {
fetchAllData();
const interval = setInterval(fetchAllData, 5000);
return () => clearInterval(interval);
}, [fetchAllData]);
// User Actions
const handleRoleChange = async (userId, newRole) => {
setMessage('');
setError('');
try {
const activeToken = localStorage.getItem('wss_token') || token;
const res = await fetch(`/api/auth/users/${userId}/role`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${activeToken}`,
},
body: JSON.stringify({ role: newRole }),
});
if (res.ok) {
setMessage(`User role successfully updated to ${newRole}.`);
fetchAllData();
} else {
const data = await res.json();
setError(data.message || 'Failed to update role.');
}
} catch {
setError('Could not connect to API.');
}
};
const handleUnlock = async (userId) => {
setMessage('');
setError('');
try {
const activeToken = localStorage.getItem('wss_token') || token;
const res = await fetch(`/api/auth/users/${userId}/unlock`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${activeToken}` },
});
if (res.ok) {
setMessage('User account unlocked successfully.');
fetchAllData();
} else {
const data = await res.json();
setError(data.message || 'Failed to unlock user.');
}
} catch {
setError('Could not connect to API.');
}
};
// Organization Actions
const handleProvisionTenant = () => {
setPromptValues({ tier: 'none', name: '', admin_email: '' });
setPromptModal({
isOpen: true,
title: 'Add New Organization',
desc: 'Create a new tenant organization in LarShield.',
inputs: [
{ key: 'name', label: 'Organization Name', placeholder: 'Enter organization name...' },
{
key: 'tier',
label: 'Subscription Tier',
type: 'select',
options: [
{ label: 'None', value: 'none' },
{ label: 'Quick', value: 'quick' },
{ label: 'Advanced', value: 'advanced' },
{ label: 'Deep', value: 'deep' },
{ label: 'Enterprise (Custom)', value: 'Enterprise(Custom)' }
]
},
{ key: 'admin_email', label: 'Admin Email (Optional)', placeholder: 'admin@company.com' }
],
onConfirm: async (values) => {
if (!values.name || !values.name.trim()) {
setError('Organization name is required.');
closePrompt();
return;
}
try {
const activeToken = localStorage.getItem('wss_token') || token;
const res = await fetch('/api/auth/organizations', {
method: 'POST',
headers: { 'Authorization': `Bearer ${activeToken}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ name: values.name, tier: values.tier, admin_email: values.admin_email })
});
if (res.ok) {
setMessage('Organization created successfully.');
fetchAllData();
} else {
const data = await res.json();
setError(data.message || 'Failed to create organization.');
}
} catch {
setError('Could not connect to API to create organization.');
}
closePrompt();
}
});
};
const handleEditTenant = (org) => {
const rawTier = (org.tier || org.subscription_tier || 'none');
const isCustom = rawTier.toLowerCase().includes('custom') || rawTier.toLowerCase().includes('enterprise');
const initialTier = isCustom
? 'Enterprise(Custom)'
: ['none', 'quick', 'advanced', 'deep'].includes(rawTier.toLowerCase())
? rawTier.toLowerCase()
: 'none';
setPromptValues({ name: org.name, tier: initialTier });
setPromptModal({
isOpen: true,
title: 'Edit Organization',
desc: `Modify subscription tier and name for ${org.name}`,
inputs: [
{ key: 'name', label: 'Organization Name', placeholder: 'Enter organization name...' },
{
key: 'tier',
label: 'Subscription Tier',
type: 'select',
options: [
{ label: 'None', value: 'none' },
{ label: 'Quick', value: 'quick' },
{ label: 'Advanced', value: 'advanced' },
{ label: 'Deep', value: 'deep' },
{ label: 'Enterprise (Custom)', value: 'Enterprise(Custom)' }
]
}
],
onConfirm: async (values) => {
try {
const activeToken = localStorage.getItem('wss_token') || token;
const res = await fetch(`/api/auth/organizations/${org.id}`, {
method: 'PUT',
headers: { 'Authorization': `Bearer ${activeToken}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ name: values.name, tier: values.tier })
});
if (res.ok) {
setMessage('Organization updated successfully.');
fetchAllData();
} else {
const data = await res.json();
setError(data.message || 'Failed to update organization.');
}
} catch {
setError('Could not connect to API to update organization.');
}
closePrompt();
}
});
};
const handleAssignScans = (org) => {
setPromptValues({ scan_type: 'Deep', count: '1' });
setPromptModal({
isOpen: true,
title: 'Assign Custom Scans',
desc: `Grant specific scan limits for ${org.name}`,
inputs: [
{
key: 'scan_type',
label: 'Scan Type',
type: 'select',
options: ['Quick', 'Advanced', 'Deep']
},
{ key: 'count', label: 'Number of Scans', placeholder: 'e.g., 5' }
],
onConfirm: async (values) => {
const addedCount = parseInt(values.count);
if (isNaN(addedCount) || addedCount <= 0) {
setError('Please enter a valid scan count.');
closePrompt();
return;
}
try {
const activeToken = localStorage.getItem('wss_token') || token;
const res = await fetch(`/api/auth/organizations/${org.id}/quotas`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${activeToken}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ scan_type: values.scan_type, count: addedCount })
});
if (res.ok) {
setMessage(`${addedCount} ${values.scan_type} scan(s) assigned to ${org.name}.`);
fetchAllData();
} else {
const data = await res.json();
setError(data.message || 'Failed to assign scans.');
}
} catch {
setError('Could not connect to API for assigning scans.');
}
closePrompt();
}
});
};
const handleImpersonate = async (orgId, orgName) => {
try {
const activeToken = localStorage.getItem('wss_token') || token;
const res = await fetch(`/api/auth/impersonate/${orgId}`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${activeToken}` }
});
if (res.ok) {
const data = await res.json();
localStorage.setItem('original_admin_token', activeToken);
localStorage.setItem('wss_token', data.access_token);
window.location.href = '/dashboard';
} else {
const data = await res.json();
setError(data.message || 'Failed to impersonate organization.');
}
} catch {
setError('Could not connect to API for impersonation.');
}
};
const updateScanAccess = async (scanType, requiredTier, isEnabled) => {
setMessage('');
setError('');
try {
const activeToken = localStorage.getItem('wss_token') || token;
const res = await fetch(`/api/admin/scan-access/${scanType}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${activeToken}`,
},
body: JSON.stringify({ required_tier: requiredTier, is_enabled: isEnabled }),
});
if (res.ok) {
setMessage(`${scanType} scan access rules updated.`);
fetchAllData();
} else {
const data = await res.json();
setError(data.message || 'Failed to update scan access control.');
}
} catch {
setError('Could not connect to API.');
}
};
// User Filter & Sort Logic
const getFilteredUsers = () => {
return (users || []).filter(u => {
const emailMatch = !userSearch || u.email?.toLowerCase().includes(userSearch.toLowerCase()) || u.org_name?.toLowerCase().includes(userSearch.toLowerCase());
const roleMatch = userRoleFilter === 'all' || u.role === userRoleFilter;
const orgMatch = userOrgFilter === 'all' || (
userOrgFilter === 'no_org' ? (!u.org_id || u.org_name?.startsWith('No Org')) : String(u.org_id) === String(userOrgFilter)
);
return emailMatch && roleMatch && orgMatch;
});
};
const getSortedUsers = () => {
const list = getFilteredUsers();
return list.sort((a, b) => {
let aVal, bVal;
switch (sortUserCol) {
case 'Email': aVal = a.email || ''; bVal = b.email || ''; break;
case 'Role': aVal = a.role || ''; bVal = b.role || ''; break;
case 'Organization': aVal = a.org_name || ''; bVal = b.org_name || ''; break;
case 'Status': aVal = a.locked_until ? 1 : 0; bVal = b.locked_until ? 1 : 0; break;
default: aVal = a.email || ''; bVal = b.email || '';
}
if (aVal < bVal) return sortUserDir === 'asc' ? -1 : 1;
if (aVal > bVal) return sortUserDir === 'asc' ? 1 : -1;
return 0;
});
};
const handleUserSort = (col) => {
if (col === 'Actions') return;
if (sortUserCol === col) {
setSortUserDir(sortUserDir === 'asc' ? 'desc' : 'asc');
} else {
setSortUserCol(col);
setSortUserDir('asc');
}
};
// Org Filter & Sort Logic
const getFilteredOrgs = () => {
return (organizations || []).filter(org => {
return !orgSearch || org.name?.toLowerCase().includes(orgSearch.toLowerCase()) || org.tier?.toLowerCase().includes(orgSearch.toLowerCase());
});
};
const getSortedOrgs = () => {
const list = getFilteredOrgs();
return list.sort((a, b) => {
let aVal, bVal;
switch (sortOrgCol) {
case 'Tenant Name': aVal = a.name || ''; bVal = b.name || ''; break;
case 'Tier': aVal = a.tier || a.subscription_tier || ''; bVal = b.tier || b.subscription_tier || ''; break;
case 'Created': aVal = new Date(a.created || a.created_at || 0).getTime(); bVal = new Date(b.created || b.created_at || 0).getTime(); break;
default: aVal = a.name || ''; bVal = b.name || '';
}
if (aVal < bVal) return sortOrgDir === 'asc' ? -1 : 1;
if (aVal > bVal) return sortOrgDir === 'asc' ? 1 : -1;
return 0;
});
};
const handleOrgSort = (col) => {
if (col === 'Actions' || col === 'Quotas') return;
if (sortOrgCol === col) {
setSortOrgDir(sortOrgDir === 'asc' ? 'desc' : 'asc');
} else {
setSortOrgCol(col);
setSortOrgDir('asc');
}
};
if (loading) {
return (
<div className="flex h-[80vh] items-center justify-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary"></div>
</div>
);
}
// uniqueOrgsList for filtering is derived inline in the JSX dropdown below
return (
<div className="w-full text-on-surface animate-fade-in pb-xl">
{/* Header Banner */}
<div className="flex flex-col md:flex-row md:items-center justify-between mb-xl gap-sm border-b border-outline-variant/60 pb-md">
<div>
<h1 className="font-extrabold text-on-surface tracking-tight text-[24px] m-0 flex items-center gap-1.5">
Admin <span className="text-primary">Management Console</span>
</h1>
<p className="font-body-md text-on-surface-variant text-[13.5px] mt-1 m-0">
Global client oversight, organization provisioning, user role management, and system logs.
</p>
</div>
<div className="flex gap-sm flex-wrap items-center">
<button
onClick={fetchAllData}
className="flex items-center px-3.5 py-1.5 bg-surface-container border border-outline-variant text-on-surface rounded-lg hover:bg-surface-container-high transition-colors font-bold text-[12.5px] cursor-pointer shadow-2xs"
>
<RefreshCw className={`w-3.5 h-3.5 mr-1.5 text-primary ${syncing ? 'animate-spin' : ''}`} /> Sync Metrics
</button>
<button
onClick={() => navigate('/organization')}
className="flex items-center px-3.5 py-1.5 bg-surface-container border border-outline-variant text-on-surface rounded-lg hover:bg-surface-container-high transition-colors font-bold text-[12.5px] cursor-pointer shadow-2xs"
>
<BarChart3 className="w-3.5 h-3.5 mr-1.5 text-primary" /> Org Dashboard
</button>
<button
onClick={() => navigate('/super-admin/logs')}
className="flex items-center px-3.5 py-1.5 bg-surface-container border border-outline-variant text-on-surface rounded-lg hover:bg-surface-container-high transition-colors font-bold text-[12.5px] cursor-pointer shadow-2xs"
>
<ShieldAlert className="w-3.5 h-3.5 mr-1.5 text-primary" /> Logs & Threats
</button>
<button
onClick={handleProvisionTenant}
className="flex items-center px-4 py-1.5 bg-primary text-white rounded-lg hover:brightness-110 transition-all font-bold text-[13px] border-0 cursor-pointer shadow-sm"
>
<Plus className="w-4 h-4 mr-1" /> Add Organization
</button>
</div>
</div>
{/* Notifications */}
{message && (
<div className="flex gap-2 bg-green-500/10 border border-green-500/30 rounded-xl p-3 mb-md text-green-400 font-bold text-[13px] items-center animate-fade-in">
<CheckCircle2 className="w-4 h-4 shrink-0 text-green-500" />
<span>{message}</span>
</div>
)}
{error && (
<div className="flex gap-2 bg-error/10 border border-error/30 rounded-xl p-3 mb-md text-error font-bold text-[13px] items-center animate-fade-in">
<AlertCircle className="w-4 h-4 shrink-0" />
<span>{error}</span>
</div>
)}
{/* Metric Cards Grid */}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-md mb-xl">
{[
{ title: 'TOTAL TENANTS', value: (metrics?.total_tenants || organizations.length).toString(), icon: Building2, color: 'text-blue-500', bg: 'bg-blue-500/10 border-blue-500/20' },
{ title: 'GLOBAL USERS', value: (metrics?.global_users || users.length).toString(), icon: Users, color: 'text-purple-500', bg: 'bg-purple-500/10 border-purple-500/20' },
{ title: 'ACTIVE LICENSES', value: (metrics?.active_licenses || 0).toString(), icon: Shield, color: 'text-green-500', bg: 'bg-green-500/10 border-green-500/20' },
{ title: 'ACTIVE SCANNERS', value: (metrics?.active_scanners || 0).toString(), icon: Activity, color: 'text-orange-500', bg: 'bg-orange-500/10 border-orange-500/20' }
].map((m, i) => (
<div key={i} className="bg-surface-container-lowest border border-outline-variant p-lg rounded-2xl shadow-2xs hover:shadow-md transition-all group">
<div className="flex justify-between items-center">
<div>
<p className="text-on-surface-variant font-bold text-[12px] uppercase tracking-wider mb-1.5">{m.title}</p>
<h3 className="text-[30px] font-extrabold tracking-tight text-on-surface leading-none">{m.value}</h3>
</div>
<div className={`${m.bg} p-3 rounded-xl border group-hover:scale-110 transition-transform flex items-center justify-center`}>
<m.icon className={`${m.color} w-6 h-6`} />
</div>
</div>
</div>
))}
</div>
{/* Tab Controls Bar */}
<div className="flex items-center gap-2 mb-lg border-b border-outline-variant/60 pb-sm">
<button
onClick={() => setActiveTab('members')}
className={`flex items-center gap-2 px-4 py-2 rounded-lg font-bold text-[13px] transition-all cursor-pointer border-0 ${
activeTab === 'members'
? 'bg-primary text-white shadow-md shadow-primary/20'
: 'bg-surface-container text-on-surface-variant hover:bg-surface-container-high'
}`}
>
<Users className="w-4 h-4" /> Global Members ({users.length})
</button>
<button
onClick={() => setActiveTab('orgs')}
className={`flex items-center gap-2 px-4 py-2 rounded-lg font-bold text-[13px] transition-all cursor-pointer border-0 ${
activeTab === 'orgs'
? 'bg-primary text-white shadow-md shadow-primary/20'
: 'bg-surface-container text-on-surface-variant hover:bg-surface-container-high'
}`}
>
<Building2 className="w-4 h-4" /> Organizations & Quotas ({organizations.length})
</button>
<button
onClick={() => setActiveTab('scan_access')}
className={`flex items-center gap-2 px-4 py-2 rounded-lg font-bold text-[13px] transition-all cursor-pointer border-0 ${
activeTab === 'scan_access'
? 'bg-primary text-white shadow-md shadow-primary/20'
: 'bg-surface-container text-on-surface-variant hover:bg-surface-container-high'
}`}
>
<Sliders className="w-4 h-4" /> Scanner Modes & Access
</button>
<button
onClick={() => setActiveTab('audit')}
className={`flex items-center gap-2 px-4 py-2 rounded-lg font-bold text-[13px] transition-all cursor-pointer border-0 ${
activeTab === 'audit'
? 'bg-primary text-white shadow-md shadow-primary/20'
: 'bg-surface-container text-on-surface-variant hover:bg-surface-container-high'
}`}
>
<FileText className="w-4 h-4" /> Audit Trail
</button>
</div>
{/* Tab 1: Global Members */}
{activeTab === 'members' && (
<div className="bg-surface-container-lowest border border-outline-variant rounded-2xl shadow-sm overflow-hidden p-lg">
<div className="flex flex-col md:flex-row justify-between items-start md:items-center gap-md mb-lg">
<div>
<h2 className="font-extrabold text-on-surface text-[18px] m-0">Global User Accounts</h2>
<p className="text-on-surface-variant text-[13px] mt-0.5 m-0">Manage roles, permissions, and account status across all organizations.</p>
</div>
<div className="flex gap-sm flex-wrap w-full md:w-auto">
<div className="relative flex-1 md:w-64">
<Search className="w-4 h-4 absolute left-3 top-2.5 text-on-surface-variant" />
<input
type="text"
placeholder="Search email or org..."
value={userSearch}
onChange={e => setUserSearch(e.target.value)}
className="w-full bg-surface-container border border-outline-variant rounded-lg pl-9 pr-3 py-1.5 text-[13px] outline-none focus:border-primary text-on-surface font-medium"
/>
</div>
<select
value={userRoleFilter}
onChange={e => setUserRoleFilter(e.target.value)}
className="bg-surface-container border border-outline-variant rounded-lg px-3 py-1.5 text-[13px] font-bold outline-none text-on-surface"
>
<option value="all">All Roles</option>
<option value="super_admin">Super Admin</option>
<option value="admin">Admin</option>
<option value="support_engineer">Support Engineer</option>
<option value="org_admin">Organization Admin</option>
<option value="soc_analyst">SOC Analyst</option>
<option value="executive_user">Executive</option>
<option value="read_only">Read Only</option>
</select>
<select
value={userOrgFilter}
onChange={e => setUserOrgFilter(e.target.value)}
className="bg-surface-container border border-outline-variant rounded-lg px-3 py-1.5 text-[13px] font-bold outline-none text-on-surface"
>
<option value="all">All Organizations</option>
<option value="no_org">No Org (Global Role)</option>
{organizations.map(org => <option key={org.id} value={org.id}>{org.name}</option>)}
</select>
</div>
</div>
<div className="overflow-x-auto">
<table className="w-full text-left border-collapse">
<thead>
<tr className="border-b border-outline-variant bg-surface-container-high/60 select-none">
{['Email', 'Role', 'Organization', 'Status', 'Actions'].map((col) => (
<th
key={col}
onClick={() => handleUserSort(col)}
className={`px-4 py-3 text-[12px] font-bold uppercase tracking-wider text-on-surface-variant ${col === 'Actions' ? 'text-right' : 'cursor-pointer hover:text-primary'}`}
>
{col} {sortUserCol === col ? (sortUserDir === 'asc' ? '↑' : '↓') : ''}
</th>
))}
</tr>
</thead>
<tbody>
{getSortedUsers().map((u) => (
<tr key={u.id} className="border-b border-outline-variant/40 hover:bg-surface-container-high/30 transition-colors">
<td className="px-4 py-3 font-bold text-[13.5px] text-on-surface">{u.email}</td>
<td className="px-4 py-3">
<select
value={u.role || 'read_only'}
onChange={(e) => handleRoleChange(u.id, e.target.value)}
className="bg-surface-container border border-outline-variant rounded-lg px-2.5 py-1 text-[12.5px] font-bold text-on-surface outline-none cursor-pointer"
>
<option value="super_admin">Super Admin</option>
<option value="admin">Admin</option>
<option value="support_engineer">Support Engineer</option>
<option value="org_admin">Org Admin</option>
<option value="soc_analyst">SOC Analyst</option>
<option value="executive_user">Executive</option>
<option value="read_only">Read Only</option>
</select>
</td>
<td className="px-4 py-3 text-[13px] font-medium text-on-surface-variant">
{u.org_name || 'No Org'}
</td>
<td className="px-4 py-3">
{u.locked_until ? (
<span className="inline-flex items-center gap-1 bg-error/10 text-error px-2.5 py-0.5 rounded-full text-[11.5px] font-bold">
<Lock className="w-3 h-3" /> Locked
</span>
) : (
<span className="inline-flex items-center gap-1 bg-green-500/10 text-green-500 px-2.5 py-0.5 rounded-full text-[11.5px] font-bold">
<CheckCircle2 className="w-3 h-3" /> Active
</span>
)}
</td>
<td className="px-4 py-3 text-right">
{u.locked_until && (
<button
onClick={() => handleUnlock(u.id)}
className="px-3 py-1 bg-primary text-white rounded-lg text-[12px] font-bold hover:brightness-110 transition-all border-0 cursor-pointer shadow-xs inline-flex items-center gap-1"
>
<Unlock className="w-3 h-3" /> Unlock
</button>
)}
</td>
</tr>
))}
{getSortedUsers().length === 0 && (
<tr>
<td colSpan="5" className="text-center py-xl text-on-surface-variant font-medium text-[13px]">
No users match the current search filters.
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
)}
{/* Tab 2: Organizations */}
{activeTab === 'orgs' && (
<div className="bg-surface-container-lowest border border-outline-variant rounded-2xl shadow-sm overflow-hidden p-lg">
<div className="flex flex-col md:flex-row justify-between items-start md:items-center gap-md mb-lg">
<div>
<h2 className="font-extrabold text-on-surface text-[18px] m-0">Registered Organizations</h2>
<p className="text-on-surface-variant text-[13px] mt-0.5 m-0">Provision tenant accounts, update subscription tiers, and allocate scan quotas.</p>
</div>
<div className="flex gap-sm w-full md:w-auto">
<div className="relative flex-1 md:w-64">
<Search className="w-4 h-4 absolute left-3 top-2.5 text-on-surface-variant" />
<input
type="text"
placeholder="Search organization name..."
value={orgSearch}
onChange={e => setOrgSearch(e.target.value)}
className="w-full bg-surface-container border border-outline-variant rounded-lg pl-9 pr-3 py-1.5 text-[13px] outline-none focus:border-primary text-on-surface font-medium"
/>
</div>
<button
onClick={handleProvisionTenant}
className="px-3.5 py-1.5 bg-primary text-white rounded-lg font-bold text-[13px] hover:brightness-110 transition-all border-0 cursor-pointer shadow-sm flex items-center gap-1 whitespace-nowrap"
>
<Plus className="w-4 h-4" /> Add Tenant
</button>
</div>
</div>
<div className="overflow-x-auto">
<table className="w-full text-left border-collapse">
<thead>
<tr className="border-b border-outline-variant bg-surface-container-high/60 select-none">
{['Tenant Name', 'Tier', 'Quotas', 'Created', 'Actions'].map((col) => (
<th
key={col}
onClick={() => handleOrgSort(col)}
className={`px-4 py-3 text-[12px] font-bold uppercase tracking-wider text-on-surface-variant ${col === 'Actions' ? 'text-right' : 'cursor-pointer hover:text-primary'}`}
>
{col} {sortOrgCol === col ? (sortOrgDir === 'asc' ? '↑' : '↓') : ''}
</th>
))}
</tr>
</thead>
<tbody>
{getSortedOrgs().map((org) => (
<tr key={org.id} className="border-b border-outline-variant/40 hover:bg-surface-container-high/30 transition-colors">
<td className="px-4 py-3 font-extrabold text-[14px] text-on-surface">{org.name}</td>
<td className="px-4 py-3 text-[13px] font-bold capitalize text-primary">
{org.tier || org.subscription_tier || 'Free'}
</td>
<td className="px-4 py-3">
<div className="flex flex-wrap gap-1.5 items-center">
{org.quotas?.filter(q => ['Quick', 'Advanced', 'Deep'].includes(q.scan_type)).map((q, idx) => {
const remaining = q.allocated_count === -1 ? '∞' : Math.max(0, q.allocated_count - (q.used_count || 0));
const style = q.scan_type === 'Deep' ? 'bg-orange-500/10 text-orange-400 border-orange-500/30' :
q.scan_type === 'Advanced' ? 'bg-purple-500/10 text-purple-400 border-purple-500/30' :
q.scan_type === 'Quick' ? 'bg-blue-500/10 text-blue-400 border-blue-500/30' :
'bg-emerald-500/10 text-emerald-400 border-emerald-500/30';
return (
<div key={idx} className={`text-[10.5px] font-extrabold px-2 py-0.5 rounded border flex items-center gap-1 shadow-2xs ${style}`}>
<span className="uppercase tracking-wider">{q.scan_type}:</span>
<span className="text-[12px]">{remaining}</span>
</div>
);
})}
</div>
</td>
<td className="px-4 py-3 text-[12.5px] font-medium text-on-surface-variant">
{org.created ? org.created : (org.created_at ? new Date(org.created_at).toLocaleDateString() : 'N/A')}
</td>
<td className="px-4 py-3 text-right whitespace-nowrap">
<div className="inline-flex items-center justify-end gap-1 whitespace-nowrap">
<button
onClick={() => handleEditTenant(org)}
className="px-2.5 py-1 bg-surface-container border border-outline-variant text-on-surface rounded-lg hover:bg-surface-container-high transition-colors font-bold text-[12px] cursor-pointer flex items-center gap-1"
>
<Edit3 className="w-3.5 h-3.5 text-primary" /> Edit
</button>
<button
onClick={() => handleAssignScans(org)}
className="px-2.5 py-1 bg-surface-container border border-outline-variant text-on-surface rounded-lg hover:bg-surface-container-high transition-colors font-bold text-[12px] cursor-pointer flex items-center gap-1"
>
<Plus className="w-3.5 h-3.5 text-primary" /> Quotas
</button>
<button
onClick={() => handleImpersonate(org.id, org.name)}
className="px-2.5 py-1 bg-primary/10 text-primary border border-primary/20 rounded-lg hover:bg-primary/20 transition-colors font-bold text-[12px] cursor-pointer flex items-center gap-1"
>
<Key className="w-3.5 h-3.5" /> Impersonate
</button>
</div>
</td>
</tr>
))}
{getSortedOrgs().length === 0 && (
<tr>
<td colSpan="5" className="text-center py-xl text-on-surface-variant font-medium text-[13px]">
No organizations found.
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
)}
{/* Tab 3: Scanner Modes & Access */}
{activeTab === 'scan_access' && (
<div className="bg-surface-container-lowest border border-outline-variant rounded-2xl shadow-sm p-lg">
<h2 className="font-extrabold text-on-surface text-[18px] mb-1">Scanner Engine Access Controls</h2>
<p className="text-on-surface-variant text-[13.5px] mb-lg">
Configure global subscription tier requirements and enable/disable specific scan engines platform-wide.
</p>
<div className="grid grid-cols-1 gap-md">
{(scanAccess || []).map((mode) => (
<div key={mode.scan_type} className="bg-surface-container-low border border-outline-variant/60 rounded-xl p-md flex flex-col md:flex-row md:items-center justify-between gap-md">
<div>
<h4 className="font-extrabold text-on-surface text-[15px] m-0">{mode.scan_type} Scan Engine</h4>
<p className="text-on-surface-variant text-[12.5px] mt-0.5 m-0">
Status: <span className={mode.is_enabled ? 'text-green-400 font-bold' : 'text-error font-bold'}>{mode.is_enabled ? 'Globally Enabled' : 'Globally Disabled'}</span>
</p>
</div>
<div className="flex items-center gap-lg">
<div className="flex flex-col gap-1">
<label className="text-[11px] font-bold uppercase tracking-wider text-on-surface-variant">Required Plan Tier</label>
<select
value={mode.required_tier}
onChange={(e) => updateScanAccess(mode.scan_type, e.target.value, mode.is_enabled)}
className="bg-surface-container border border-outline-variant rounded-lg px-3 py-1.5 text-[13px] font-bold text-on-surface outline-none cursor-pointer"
>
<option value="quick">Quick</option>
<option value="advanced">Advanced</option>
<option value="deep">Deep</option>
<option value="Enterprise(Custom)">Enterprise(Custom)</option>
</select>
</div>
<div className="flex flex-col gap-1">
<label className="text-[11px] font-bold uppercase tracking-wider text-on-surface-variant">Engine Switch</label>
<button
onClick={() => updateScanAccess(mode.scan_type, mode.required_tier, !mode.is_enabled)}
className={`px-4 py-1.5 rounded-lg text-[12.5px] font-bold cursor-pointer transition-all border-0 ${
mode.is_enabled ? 'bg-green-500/20 text-green-400 hover:bg-green-500/30' : 'bg-error/20 text-error hover:bg-error/30'
}`}
>
{mode.is_enabled ? 'Enabled' : 'Disabled'}
</button>
</div>
</div>
</div>
))}
</div>
</div>
)}
{/* Tab 4: Audit Trail Preview */}
{activeTab === 'audit' && (
<div className="bg-surface-container-lowest border border-outline-variant rounded-2xl shadow-sm p-lg">
<div className="flex justify-between items-center mb-md">
<div>
<h2 className="font-extrabold text-on-surface text-[18px] m-0">Recent Audit Trail</h2>
<p className="text-on-surface-variant text-[13px] mt-0.5 m-0">Security actions and administrative audit logs.</p>
</div>
<button
onClick={() => navigate('/super-admin/logs')}
className="px-3.5 py-1.5 bg-primary text-white rounded-lg font-bold text-[12.5px] hover:brightness-110 transition-all border-0 cursor-pointer flex items-center gap-1 shadow-xs"
>
<FileText className="w-3.5 h-3.5" /> Full Audit Logs
</button>
</div>
<div className="overflow-x-auto">
<table className="w-full text-left border-collapse">
<thead>
<tr className="border-b border-outline-variant bg-surface-container-high/60">
<th className="px-4 py-3 text-[12px] font-bold uppercase tracking-wider text-on-surface-variant">TIMESTAMP</th>
<th className="px-4 py-3 text-[12px] font-bold uppercase tracking-wider text-on-surface-variant">PERFORMED BY</th>
<th className="px-4 py-3 text-[12px] font-bold uppercase tracking-wider text-on-surface-variant">ACTION & DETAILS</th>
</tr>
</thead>
<tbody>
{auditLogs.slice(0, 10).map((log) => (
<tr key={log.id} className="border-b border-outline-variant/40 hover:bg-surface-container-high/30 transition-colors">
<td className="px-4 py-3 text-[12.5px] font-medium text-on-surface-variant whitespace-nowrap">
{log.timestamp || 'N/A'}
</td>
<td className="px-4 py-3 font-bold text-[13px] text-primary">
{log.user_email || log.admin_id || 'System'}
</td>
<td className="px-4 py-3 text-[13px] font-medium text-on-surface">
{log.action}
</td>
</tr>
))}
{auditLogs.length === 0 && (
<tr>
<td colSpan="3" className="text-center py-xl text-on-surface-variant font-medium text-[13px]">
No audit logs available.
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
)}
{/* Modal for adding/editing tenant & assigning quotas */}
<CustomModal
isOpen={promptModal.isOpen}
onClose={closePrompt}
title={promptModal.title}
description={promptModal.desc}
footer={
<>
<button onClick={closePrompt} className="px-4 py-2 text-on-surface-variant hover:bg-surface-container rounded-lg font-bold border-0 bg-transparent cursor-pointer">Cancel</button>
<button onClick={() => promptModal.onConfirm(promptValues)} className="px-4 py-2 bg-primary text-white rounded-lg font-bold border-0 cursor-pointer shadow-md shadow-primary/20">Confirm</button>
</>
}
>
<div className="flex flex-col gap-4">
{promptModal.inputs.map(input => (
<div key={input.key} className="flex flex-col">
<label className="text-[12px] font-bold text-on-surface-variant mb-1">{input.label}</label>
{input.type === 'select' ? (
<select
value={promptValues[input.key] || ''}
onChange={(e) => handlePromptChange(input.key, e.target.value)}
className="bg-surface-container border border-outline-variant rounded-lg px-3 py-2 focus:border-primary outline-none text-on-surface font-medium"
>
{input.options.map(opt => (
typeof opt === 'object' ? (
<option key={opt.value} value={opt.value}>{opt.label}</option>
) : (
<option key={opt} value={opt}>{opt}</option>
)
))}
</select>
) : (
<input
type="text"
value={promptValues[input.key] || ''}
onChange={(e) => handlePromptChange(input.key, e.target.value)}
placeholder={input.placeholder}
className="bg-surface-container border border-outline-variant rounded-lg px-3 py-2 focus:border-primary outline-none text-on-surface font-medium"
/>
)}
</div>
))}
</div>
</CustomModal>
</div>
);
};
export const AdminPage = () => (
<ErrorBoundary>
<AdminPageContent />
</ErrorBoundary>
);
|