680 lines
30 KiB
TypeScript
680 lines
30 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import {
|
|
Shield,
|
|
Key,
|
|
Lock,
|
|
Unlock,
|
|
LogOut,
|
|
Plus,
|
|
Trash2,
|
|
RefreshCw,
|
|
User as UserIcon,
|
|
CheckCircle2,
|
|
AlertTriangle,
|
|
FileText,
|
|
Hash,
|
|
} from 'lucide-react';
|
|
import { Modal } from '../ui/Modal';
|
|
import { useToast } from '../ui/Toast';
|
|
import {
|
|
getUser,
|
|
getUserRoles,
|
|
getSystemRoles,
|
|
assignUserRole,
|
|
removeUserRole,
|
|
getUserClaims,
|
|
forceLogoutUser,
|
|
setUserLockout,
|
|
updateUserStatus,
|
|
} from '../../services/api';
|
|
import { clearCachedUserRbac, fetchAndCacheRoleClaims } from '../../services/roleClaimsCache';
|
|
import { HelpTooltip } from '../ui/HelpTooltip';
|
|
import { ObfuscatedIdNumber } from '../ui/ObfuscatedIdNumber';
|
|
import { decodeIdNumberHex } from '../../utils/hashids';
|
|
import { User, Role, Claim, ActivityStatus } from '../../types';
|
|
import { formatDate, getActivityStatusBadge, getUserRoleBadge } from '../../lib/formatters';
|
|
import { useRbac, ROLE_CLAIM_DEFINITIONS } from '../../auth/rbac';
|
|
|
|
interface UserSecurityModalProps {
|
|
user: User | null;
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
}
|
|
|
|
export const UserSecurityModal: React.FC<UserSecurityModalProps> = ({
|
|
user,
|
|
isOpen,
|
|
onClose,
|
|
}) => {
|
|
const queryClient = useQueryClient();
|
|
const { success, error: toastError } = useToast();
|
|
const { canApprove, canAdd, canView, canAccess, isUnrestricted } = useRbac();
|
|
const hasTenantSelfHelpAccess = canAccess('tenant_self_service') || canView('tenant_self_service');
|
|
|
|
const canManageRolesAndClaims =
|
|
canApprove('settings_users') ||
|
|
canApprove('tenant_management') ||
|
|
canApprove('tenant_self_service') ||
|
|
isUnrestricted;
|
|
const canManageSessions =
|
|
canApprove('settings_users') ||
|
|
canApprove('tenant_management') ||
|
|
hasTenantSelfHelpAccess ||
|
|
isUnrestricted;
|
|
const canChangeStatus =
|
|
canAdd('settings_users') ||
|
|
canAdd('tenant_management') ||
|
|
canAdd('tenant_self_service') ||
|
|
canApprove('settings_users') ||
|
|
isUnrestricted;
|
|
|
|
const [activeTab, setActiveTab] = useState<'overview' | 'roles' | 'claims' | 'sessions'>('overview');
|
|
|
|
const [selectedRoleToAdd, setSelectedRoleToAdd] = useState('');
|
|
|
|
const userId = user?.id || '';
|
|
|
|
// 1. Fetch fresh user details
|
|
const { data: freshUser, isLoading: loadingUser, refetch: refetchUser } = useQuery<User | null>({
|
|
queryKey: ['user-detail', userId],
|
|
queryFn: () => (userId ? getUser(userId) : null),
|
|
enabled: isOpen && !!userId,
|
|
});
|
|
|
|
// 2. Fetch assigned user roles
|
|
const { data: userRoles = [], isLoading: loadingUserRoles, refetch: refetchUserRoles } = useQuery<Role[]>({
|
|
queryKey: ['user-roles', userId],
|
|
queryFn: () => (userId ? getUserRoles(userId) : []),
|
|
enabled: isOpen && !!userId,
|
|
});
|
|
|
|
// 3. Fetch system roles directory
|
|
const { data: systemRoles = [], isLoading: loadingSystemRoles } = useQuery<Role[]>({
|
|
queryKey: ['system-roles'],
|
|
queryFn: getSystemRoles,
|
|
enabled: isOpen,
|
|
});
|
|
|
|
// Effective role derivation for hooks (Single-role policy)
|
|
const rawRoleValue = freshUser?.role || user?.role;
|
|
const userRoleStr = rawRoleValue != null ? String(rawRoleValue).trim() : '';
|
|
|
|
const assignedRole = React.useMemo(() => {
|
|
if (Array.isArray(userRoles) && userRoles.length > 0) {
|
|
return userRoles[0];
|
|
}
|
|
if (userRoleStr && Array.isArray(systemRoles)) {
|
|
const match = systemRoles.find(
|
|
(r) =>
|
|
r &&
|
|
((r.name && r.name.toLowerCase() === userRoleStr.toLowerCase()) ||
|
|
String(r.id) === userRoleStr)
|
|
);
|
|
if (match) return match;
|
|
return { id: userRoleStr, name: userRoleStr } as Role;
|
|
}
|
|
return null;
|
|
}, [userRoles, userRoleStr, systemRoles]);
|
|
|
|
const assignedRoleId = assignedRole?.id || (assignedRole as any)?.Id || '';
|
|
const assignedRoleName = assignedRole?.name || (assignedRole as any)?.Name || '';
|
|
|
|
// 4. Fetch claims derived from the user's role by hitting the endpoint
|
|
const {
|
|
data: roleClaims = [],
|
|
isLoading: loadingRoleClaims,
|
|
refetch: refetchRoleClaims,
|
|
} = useQuery<Claim[]>({
|
|
queryKey: ['role-claims-detail', assignedRoleId, assignedRoleName],
|
|
queryFn: async () => {
|
|
if (!assignedRoleId && !assignedRoleName) return [];
|
|
return await fetchAndCacheRoleClaims(String(assignedRoleId), assignedRoleName, true);
|
|
},
|
|
enabled: isOpen && (!!assignedRoleId || !!assignedRoleName),
|
|
});
|
|
|
|
// 5. Fetch direct user claims if any
|
|
const {
|
|
data: directClaims = [],
|
|
isLoading: loadingDirectClaims,
|
|
refetch: refetchDirectClaims,
|
|
} = useQuery<Claim[]>({
|
|
queryKey: ['user-direct-claims', userId],
|
|
queryFn: () => (userId ? getUserClaims(userId) : []),
|
|
enabled: isOpen && !!userId,
|
|
});
|
|
|
|
// Combined claims with metadata
|
|
const userEffectiveClaims = React.useMemo(() => {
|
|
const map = new Map<string, Claim & { description?: string; label?: string; source: string }>();
|
|
|
|
// 1. Role claims from endpoint
|
|
if (Array.isArray(roleClaims)) {
|
|
roleClaims.forEach((c) => {
|
|
if (!c) return;
|
|
const type = (c.claimType || c.type || '').trim();
|
|
if (!type) return;
|
|
const def = ROLE_CLAIM_DEFINITIONS.find((d) => d.claim.toLowerCase() === type.toLowerCase());
|
|
map.set(type.toLowerCase(), {
|
|
...c,
|
|
claimType: type,
|
|
claimValue: c.claimValue || c.value || 'all',
|
|
label: def?.label || type,
|
|
description: def?.description || 'Security claim granted via role membership',
|
|
source: assignedRoleName ? `Role: ${assignedRoleName}` : 'Role Policy',
|
|
});
|
|
});
|
|
}
|
|
|
|
// 2. Direct user claims if any
|
|
if (Array.isArray(directClaims)) {
|
|
directClaims.forEach((c) => {
|
|
if (!c) return;
|
|
const type = (c.claimType || c.type || '').trim();
|
|
if (!type) return;
|
|
if (!map.has(type.toLowerCase())) {
|
|
const def = ROLE_CLAIM_DEFINITIONS.find((d) => d.claim.toLowerCase() === type.toLowerCase());
|
|
map.set(type.toLowerCase(), {
|
|
...c,
|
|
claimType: type,
|
|
claimValue: c.claimValue || c.value || 'all',
|
|
label: def?.label || type,
|
|
description: def?.description || 'Direct user account claim',
|
|
source: 'Direct User Claim',
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
return Array.from(map.values());
|
|
}, [roleClaims, directClaims, assignedRoleName]);
|
|
|
|
// Role Mutations (Strictly single-role per user)
|
|
const assignRoleMutation = useMutation({
|
|
mutationFn: (roleId: string) => {
|
|
if ((Array.isArray(userRoles) ? userRoles : []).length >= 1) {
|
|
throw new Error('Single-role policy: This user already has an assigned role. Please remove the existing role before assigning a new one.');
|
|
}
|
|
const selectedRole = (Array.isArray(systemRoles) ? systemRoles : []).find(
|
|
(r) => r && (String(r.id) === String(roleId) || String((r as any).Id) === String(roleId))
|
|
);
|
|
const roleName = selectedRole?.name || (selectedRole as any)?.Name || '';
|
|
if (selectedRole && (roleName.toLowerCase() === 'admin' || String(selectedRole.id) === '1') && hasTenantSelfHelpAccess) {
|
|
throw new Error('Security policy: Users with tenant self-help access cannot assign Admin roles.');
|
|
}
|
|
return assignUserRole(userId, roleId);
|
|
},
|
|
onSuccess: () => {
|
|
clearCachedUserRbac(userId);
|
|
success('Role Assigned', 'Role assigned successfully.');
|
|
refetchUserRoles();
|
|
refetchUser();
|
|
refetchRoleClaims();
|
|
queryClient.invalidateQueries({ queryKey: ['users'] });
|
|
setSelectedRoleToAdd('');
|
|
},
|
|
onError: (err: any) => toastError('Role Assignment Failed', err),
|
|
});
|
|
|
|
const removeRoleMutation = useMutation({
|
|
mutationFn: (roleId: string) => removeUserRole(userId, roleId),
|
|
onSuccess: () => {
|
|
clearCachedUserRbac(userId);
|
|
success('Role Removed', 'Role revoked from user.');
|
|
refetchUserRoles();
|
|
refetchUser();
|
|
refetchRoleClaims();
|
|
queryClient.invalidateQueries({ queryKey: ['users'] });
|
|
},
|
|
onError: (err: any) => toastError('Role Removal Failed', err),
|
|
});
|
|
|
|
// Session & Lockout Mutations
|
|
const logoutMutation = useMutation({
|
|
mutationFn: () => forceLogoutUser(userId),
|
|
onSuccess: () => success('Session Terminated', 'Target user session revoked. Re-authentication required.'),
|
|
onError: (err: any) => toastError('Logout Failed', err),
|
|
});
|
|
|
|
const lockoutMutation = useMutation({
|
|
mutationFn: (disableLock: boolean) => setUserLockout(userId, disableLock),
|
|
onSuccess: (_, disableLock) => {
|
|
success(
|
|
disableLock ? 'Account Unlocked' : 'Account Locked',
|
|
`User account lockout status successfully modified.`
|
|
);
|
|
refetchUser();
|
|
},
|
|
onError: (err: any) => toastError('Lockout Update Failed', err),
|
|
});
|
|
|
|
const statusMutation = useMutation({
|
|
mutationFn: (newStatus: ActivityStatus) => updateUserStatus(userId, newStatus),
|
|
onSuccess: () => {
|
|
success('Status Updated', 'User activity status changed.');
|
|
refetchUser();
|
|
queryClient.invalidateQueries({ queryKey: ['users'] });
|
|
},
|
|
onError: (err: any) => toastError('Status Update Failed', err),
|
|
});
|
|
|
|
const availableRoles = React.useMemo(() => {
|
|
return (Array.isArray(systemRoles) ? systemRoles : []).filter((r) => {
|
|
if (!r) return false;
|
|
const rId = r.id || (r as any).Id;
|
|
const rName = r.name || (r as any).Name || '';
|
|
if (hasTenantSelfHelpAccess && (rName.toLowerCase() === 'admin' || String(rId) === '1')) {
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
}, [systemRoles, hasTenantSelfHelpAccess]);
|
|
|
|
if (!isOpen || !user) return null;
|
|
|
|
const effectiveUser: User = freshUser || user;
|
|
const decodedId = effectiveUser.idNumber ? decodeIdNumberHex(effectiveUser.idNumber) : null;
|
|
|
|
return (
|
|
<Modal
|
|
isOpen={isOpen}
|
|
onClose={onClose}
|
|
title={`User Administration: ${effectiveUser.email || effectiveUser.id}`}
|
|
subtitle="Direct tenant-level user, role, claims, and session administration without database intervention."
|
|
>
|
|
<div className="space-y-4 text-xs">
|
|
{/* Navigation Tabs */}
|
|
<div className="flex gap-2 border-b border-slate-800 pb-2">
|
|
<button
|
|
type="button"
|
|
onClick={() => setActiveTab('overview')}
|
|
className={`px-3 py-1.5 rounded-lg font-semibold transition cursor-pointer ${
|
|
activeTab === 'overview'
|
|
? 'bg-blue-600 text-white'
|
|
: 'text-slate-400 hover:text-white hover:bg-slate-800'
|
|
}`}
|
|
>
|
|
Profile & Status
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setActiveTab('roles')}
|
|
className={`px-3 py-1.5 rounded-lg font-semibold transition cursor-pointer flex items-center gap-1.5 ${
|
|
activeTab === 'roles'
|
|
? 'bg-blue-600 text-white'
|
|
: 'text-slate-400 hover:text-white hover:bg-slate-800'
|
|
}`}
|
|
>
|
|
<Shield className="w-3.5 h-3.5" />
|
|
<span>Roles ({(Array.isArray(userRoles) ? userRoles : []).length})</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setActiveTab('claims')}
|
|
className={`px-3 py-1.5 rounded-lg font-semibold transition cursor-pointer flex items-center gap-1.5 ${
|
|
activeTab === 'claims'
|
|
? 'bg-blue-600 text-white'
|
|
: 'text-slate-400 hover:text-white hover:bg-slate-800'
|
|
}`}
|
|
>
|
|
<Key className="w-3.5 h-3.5" />
|
|
<span>Claims ({userEffectiveClaims.length})</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setActiveTab('sessions')}
|
|
className={`px-3 py-1.5 rounded-lg font-semibold transition cursor-pointer flex items-center gap-1.5 ${
|
|
activeTab === 'sessions'
|
|
? 'bg-blue-600 text-white'
|
|
: 'text-slate-400 hover:text-white hover:bg-slate-800'
|
|
}`}
|
|
>
|
|
<Lock className="w-3.5 h-3.5" />
|
|
<span>Security & Sessions</span>
|
|
</button>
|
|
</div>
|
|
|
|
{/* TAB 1: Overview & Profile */}
|
|
{activeTab === 'overview' && (
|
|
<div className="space-y-3">
|
|
<div className="p-4 bg-slate-900/60 rounded-xl border border-slate-800 space-y-2.5">
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div>
|
|
<span className="text-slate-400 font-semibold block text-[11px] uppercase">Email:</span>
|
|
<span className="text-white font-medium">{effectiveUser.email || '—'}</span>
|
|
</div>
|
|
<div>
|
|
<span className="text-slate-400 font-semibold block text-[11px] uppercase">User UUID:</span>
|
|
<span className="font-mono text-slate-300 text-[11px] break-all">{effectiveUser.id}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-3 border-t border-slate-800/80 pt-2">
|
|
<div>
|
|
<span className="text-slate-400 font-semibold block text-[11px] uppercase">Id Number:</span>
|
|
<div className="flex items-center gap-2 mt-0.5">
|
|
<ObfuscatedIdNumber idNumber={effectiveUser.idNumber} />
|
|
{decodedId && decodedId !== effectiveUser.idNumber && (
|
|
<div className="flex items-center gap-1 text-[10px] text-blue-400 font-mono">
|
|
<span>(Decoded:</span>
|
|
<ObfuscatedIdNumber idNumber={decodedId} />
|
|
<span>)</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<span className="text-slate-400 font-semibold block text-[11px] uppercase">Last Login:</span>
|
|
<span className="font-mono text-slate-300">
|
|
{effectiveUser.lastLoginAt ? formatDate(effectiveUser.lastLoginAt) : 'Never'}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="border-t border-slate-800/80 pt-2 flex items-center justify-between">
|
|
<div>
|
|
<span className="text-slate-400 font-semibold block text-[11px] uppercase mb-0.5">
|
|
Account Status:
|
|
</span>
|
|
<span
|
|
className={`inline-flex items-center px-2 py-0.5 rounded text-[10px] font-semibold border ${
|
|
(getActivityStatusBadge(effectiveUser?.status) || {}).className || 'bg-slate-800 text-slate-300'
|
|
}`}
|
|
>
|
|
{(getActivityStatusBadge(effectiveUser?.status) || {}).label || 'Active'}
|
|
</span>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
{effectiveUser?.status === ActivityStatus.Active ? (
|
|
<button
|
|
type="button"
|
|
onClick={() => statusMutation.mutate(ActivityStatus.Inactive)}
|
|
disabled={statusMutation.isPending || !canChangeStatus}
|
|
className="px-2.5 py-1 text-xs font-semibold text-amber-400 bg-amber-500/10 hover:bg-amber-500/20 border border-amber-500/20 rounded-lg transition disabled:opacity-50 cursor-pointer"
|
|
>
|
|
Suspend User
|
|
</button>
|
|
) : (
|
|
<button
|
|
type="button"
|
|
onClick={() => statusMutation.mutate(ActivityStatus.Active)}
|
|
disabled={statusMutation.isPending || !canChangeStatus}
|
|
className="px-2.5 py-1 text-xs font-semibold text-emerald-400 bg-emerald-500/10 hover:bg-emerald-500/20 border border-emerald-500/20 rounded-lg transition disabled:opacity-50 cursor-pointer"
|
|
>
|
|
Activate User
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* TAB 2: Roles (Single-Role Policy) */}
|
|
{activeTab === 'roles' && (
|
|
<div className="space-y-3">
|
|
{/* If user already has a role, enforce single-role policy */}
|
|
{(Array.isArray(userRoles) ? userRoles : []).length > 0 ? (
|
|
<div className="p-3 bg-slate-900/80 rounded-xl border border-slate-800 flex items-center justify-between gap-3">
|
|
<div className="flex items-center gap-2 min-w-0">
|
|
<Shield className="w-4 h-4 text-blue-400 shrink-0" />
|
|
<div className="min-w-0">
|
|
<span className="font-semibold text-white block">Active Assigned Role (1 of 1 Max)</span>
|
|
<p className="text-[11px] text-slate-400">
|
|
The system strictly enforces one role per user. To assign a different role, remove the current role first.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<span className="text-[10px] font-mono text-emerald-400 bg-emerald-500/10 border border-emerald-500/25 px-2 py-0.5 rounded-full shrink-0 font-semibold">
|
|
Role Assigned
|
|
</span>
|
|
</div>
|
|
) : (
|
|
/* Add Role selector when user has no role */
|
|
<div className="space-y-1.5">
|
|
<label className="block text-[11px] font-semibold uppercase tracking-wider text-slate-400">
|
|
Assign System Role (1 Role Max)
|
|
</label>
|
|
<div className="flex items-center gap-2">
|
|
<select
|
|
value={selectedRoleToAdd}
|
|
onChange={(e) => setSelectedRoleToAdd(e.target.value)}
|
|
disabled={loadingSystemRoles || availableRoles.length === 0 || !canManageRolesAndClaims || assignRoleMutation.isPending}
|
|
className="flex-1 bg-slate-900 border border-slate-800 rounded-xl px-3 py-1.5 text-white focus:outline-none focus:border-blue-500 disabled:opacity-50 text-xs"
|
|
>
|
|
<option value="">
|
|
{availableRoles.length === 0 ? 'No available roles' : 'Select role to assign...'}
|
|
</option>
|
|
{availableRoles.map((r: any) => {
|
|
const roleOptId = r?.id || r?.Id || String(r);
|
|
const roleOptName = r?.name || r?.Name || String(r);
|
|
return (
|
|
<option key={roleOptId} value={roleOptId}>
|
|
{roleOptName}
|
|
</option>
|
|
);
|
|
})}
|
|
</select>
|
|
<button
|
|
type="button"
|
|
onClick={() => selectedRoleToAdd && assignRoleMutation.mutate(selectedRoleToAdd)}
|
|
disabled={!selectedRoleToAdd || assignRoleMutation.isPending || !canManageRolesAndClaims}
|
|
className="inline-flex items-center gap-1 px-3 py-1.5 bg-blue-600 hover:bg-blue-500 text-white rounded-xl font-semibold transition disabled:opacity-50 cursor-pointer shrink-0 text-xs shadow-md shadow-blue-600/20"
|
|
>
|
|
<Plus className="w-3.5 h-3.5" />
|
|
<span>{assignRoleMutation.isPending ? 'Assigning...' : 'Assign Role'}</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* List of currently assigned roles */}
|
|
<div className="bg-slate-900/60 rounded-xl border border-slate-800 divide-y divide-slate-800/80">
|
|
{loadingUserRoles ? (
|
|
<div className="p-6 text-center text-slate-400 flex flex-col items-center gap-2">
|
|
<RefreshCw className="w-5 h-5 animate-spin text-blue-400" />
|
|
<span>Loading assigned role...</span>
|
|
</div>
|
|
) : (Array.isArray(userRoles) ? userRoles : []).length === 0 ? (
|
|
<div className="p-6 text-center text-slate-500 flex flex-col items-center gap-2">
|
|
<Shield className="w-7 h-7 text-slate-600 mb-0.5" />
|
|
<p className="font-semibold text-slate-300">No role assigned to this user.</p>
|
|
<p className="text-[11px] text-slate-500">Select a role above to assign it. Users must have exactly one role.</p>
|
|
</div>
|
|
) : (
|
|
(Array.isArray(userRoles) ? userRoles : []).map((role) => {
|
|
if (!role) return null;
|
|
const roleId = role.id || (role as any).Id || String(role);
|
|
const roleName = role.name || (role as any).Name || String(role);
|
|
return (
|
|
<div key={roleId} className="p-3.5 flex items-center justify-between gap-3">
|
|
<div className="min-w-0">
|
|
<div className="text-white font-semibold flex items-center gap-2">
|
|
<Shield className="w-4 h-4 text-blue-400 shrink-0" />
|
|
<span className="text-sm">{roleName}</span>
|
|
<span className="text-[10px] text-blue-400 bg-blue-500/10 border border-blue-500/20 px-2 py-0.5 rounded font-mono font-medium">
|
|
Active
|
|
</span>
|
|
</div>
|
|
<div className="text-[10px] text-slate-400 font-mono mt-1">Role ID: {roleId}</div>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={() => removeRoleMutation.mutate(roleId)}
|
|
disabled={removeRoleMutation.isPending || !canManageRolesAndClaims}
|
|
className="p-2 text-rose-400 hover:text-white hover:bg-rose-500/20 border border-transparent hover:border-rose-500/30 rounded-xl transition disabled:opacity-50 cursor-pointer shrink-0"
|
|
title="Remove Role"
|
|
>
|
|
<Trash2 className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
);
|
|
})
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* TAB 3: Claims (Read-Only Inherited from Role) */}
|
|
{activeTab === 'claims' && (
|
|
<div className="space-y-3">
|
|
{/* Informational Policy Banner */}
|
|
<div className="p-3 bg-blue-500/10 rounded-xl border border-blue-500/20 text-xs flex items-center justify-between gap-3">
|
|
<div className="flex items-center gap-2.5 min-w-0">
|
|
<CheckCircle2 className="w-4 h-4 text-blue-400 shrink-0" />
|
|
<div className="min-w-0">
|
|
<span className="font-semibold text-white block">Role-Derived Security Claims</span>
|
|
<p className="text-[11px] text-slate-400">
|
|
{assignedRoleName ? (
|
|
<>Claims are inherited directly from the assigned role (<strong className="text-blue-300">{assignedRoleName}</strong>). They cannot be manually added or removed at the individual user level.</>
|
|
) : (
|
|
<>Assign a role on the Roles tab to grant security claims to this user.</>
|
|
)}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<span className="text-[10px] font-mono text-blue-400 bg-blue-500/20 px-2 py-0.5 rounded border border-blue-500/30 shrink-0 font-semibold">
|
|
Read-Only
|
|
</span>
|
|
</div>
|
|
|
|
{/* Claims list */}
|
|
<div className="bg-slate-900/60 rounded-xl border border-slate-800 divide-y divide-slate-800/80">
|
|
{loadingRoleClaims || loadingDirectClaims ? (
|
|
<div className="p-8 text-center text-slate-400 flex flex-col items-center gap-2">
|
|
<RefreshCw className="w-6 h-6 animate-spin text-blue-400" />
|
|
<span>Loading security claims from role endpoint...</span>
|
|
</div>
|
|
) : userEffectiveClaims.length === 0 ? (
|
|
<div className="p-8 text-center text-slate-500 flex flex-col items-center gap-2">
|
|
<Key className="w-8 h-8 text-slate-600 mb-1" />
|
|
<p className="font-semibold text-slate-300">No active claims found for user.</p>
|
|
<p className="text-[11px] text-slate-500 max-w-sm">
|
|
{assignedRoleName
|
|
? `The assigned role (${assignedRoleName}) does not contain any configured permission claims.`
|
|
: 'Assign a role to this user on the Roles tab to establish functional permissions.'}
|
|
</p>
|
|
</div>
|
|
) : (
|
|
userEffectiveClaims.map((claim, idx) => {
|
|
const claimType = claim.claimType || claim.type || 'Custom Claim';
|
|
const claimVal = claim.claimValue ?? claim.value ?? 'all';
|
|
return (
|
|
<div key={idx} className="p-3.5 flex items-center justify-between gap-3">
|
|
<div className="min-w-0 flex-1">
|
|
<div className="text-white font-medium flex items-center gap-2 flex-wrap">
|
|
<Key className="w-3.5 h-3.5 text-amber-400 shrink-0" />
|
|
<span className="font-semibold text-xs text-white">{claim.label || claimType}</span>
|
|
<span className="text-[10px] font-mono text-slate-400 bg-slate-800 px-1.5 py-0.2 rounded border border-slate-700/60">
|
|
{claimType}
|
|
</span>
|
|
</div>
|
|
{claim.description && (
|
|
<p className="text-[11px] text-slate-400 mt-0.5 leading-relaxed">
|
|
{claim.description}
|
|
</p>
|
|
)}
|
|
</div>
|
|
<div className="flex items-center gap-2 shrink-0">
|
|
<span className="inline-flex items-center px-2 py-0.5 rounded text-[10px] font-semibold bg-emerald-500/10 text-emerald-400 border border-emerald-500/20 font-mono">
|
|
Value: {claimVal}
|
|
</span>
|
|
<span className="text-[10px] text-slate-500 hidden sm:inline">
|
|
{claim.source}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
);
|
|
})
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* TAB 4: Sessions & Direct Account Interventions */}
|
|
{activeTab === 'sessions' && (
|
|
<div className="space-y-3">
|
|
<div className="p-4 bg-slate-900/60 rounded-xl border border-slate-800 space-y-4">
|
|
{/* Force Logout Action */}
|
|
<div className="flex items-center justify-between border-b border-slate-800/80 pb-3">
|
|
<div>
|
|
<h4 className="text-white font-semibold flex items-center gap-1.5">
|
|
<LogOut className="w-4 h-4 text-amber-400" />
|
|
<span>Revoke Active User Session</span>
|
|
<HelpTooltip
|
|
title="User Session Invalidation"
|
|
content="Enables a tenant self-service user to immediately logout a user if they are currently logged in, terminating their active token and session."
|
|
size="sm"
|
|
placement="top"
|
|
/>
|
|
</h4>
|
|
<p className="text-[11px] text-slate-400 mt-0.5">
|
|
Terminates all active JWT tokens and sessions immediately without direct database intervention.
|
|
</p>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={() => logoutMutation.mutate()}
|
|
disabled={logoutMutation.isPending || !canManageSessions}
|
|
className="px-3 py-1.5 bg-amber-500/10 hover:bg-amber-500/20 text-amber-400 border border-amber-500/20 rounded-xl font-semibold transition cursor-pointer disabled:opacity-50"
|
|
>
|
|
{logoutMutation.isPending ? 'Revoking...' : 'Force Logout'}
|
|
</button>
|
|
</div>
|
|
|
|
{/* Lock / Unlock Account */}
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<h4 className="text-white font-semibold flex items-center gap-1.5">
|
|
<Lock className="w-4 h-4 text-rose-400" />
|
|
<span>Account Lockout Control</span>
|
|
<HelpTooltip
|
|
title="Account Lockout Control"
|
|
content="Enables a tenant self-service user to change a user's lockout status. The system defaults to a lockout period of 100 years when locking a user account."
|
|
size="sm"
|
|
placement="top"
|
|
/>
|
|
</h4>
|
|
<p className="text-[11px] text-slate-400 mt-0.5">
|
|
Temporarily lock out or re-enable the user from logging into this tenant. The system defaults to a lockout period of 100 years.
|
|
</p>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={() => lockoutMutation.mutate(false)}
|
|
disabled={lockoutMutation.isPending || !canManageSessions}
|
|
className="px-3 py-1.5 bg-rose-500/10 hover:bg-rose-500/20 text-rose-400 border border-rose-500/20 rounded-xl font-semibold transition cursor-pointer disabled:opacity-50"
|
|
>
|
|
Lock User
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => lockoutMutation.mutate(true)}
|
|
disabled={lockoutMutation.isPending || !canManageSessions}
|
|
className="px-3 py-1.5 bg-emerald-500/10 hover:bg-emerald-500/20 text-emerald-400 border border-emerald-500/20 rounded-xl font-semibold transition cursor-pointer disabled:opacity-50"
|
|
>
|
|
Unlock User
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Modal Footer */}
|
|
<div className="pt-2 border-t border-slate-800 flex justify-end">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="px-4 py-2 text-xs font-semibold text-white bg-slate-800 hover:bg-slate-700 rounded-xl transition cursor-pointer"
|
|
>
|
|
Close
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
);
|
|
};
|