Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import { useMemo, useState } from "react";
import { BotIcon, User as UserIcon, Users as UsersIcon } from "lucide-react";
import { createNotification } from "@app/components/notifications";
import {
Button,
Combobox,
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
Field,
FieldContent,
FieldLabel,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue
} from "@app/components/v3";
import { useOrganization } from "@app/context";
import { useGetOrganizationGroups } from "@app/hooks/api/organization/queries";
import {
useAddAccountGroupMember,
useAddAccountIdentityMember,
useAddAccountMember,
useAddFolderGroupMember,
useAddFolderIdentityMember,
useAddFolderMember,
useListPamProductGroups,
useListPamProductIdentities,
useListPamResourceRoles,
useUpdateAccountGroupMemberRole,
useUpdateAccountIdentityMemberRole,
useUpdateAccountMemberRole,
useUpdateFolderGroupMemberRole,
useUpdateFolderIdentityMemberRole,
useUpdateFolderMemberRole
} from "@app/hooks/api/pam";
import { PamMemberKind, PamMembershipScope } from "../../components/memberEnums";
import { usePamProductUsers } from "../../components/usePamProductUsers";
type AssigneeOption = {
value: string;
label: string;
kind: PamMemberKind;
subtitle: string;
};
export type EditMemberTarget = {
kind: PamMemberKind;
id: string;
label: string;
role: string;
};
const EXPIRY_OPTIONS = [
{ value: "none", label: "No expiry" },
{ value: "15m", label: "15 minutes" },
{ value: "1h", label: "1 hour" },
{ value: "4h", label: "4 hours" },
{ value: "1d", label: "1 day" },
{ value: "3d", label: "3 days" },
{ value: "7d", label: "1 week" },
{ value: "14d", label: "2 weeks" },
{ value: "30d", label: "1 month" }
];
const ASSIGNEE_ICON: Record<PamMemberKind, typeof UserIcon> = {
[PamMemberKind.User]: UserIcon,
[PamMemberKind.Group]: UsersIcon,
[PamMemberKind.Identity]: BotIcon
};
type Props = {
isOpen: boolean;
onOpenChange: (open: boolean) => void;
scope: PamMembershipScope;
resourceId: string;
existingUserIds: Set<string>;
existingGroupIds: Set<string>;
existingIdentityIds: Set<string>;
editMember?: EditMemberTarget | null;
};
export const AssignAccessModal = ({
isOpen,
onOpenChange,
scope,
resourceId,
existingUserIds,
existingGroupIds,
existingIdentityIds,
editMember
}: Props) => {
const { currentOrg } = useOrganization();
const { users: productUsers } = usePamProductUsers();
const { data: orgGroups } = useGetOrganizationGroups(currentOrg.id);
const { data: productGroups } = useListPamProductGroups();
// Product identity members carry the identity name, covering both org-level and PAM-scoped identities
const { data: productIdentities } = useListPamProductIdentities();
const { data: resourceRoles } = useListPamResourceRoles();
const addAccountUser = useAddAccountMember();
const addAccountGroup = useAddAccountGroupMember();
const addAccountIdentity = useAddAccountIdentityMember();
const addFolderUser = useAddFolderMember();
const addFolderGroup = useAddFolderGroupMember();
const addFolderIdentity = useAddFolderIdentityMember();
const updateAccountUser = useUpdateAccountMemberRole();
const updateAccountGroup = useUpdateAccountGroupMemberRole();
const updateAccountIdentity = useUpdateAccountIdentityMemberRole();
const updateFolderUser = useUpdateFolderMemberRole();
const updateFolderGroup = useUpdateFolderGroupMemberRole();
const updateFolderIdentity = useUpdateFolderIdentityMemberRole();
const isEdit = Boolean(editMember);
const [selectedActor, setSelectedActor] = useState<AssigneeOption | null>(null);
const [roleSlug, setRoleSlug] = useState<string>("");
const [expiry, setExpiry] = useState<string>("none");
// Default to Connector
const defaultRoleSlug = useMemo(() => {
if (!resourceRoles?.length) return "";
return (
resourceRoles.find((r) => r.slug === "connector")?.slug ??
resourceRoles[resourceRoles.length - 1].slug
);
}, [resourceRoles]);
const effectiveRoleSlug = roleSlug || editMember?.role || defaultRoleSlug;
const lockedActor = useMemo<AssigneeOption | null>(() => {
if (!editMember) return null;
const subtitleMap: Record<PamMemberKind, string> = {
[PamMemberKind.User]: "",
[PamMemberKind.Group]: "Group",
[PamMemberKind.Identity]: "Machine Identity"
};
return {
value: editMember.id,
label: editMember.label,
kind: editMember.kind,
subtitle: subtitleMap[editMember.kind]
};
}, [editMember]);
const pamGroupIds = useMemo(
() => new Set((productGroups ?? []).map((m) => m.groupId).filter(Boolean) as string[]),
[productGroups]
);
const assigneeOptions = useMemo<AssigneeOption[]>(() => {
const groups: AssigneeOption[] = (orgGroups ?? [])
.filter((g) => pamGroupIds.has(g.id) && !existingGroupIds.has(g.id))
.map((g) => ({ value: g.id, label: g.name, kind: PamMemberKind.Group, subtitle: "Group" }));
const users: AssigneeOption[] = productUsers
.filter((u) => !u.isGroupMember && !existingUserIds.has(u.id))
.map((u) => ({ value: u.id, label: u.name, kind: PamMemberKind.User, subtitle: u.email }));
const existingIds = existingIdentityIds ?? new Set<string>();
const identities: AssigneeOption[] = (productIdentities ?? [])
.filter((m): m is typeof m & { identityId: string } => Boolean(m.identityId))
.filter((m) => !existingIds.has(m.identityId))
.map((m) => ({
value: m.identityId,
label: m.name,
kind: PamMemberKind.Identity,
subtitle: "Machine Identity"
}));
return [...groups, ...identities, ...users];
}, [
orgGroups,
productUsers,
productIdentities,
pamGroupIds,
existingGroupIds,
existingUserIds,
existingIdentityIds
]);
const selectedRole = (resourceRoles ?? []).find((r) => r.slug === effectiveRoleSlug) ?? null;
const isPending =
addAccountUser.isPending ||
addAccountGroup.isPending ||
addAccountIdentity.isPending ||
addFolderUser.isPending ||
addFolderGroup.isPending ||
addFolderIdentity.isPending ||
updateAccountUser.isPending ||
updateAccountGroup.isPending ||
updateAccountIdentity.isPending ||
updateFolderUser.isPending ||
updateFolderGroup.isPending ||
updateFolderIdentity.isPending;
const reset = () => {
setSelectedActor(null);
setRoleSlug("");
setExpiry("none");
};
const handleEdit = () => {
if (!editMember || !effectiveRoleSlug) return;
const opts = {
onSuccess: () => {
createNotification({ type: "success", text: "Role updated" });
reset();
onOpenChange(false);
}
};
if (scope === PamMembershipScope.Account && editMember.kind === PamMemberKind.User) {
updateAccountUser.mutate(
{ accountId: resourceId, userId: editMember.id, role: effectiveRoleSlug },
opts
);
} else if (scope === PamMembershipScope.Account && editMember.kind === PamMemberKind.Group) {
updateAccountGroup.mutate(
{ accountId: resourceId, groupId: editMember.id, role: effectiveRoleSlug },
opts
);
} else if (scope === PamMembershipScope.Account && editMember.kind === PamMemberKind.Identity) {
updateAccountIdentity.mutate(
{ accountId: resourceId, identityId: editMember.id, role: effectiveRoleSlug },
opts
);
} else if (editMember.kind === PamMemberKind.User) {
updateFolderUser.mutate(
{ folderId: resourceId, userId: editMember.id, role: effectiveRoleSlug },
opts
);
} else if (editMember.kind === PamMemberKind.Group) {
updateFolderGroup.mutate(
{ folderId: resourceId, groupId: editMember.id, role: effectiveRoleSlug },
opts
);
} else {
updateFolderIdentity.mutate(
{ folderId: resourceId, identityId: editMember.id, role: effectiveRoleSlug },
opts
);
}
};
const handleAssign = () => {
if (!selectedActor || !effectiveRoleSlug) return;
const expiryValue = expiry === "none" ? null : expiry;
const opts = {
onSuccess: () => {
createNotification({ type: "success", text: "Access assigned" });
reset();
onOpenChange(false);
}
};
if (scope === PamMembershipScope.Account && selectedActor.kind === PamMemberKind.User) {
addAccountUser.mutate(
{
accountId: resourceId,
userId: selectedActor.value,
role: effectiveRoleSlug,
expiry: expiryValue
},
opts
);
} else if (scope === PamMembershipScope.Account && selectedActor.kind === PamMemberKind.Group) {
addAccountGroup.mutate(
{
accountId: resourceId,
groupId: selectedActor.value,
role: effectiveRoleSlug,
expiry: expiryValue
},
opts
);
} else if (
scope === PamMembershipScope.Account &&
selectedActor.kind === PamMemberKind.Identity
) {
addAccountIdentity.mutate(
{
accountId: resourceId,
identityId: selectedActor.value,
role: effectiveRoleSlug,
expiry: expiryValue
},
opts
);
} else if (selectedActor.kind === PamMemberKind.User) {
addFolderUser.mutate(
{
folderId: resourceId,
userId: selectedActor.value,
role: effectiveRoleSlug,
expiry: expiryValue
},
opts
);
} else if (selectedActor.kind === PamMemberKind.Group) {
addFolderGroup.mutate(
{
folderId: resourceId,
groupId: selectedActor.value,
role: effectiveRoleSlug,
expiry: expiryValue
},
opts
);
} else {
addFolderIdentity.mutate(
{
folderId: resourceId,
identityId: selectedActor.value,
role: effectiveRoleSlug,
expiry: expiryValue
},
opts
);
}
};
return (
<Dialog
open={isOpen}
onOpenChange={(open) => {
if (!open) reset();
onOpenChange(open);
}}
>
<DialogContent className="sm:max-w-xl">
<DialogHeader>
<DialogTitle>{isEdit ? "Edit Access" : "Assign Access"}</DialogTitle>
</DialogHeader>
<div className="flex flex-col gap-4">
<Field>
<FieldLabel htmlFor="pam-assign-access-assignee">Assignee</FieldLabel>
<FieldContent>
<Combobox
id="pam-assign-access-assignee"
value={isEdit ? lockedActor : selectedActor}
options={assigneeOptions}
isDisabled={isEdit}
onValueChange={(opt) => setSelectedActor(opt)}
getOptionValue={(opt) => `${opt.kind}:${opt.value}`}
getOptionLabel={(opt) => opt.label}
getOptionKeywords={(opt) => (opt.subtitle ? [opt.subtitle] : [])}
placeholder="Pick a user, group, or identity..."
searchPlaceholder="Pick a user, group, or identity..."
searchAriaLabel="Search users, groups, and identities"
emptyMessage="No users, groups, or identities available to assign."
modal
renderOption={(opt) => {
const Icon = ASSIGNEE_ICON[opt.kind];
return (
<span className="flex min-w-0 items-center gap-2.5">
<Icon className="size-4 shrink-0 text-muted" />
<span className="min-w-0">
<span className="block truncate">{opt.label}</span>
{opt.subtitle && (
<span className="block truncate text-xs leading-4 text-muted">
{opt.subtitle}
</span>
)}
</span>
</span>
);
}}
/>
</FieldContent>
</Field>
<Field>
<FieldLabel htmlFor="pam-assign-access-role">Role</FieldLabel>
<FieldContent>
<Combobox
id="pam-assign-access-role"
value={selectedRole}
options={resourceRoles ?? []}
isClearable={false}
onValueChange={(opt) => setRoleSlug(opt.slug)}
getOptionValue={(opt) => opt.slug}
getOptionLabel={(opt) => opt.name}
placeholder="Select a role..."
searchPlaceholder="Select a role..."
searchAriaLabel="Search roles"
emptyMessage="No roles found."
modal
renderOption={(opt) => (
<span className="block min-w-0">
<span className="block">{opt.name}</span>
{opt.description && (
<span className="block text-xs leading-4 whitespace-normal text-muted">
{opt.description}
</span>
)}
</span>
)}
/>
</FieldContent>
</Field>
{!isEdit && (
<Field>
<FieldLabel>Expiry</FieldLabel>
<FieldContent>
<Select value={expiry} onValueChange={setExpiry}>
<SelectTrigger className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent position="popper">
{EXPIRY_OPTIONS.map((opt) => (
<SelectItem key={opt.value} value={opt.value}>
{opt.label}
</SelectItem>
))}
</SelectContent>
</Select>
</FieldContent>
</Field>
)}
</div>
<DialogFooter>
<Button variant="ghost" onClick={() => onOpenChange(false)}>
Cancel
</Button>
{isEdit ? (
<Button variant="pam" isPending={isPending} onClick={handleEdit}>
Save
</Button>
) : (
<Button
variant="pam"
isPending={isPending}
isDisabled={!selectedActor}
onClick={handleAssign}
>
Assign
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
);
};