fix: reconcile direct invites after share-link join

main
Binks Riverton 2026-08-29 19:01:43 +08:00 committed by lyingbug
parent 4eb64da5ad
commit 1cb140da9d
3 changed files with 85 additions and 1 deletions

View File

@ -287,6 +287,23 @@ func (s *tenantInvitationService) MarkPendingAcceptedIfExists(
return s.repo.MarkStatusIfPending(ctx, inv.ID, types.TenantInvitationStatusAccepted, s.now())
}
// reconcilePendingInvitation closes a per-user invitation after another
// successful join path (for example a multi-use share link) has already
// established the membership. This keeps the invitation inbox consistent
// without turning a bookkeeping failure into a failed join after the member
// row has already been committed.
func (s *tenantInvitationService) reconcilePendingInvitation(
ctx context.Context,
tenantID uint64,
inviteeUserID string,
) {
if err := s.MarkPendingAcceptedIfExists(ctx, tenantID, inviteeUserID); err != nil {
logger.Warnf(ctx,
"failed to reconcile pending invitation for tenant %d user %s: %v",
tenantID, inviteeUserID, err)
}
}
// emitInvitationAccepted writes the rbac.invitation_accepted audit row.
// Actor is the invitee (acting on their own inbox); target is the same
// user since the action is self-directed.
@ -578,6 +595,7 @@ func (s *tenantInvitationService) AcceptByToken(
if errors.Is(err, ErrMembershipAlreadyExists) {
existing, getErr := s.memberSvc.GetMembership(ctx, newUserID, inv.TenantID)
if getErr == nil && existing != nil {
s.reconcilePendingInvitation(ctx, inv.TenantID, newUserID)
return existing, nil
}
}
@ -595,6 +613,10 @@ func (s *tenantInvitationService) AcceptByToken(
"share-link %d accepted_count bump failed (membership still created): %v",
inv.ID, incErr)
}
// A user may have received a direct invitation and then joined through
// a share link first. Close that direct invitation now so its notification
// cannot be accepted a second time and produce a misleading audit event.
s.reconcilePendingInvitation(ctx, inv.TenantID, newUserID)
s.emitAudit(ctx, &types.AuditLog{
TenantID: inv.TenantID,
ActorUserID: auditActor(ctx),

View File

@ -558,6 +558,66 @@ func TestInvitationService_AcceptByToken_AllowsMultipleUsers(t *testing.T) {
}
}
func TestInvitationService_AcceptByToken_ReconcilesDirectInvitation(t *testing.T) {
svc, repo, _ := newInvitationSvc()
ctx := context.Background()
direct, err := svc.Create(ctx, 1, "u-alice", types.TenantRoleViewer, nil, "")
if err != nil {
t.Fatalf("create direct invitation: %v", err)
}
_, plain, err := svc.CreateShareLink(ctx, 1, types.TenantRoleViewer, nil, "")
if err != nil {
t.Fatalf("create share link: %v", err)
}
if _, err := svc.AcceptByToken(ctx, plain, "u-alice"); err != nil {
t.Fatalf("accept by token: %v", err)
}
got, err := repo.GetByID(ctx, direct.ID)
if err != nil {
t.Fatalf("get direct invitation: %v", err)
}
if got.Status != types.TenantInvitationStatusAccepted {
t.Fatalf("direct invitation must be reconciled as accepted, got %s", got.Status)
}
pending, err := svc.CountPendingByInvitee(ctx, "u-alice")
if err != nil {
t.Fatalf("count pending: %v", err)
}
if pending != 0 {
t.Fatalf("joined user must not retain an actionable invitation, got %d", pending)
}
}
func TestInvitationService_AcceptByToken_ReconcilesDirectInvitationForExistingMember(t *testing.T) {
svc, repo, memberSvc := newInvitationSvc()
ctx := context.Background()
direct, err := svc.Create(ctx, 1, "u-alice", types.TenantRoleViewer, nil, "")
if err != nil {
t.Fatalf("create direct invitation: %v", err)
}
_, plain, err := svc.CreateShareLink(ctx, 1, types.TenantRoleViewer, nil, "")
if err != nil {
t.Fatalf("create share link: %v", err)
}
if _, err := memberSvc.AddMember(ctx, "u-alice", 1, types.TenantRoleViewer, nil); err != nil {
t.Fatalf("seed membership: %v", err)
}
if _, err := svc.AcceptByToken(ctx, plain, "u-alice"); err != nil {
t.Fatalf("idempotent accept by token: %v", err)
}
got, err := repo.GetByID(ctx, direct.ID)
if err != nil {
t.Fatalf("get direct invitation: %v", err)
}
if got.Status != types.TenantInvitationStatusAccepted {
t.Fatalf("direct invitation must be reconciled for an existing member, got %s", got.Status)
}
}
func TestInvitationService_AcceptByToken_RevokedTokenRejected(t *testing.T) {
svc, _, _ := newInvitationSvc()
ctx := context.Background()

View File

@ -92,7 +92,9 @@ type TenantInvitationService interface {
// the invitation row into a terminal state — share-link rows stay
// pending so subsequent invitees can also register through the
// same link. Idempotent: if the user already has membership, the
// existing row is returned.
// existing row is returned. If the same user also has a pending direct
// invitation for the tenant, it is reconciled to accepted so the inbox
// cannot offer a second acceptance after membership already exists.
AcceptByToken(ctx context.Context, plainToken string, newUserID string) (*types.TenantMember, error)
// MarkPendingAcceptedIfExists transitions a per-user pending invitation