Fix team invite role keys and block already-registered emails.
Send SUPER_ADMIN-style keys instead of display labels, and validate invite emails against existing team members before send. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
4440f2bf26
commit
524e891e73
|
|
@ -96,6 +96,29 @@ export const getTeamMembers = (page?: number, pageSize?: number) =>
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/** Lowercased emails for every team member (active, pending invite, etc.). */
|
||||||
|
export async function fetchAllTeamMemberEmails(): Promise<Set<string>> {
|
||||||
|
const emails = new Set<string>()
|
||||||
|
const batchSize = 100
|
||||||
|
let page = 1
|
||||||
|
let totalPages = 1
|
||||||
|
|
||||||
|
do {
|
||||||
|
const res = await getTeamMembers(page, batchSize)
|
||||||
|
totalPages = res.data.metadata?.total_pages ?? 1
|
||||||
|
const members = (res.data.data ?? [])
|
||||||
|
.map((entry) => normalizeTeamMember(entry))
|
||||||
|
.filter((entry): entry is TeamMember => entry != null)
|
||||||
|
for (const member of members) {
|
||||||
|
const email = member.email.trim().toLowerCase()
|
||||||
|
if (email) emails.add(email)
|
||||||
|
}
|
||||||
|
page++
|
||||||
|
} while (page <= totalPages)
|
||||||
|
|
||||||
|
return emails
|
||||||
|
}
|
||||||
|
|
||||||
/** Permissions for the signed-in team member (from the members list payload). */
|
/** Permissions for the signed-in team member (from the members list payload). */
|
||||||
export async function fetchCurrentTeamMemberPermissions(): Promise<string[]> {
|
export async function fetchCurrentTeamMemberPermissions(): Promise<string[]> {
|
||||||
const memberId = Number(localStorage.getItem("member_id"))
|
const memberId = Number(localStorage.getItem("member_id"))
|
||||||
|
|
|
||||||
|
|
@ -42,17 +42,28 @@ export function formatTeamRoleLabel(teamRole: string): string {
|
||||||
return teamRole.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase())
|
return teamRole.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase())
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Role name sent to POST /team/members/invite (used in invitation emails). */
|
/**
|
||||||
|
* Role key/name sent to POST /team/members/invite.
|
||||||
|
* Must be a built-in team role (e.g. SUPER_ADMIN) or an exact RBAC role name/id —
|
||||||
|
* never a UI display label like "Super Admin".
|
||||||
|
*/
|
||||||
export function teamRoleNameForInvite(teamRole: string, explicitName?: string): string {
|
export function teamRoleNameForInvite(teamRole: string, explicitName?: string): string {
|
||||||
const named = explicitName?.trim()
|
const raw = (teamRole || explicitName || "").trim()
|
||||||
if (named) return named
|
if (!raw) return ""
|
||||||
|
|
||||||
const fromOptions = TEAM_ROLE_OPTIONS.find(
|
// Accidental display labels from built-in options → API keys
|
||||||
(o) => o.value === teamRole || o.value === teamRole.toUpperCase(),
|
const byLabel = TEAM_ROLE_OPTIONS.find(
|
||||||
|
(o) => o.label.toLowerCase() === raw.toLowerCase(),
|
||||||
)
|
)
|
||||||
if (fromOptions) return fromOptions.label
|
if (byLabel) return byLabel.value
|
||||||
|
|
||||||
return teamRole.trim()
|
const byValue = TEAM_ROLE_OPTIONS.find(
|
||||||
|
(o) => o.value === raw || o.value === raw.toUpperCase(),
|
||||||
|
)
|
||||||
|
if (byValue) return byValue.value
|
||||||
|
|
||||||
|
// Custom RBAC roles: keep exact name (spaces allowed); numeric id strings pass through
|
||||||
|
return raw
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TeamRoleOption = { value: string; label: string }
|
export type TeamRoleOption = { value: string; label: string }
|
||||||
|
|
@ -62,5 +73,8 @@ export function rbacRolesToTeamRoleOptions(roles: Role[]): TeamRoleOption[] {
|
||||||
new Set(roles.map((role) => role.name.trim()).filter(Boolean)),
|
new Set(roles.map((role) => role.name.trim()).filter(Boolean)),
|
||||||
).sort((a, b) => a.localeCompare(b))
|
).sort((a, b) => a.localeCompare(b))
|
||||||
|
|
||||||
return names.map((name) => ({ value: name, label: name }))
|
return names.map((name) => ({
|
||||||
|
value: name,
|
||||||
|
label: formatTeamRoleLabel(name),
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { useEffect, useMemo, useState } from "react"
|
||||||
import { Mail, Shield } from "lucide-react"
|
import { Mail, Shield } from "lucide-react"
|
||||||
import { toast } from "sonner"
|
import { toast } from "sonner"
|
||||||
import { fetchAllRoles } from "../../../api/rbac.api"
|
import { fetchAllRoles } from "../../../api/rbac.api"
|
||||||
import { inviteTeamMember } from "../../../api/team.api"
|
import { fetchAllTeamMemberEmails, inviteTeamMember } from "../../../api/team.api"
|
||||||
import { Button } from "../../../components/ui/button"
|
import { Button } from "../../../components/ui/button"
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
|
|
@ -54,6 +54,9 @@ export function InviteTeamMemberDialog({
|
||||||
const [teamRole, setTeamRole] = useState(lockedRole)
|
const [teamRole, setTeamRole] = useState(lockedRole)
|
||||||
const [loadedRoleOptions, setLoadedRoleOptions] = useState<TeamRoleOption[]>([])
|
const [loadedRoleOptions, setLoadedRoleOptions] = useState<TeamRoleOption[]>([])
|
||||||
const [rolesLoading, setRolesLoading] = useState(false)
|
const [rolesLoading, setRolesLoading] = useState(false)
|
||||||
|
const [existingEmails, setExistingEmails] = useState<Set<string>>(new Set())
|
||||||
|
const [existingEmailsLoading, setExistingEmailsLoading] = useState(false)
|
||||||
|
const [existingEmailsError, setExistingEmailsError] = useState(false)
|
||||||
const [submitting, setSubmitting] = useState(false)
|
const [submitting, setSubmitting] = useState(false)
|
||||||
const [progress, setProgress] = useState<{ current: number; total: number } | null>(
|
const [progress, setProgress] = useState<{ current: number; total: number } | null>(
|
||||||
null,
|
null,
|
||||||
|
|
@ -65,6 +68,14 @@ export function InviteTeamMemberDialog({
|
||||||
() => parsedEmails.filter((e) => !isValidInviteEmail(e)),
|
() => parsedEmails.filter((e) => !isValidInviteEmail(e)),
|
||||||
[parsedEmails],
|
[parsedEmails],
|
||||||
)
|
)
|
||||||
|
const alreadyRegisteredEmails = useMemo(
|
||||||
|
() =>
|
||||||
|
parsedEmails.filter(
|
||||||
|
(email) => isValidInviteEmail(email) && existingEmails.has(email.toLowerCase()),
|
||||||
|
),
|
||||||
|
[parsedEmails, existingEmails],
|
||||||
|
)
|
||||||
|
const hasEmailConflicts = alreadyRegisteredEmails.length > 0
|
||||||
|
|
||||||
const fallbackRoleOptions = useMemo<TeamRoleOption[]>(
|
const fallbackRoleOptions = useMemo<TeamRoleOption[]>(
|
||||||
() => TEAM_ROLE_OPTIONS.map((opt) => ({ value: opt.value, label: opt.label })),
|
() => TEAM_ROLE_OPTIONS.map((opt) => ({ value: opt.value, label: opt.label })),
|
||||||
|
|
@ -99,6 +110,31 @@ export function InviteTeamMemberDialog({
|
||||||
}
|
}
|
||||||
}, [open, roleLocked, roleOptions])
|
}, [open, roleLocked, roleOptions])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) return
|
||||||
|
|
||||||
|
let cancelled = false
|
||||||
|
setExistingEmailsLoading(true)
|
||||||
|
setExistingEmailsError(false)
|
||||||
|
void fetchAllTeamMemberEmails()
|
||||||
|
.then((emails) => {
|
||||||
|
if (cancelled) return
|
||||||
|
setExistingEmails(emails)
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (cancelled) return
|
||||||
|
setExistingEmails(new Set())
|
||||||
|
setExistingEmailsError(true)
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (!cancelled) setExistingEmailsLoading(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true
|
||||||
|
}
|
||||||
|
}, [open])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!open) return
|
if (!open) return
|
||||||
setEmailsText("")
|
setEmailsText("")
|
||||||
|
|
@ -142,8 +178,9 @@ export function InviteTeamMemberDialog({
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
// Always send the role key/name (SUPER_ADMIN), never the display label (Super Admin).
|
||||||
const role = roleLocked ? lockedRole : teamRole
|
const role = roleLocked ? lockedRole : teamRole
|
||||||
const roleName = teamRoleNameForInvite(role, presetRoleLabel)
|
const roleName = teamRoleNameForInvite(role)
|
||||||
|
|
||||||
if (parsedEmails.length === 0) {
|
if (parsedEmails.length === 0) {
|
||||||
toast.error("Enter at least one email address")
|
toast.error("Enter at least one email address")
|
||||||
|
|
@ -153,10 +190,22 @@ export function InviteTeamMemberDialog({
|
||||||
toast.error(`Invalid email: ${invalidEmails.join(", ")}`)
|
toast.error(`Invalid email: ${invalidEmails.join(", ")}`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (hasEmailConflicts) {
|
||||||
|
toast.error(
|
||||||
|
alreadyRegisteredEmails.length === 1
|
||||||
|
? `${alreadyRegisteredEmails[0]} is already a team member`
|
||||||
|
: `${alreadyRegisteredEmails.length} emails are already team members`,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
if (!role) {
|
if (!role) {
|
||||||
toast.error("Team role is required")
|
toast.error("Team role is required")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (existingEmailsLoading) {
|
||||||
|
toast.error("Still checking existing team emails. Try again in a moment.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
setSubmitting(true)
|
setSubmitting(true)
|
||||||
setResults(null)
|
setResults(null)
|
||||||
|
|
@ -178,6 +227,14 @@ export function InviteTeamMemberDialog({
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (succeeded.length > 0) {
|
||||||
|
setExistingEmails((prev) => {
|
||||||
|
const next = new Set(prev)
|
||||||
|
for (const row of succeeded) next.add(row.email.toLowerCase())
|
||||||
|
return next
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (succeeded.length === 0) {
|
if (succeeded.length === 0) {
|
||||||
const firstFailed = failed[0]
|
const firstFailed = failed[0]
|
||||||
toast.error(firstFailed?.message ?? getApiErrorMessage(null, "No invitations were sent"))
|
toast.error(firstFailed?.message ?? getApiErrorMessage(null, "No invitations were sent"))
|
||||||
|
|
@ -247,7 +304,28 @@ export function InviteTeamMemberDialog({
|
||||||
· {invalidEmails.length} invalid
|
· {invalidEmails.length} invalid
|
||||||
</span>
|
</span>
|
||||||
) : null}
|
) : null}
|
||||||
|
{existingEmailsLoading ? (
|
||||||
|
<span className="text-grayScale-400"> · Checking existing members…</span>
|
||||||
|
) : null}
|
||||||
|
{existingEmailsError ? (
|
||||||
|
<span className="text-amber-600">
|
||||||
|
{" "}
|
||||||
|
· Couldn’t verify existing emails; server will reject duplicates
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
</p>
|
</p>
|
||||||
|
{hasEmailConflicts ? (
|
||||||
|
<div className="mt-2 rounded-lg border border-destructive/20 bg-destructive/5 px-3 py-2 text-xs text-destructive">
|
||||||
|
<p className="font-semibold">Already registered — remove before sending:</p>
|
||||||
|
<ul className="mt-1 list-inside list-disc space-y-0.5">
|
||||||
|
{alreadyRegisteredEmails.map((email) => (
|
||||||
|
<li key={email} className="font-mono">
|
||||||
|
{email}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
|
|
@ -327,7 +405,13 @@ export function InviteTeamMemberDialog({
|
||||||
<Button
|
<Button
|
||||||
type="submit"
|
type="submit"
|
||||||
className="bg-brand-500 text-white hover:bg-brand-600"
|
className="bg-brand-500 text-white hover:bg-brand-600"
|
||||||
disabled={submitting || parsedEmails.length === 0}
|
disabled={
|
||||||
|
submitting ||
|
||||||
|
parsedEmails.length === 0 ||
|
||||||
|
invalidEmails.length > 0 ||
|
||||||
|
hasEmailConflicts ||
|
||||||
|
existingEmailsLoading
|
||||||
|
}
|
||||||
>
|
>
|
||||||
{submitting
|
{submitting
|
||||||
? progress
|
? progress
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user