Skip to content

Commit 1d6df18

Browse files
committed
feat(authentication): delete invitation endpoint for teams
1 parent 0c6eaaf commit 1d6df18

File tree

1 file changed

+55
-0
lines changed
  • modules/authentication/src/handlers

1 file changed

+55
-0
lines changed

modules/authentication/src/handlers/team.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,45 @@ export class TeamsHandler implements IAuthenticationStrategy {
616616
return invitation.token;
617617
}
618618

619+
async deleteInvitation(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
620+
const { user } = call.request.context;
621+
const { teamId } = call.request.urlParams;
622+
const { email } = call.request.queryParams;
623+
const config: Config = ConfigController.getInstance().config;
624+
if (!config.teams.invites.enabled) {
625+
throw new GrpcError(status.PERMISSION_DENIED, 'Team invites are disabled');
626+
}
627+
const team = await Team.getInstance().findOne({ _id: teamId });
628+
if (!team) {
629+
throw new GrpcError(
630+
status.INVALID_ARGUMENT,
631+
'Could not create invite, team does not exist',
632+
);
633+
}
634+
635+
const can = await this.grpcSdk.authorization!.can({
636+
subject: 'User:' + user._id,
637+
actions: ['invite'],
638+
resource: 'Team:' + teamId,
639+
});
640+
if (!can.allow) {
641+
throw new GrpcError(
642+
status.PERMISSION_DENIED,
643+
'You do not have permission to delete invites from this team',
644+
);
645+
}
646+
647+
// Delete any existing invite for the same email and team
648+
await Token.getInstance().deleteOne({
649+
tokenType: TokenType.TEAM_INVITE_TOKEN,
650+
// @ts-expect-error Unsafe nested property access
651+
'data.teamId': teamId,
652+
'data.email': email,
653+
});
654+
655+
return 'OK';
656+
}
657+
619658
async getTeamInvites(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
620659
const { user } = call.request.context;
621660
const { teamId } = call.request.urlParams;
@@ -1021,6 +1060,22 @@ export class TeamsHandler implements IAuthenticationStrategy {
10211060
new ConduitRouteReturnDefinition('InvitationToken', 'String'),
10221061
this.userInvite.bind(this),
10231062
);
1063+
routingManager.route(
1064+
{
1065+
path: '/teams/:teamId/invite',
1066+
description: `Deletes an invite previously sent.`,
1067+
urlParams: {
1068+
teamId: ConduitObjectId.Required,
1069+
},
1070+
queryParams: {
1071+
email: ConduitString.Required,
1072+
},
1073+
action: ConduitRouteActions.DELETE,
1074+
middlewares: authRouteMiddlewares,
1075+
},
1076+
new ConduitRouteReturnDefinition('DeleteInvitation', 'String'),
1077+
this.deleteInvitation.bind(this),
1078+
);
10241079
routingManager.route(
10251080
{
10261081
path: '/teams/:teamId/invite/persistent',

0 commit comments

Comments
 (0)