Skip to content

Commit

Permalink
Don't allow inviting existing admins or invitees (#8414)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccschmitz committed May 2, 2024
1 parent 0e1be60 commit 0f449ad
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
24 changes: 24 additions & 0 deletions backend/private-graph/graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,30 @@ func (r *mutationResolver) SendAdminWorkspaceInvite(ctx context.Context, workspa
}
}

// Check if an invite to the email address already exists
var existingInvite model.WorkspaceInviteLink
if err := r.DB.WithContext(ctx).Where("workspace_id = ? AND invitee_email ILIKE ?", workspaceID, email).First(&existingInvite).Error; err != gorm.ErrRecordNotFound {
if err != nil {
return nil, e.Wrap(err, "error checking for existing invite link")
}
return nil, e.Errorf("Looks like \"%s\" has already been invited to join this workspace.", email)
}

// Check if the email is already assigned to an admin in the workspace
var existingAdmin model.Admin
if err := r.DB.WithContext(ctx).Where("email ILIKE ?", email).First(&existingAdmin).Error; err != gorm.ErrRecordNotFound {
if err != nil {
return nil, e.Wrap(err, "error checking for existing admin")
}
var workspaceAdmin model.WorkspaceAdmin
if err := r.DB.WithContext(ctx).Where("admin_id = ? AND workspace_id = ?", existingAdmin.ID, workspaceID).First(&workspaceAdmin).Error; err != gorm.ErrRecordNotFound {
if err != nil {
return nil, e.Wrap(err, "error checking for existing admin in workspace")
}
return nil, e.Errorf("Looks like \"%s\" is already an admin in this workspace.", email)
}
}

inviteLink := r.CreateInviteLink(workspaceID, &email, role, false)

if err := r.DB.WithContext(ctx).Create(inviteLink).Error; err != nil {
Expand Down
27 changes: 21 additions & 6 deletions frontend/src/pages/WorkspaceTeam/components/InviteMemberModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ function InviteMemberModal({

const [
sendInviteEmail,
{ loading: sendLoading, data: sendInviteEmailData, reset: sendReset },
] = useSendAdminWorkspaceInviteMutation()
{
loading: sendLoading,
data: sendData,
error: sendError,
reset: sendReset,
},
] = useSendAdminWorkspaceInviteMutation({
fetchPolicy: 'no-cache',
})

const onSubmit = (e: { preventDefault: () => void }) => {
e.preventDefault()
Expand Down Expand Up @@ -94,6 +101,7 @@ function InviteMemberModal({
className={styles.emailInput}
placeholder="Email"
type="email"
required
name="invitedEmail"
autoFocus
value={email}
Expand Down Expand Up @@ -143,7 +151,7 @@ function InviteMemberModal({
</Button>
</div>
</form>
{sendInviteEmailData?.sendAdminWorkspaceInvite && (
{sendData?.sendAdminWorkspaceInvite && (
<Alert
shouldAlwaysShow
trackingId="InviteAdminToWorkspaceConfirmation"
Expand All @@ -154,9 +162,7 @@ function InviteMemberModal({
You can also share with them this link:{' '}
<span>
<CopyText
text={
sendInviteEmailData.sendAdminWorkspaceInvite
}
text={sendData.sendAdminWorkspaceInvite}
onCopyTooltipText="Copied invite link to clipboard!"
inline
/>
Expand All @@ -165,6 +171,15 @@ function InviteMemberModal({
}
/>
)}
{sendError && (
<Alert
shouldAlwaysShow
trackingId="InviteAdminToWorkspaceError"
message="Couldn't send workspace invite"
type="error"
description={sendError.message}
/>
)}
<hr className={styles.hr} />
<p className={styles.boxSubTitle}>
Or share this link with them (this link expires{' '}
Expand Down

0 comments on commit 0f449ad

Please sign in to comment.