refactor: Enhance two-factor authentication verification logic and improve placeholder formatting in NotificationRuleForm

This commit is contained in:
Nawaz Dhandala
2025-10-01 20:28:18 +01:00
parent c6be5a9ebf
commit 426dda60fe
3 changed files with 48 additions and 7 deletions

View File

@@ -9,6 +9,8 @@ import User from "../../Models/DatabaseModels/User";
import DeleteBy from "../Types/Database/DeleteBy";
import LIMIT_MAX from "../../Types/Database/LimitMax";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
import UserWebAuthn from "../../Models/DatabaseModels/UserWebAuthn";
import UserWebAuthnService from "./UserWebAuthnService";
export class Service extends DatabaseService<Model> {
public constructor() {
@@ -91,9 +93,9 @@ export class Service extends DatabaseService<Model> {
}
if (user.enableTwoFactorAuth) {
// if enabled then check if this is the only verified item for this user.
// if enabled then check if this is the only verified 2FA method for this user.
const verifiedItems: Array<Model> = await this.findBy({
const verifiedTotpItems: Array<Model> = await this.findBy({
query: {
userId: item.userId!,
isVerified: true,
@@ -106,7 +108,24 @@ export class Service extends DatabaseService<Model> {
props: deleteBy.props,
});
if (verifiedItems.length === 1) {
const verifiedWebAuthnItems: Array<UserWebAuthn> =
await UserWebAuthnService.findBy({
query: {
userId: item.userId!,
isVerified: true,
},
select: {
_id: true,
},
limit: LIMIT_MAX,
skip: 0,
props: deleteBy.props,
});
const totalVerified2FA: number =
verifiedTotpItems.length + verifiedWebAuthnItems.length;
if (totalVerified2FA === 1) {
throw new BadDataException(
"You must have atleast one verified two factor auth. Please disable two factor auth before deleting this item.",
);

View File

@@ -17,6 +17,8 @@ import {
import { Host, HttpProtocol } from "../EnvironmentConfig";
import ObjectID from "../../Types/ObjectID";
import DatabaseCommonInteractionProps from "../../Types/BaseDatabase/DatabaseCommonInteractionProps";
import UserTotpAuth from "../../Models/DatabaseModels/UserTotpAuth";
import UserTotpAuthService from "./UserTotpAuthService";
export class Service extends DatabaseService<Model> {
public constructor() {
@@ -366,9 +368,9 @@ export class Service extends DatabaseService<Model> {
}
if (user.enableTwoFactorAuth) {
// if enabled then check if this is the only verified item for this user.
// if enabled then check if this is the only verified 2FA method for this user.
const verifiedItems: Array<Model> = await this.findBy({
const verifiedWebAuthnItems: Array<Model> = await this.findBy({
query: {
userId: item.userId!,
isVerified: true,
@@ -381,7 +383,24 @@ export class Service extends DatabaseService<Model> {
props: deleteBy.props,
});
if (verifiedItems.length === 1) {
const verifiedTotpItems: Array<UserTotpAuth> =
await UserTotpAuthService.findBy({
query: {
userId: item.userId!,
isVerified: true,
},
select: {
_id: true,
},
limit: LIMIT_MAX,
skip: 0,
props: deleteBy.props,
});
const totalVerified2FA: number =
verifiedWebAuthnItems.length + verifiedTotpItems.length;
if (totalVerified2FA === 1) {
throw new BadDataException(
"You must have atleast one verified two factor auth. Please disable two factor auth before deleting this item.",
);

View File

@@ -153,7 +153,10 @@ const NotificationRuleForm: FunctionComponent<ComponentProps> = (
title: `Existing ${getWorkspaceTypeDisplayName(props.workspaceType)} Channel Name to Post To`,
description: `Please provide the name of the ${getWorkspaceTypeDisplayName(props.workspaceType)} channel you want to post to.`,
fieldType: FormFieldSchemaType.Text,
placeholder: props.workspaceType === WorkspaceType.MicrosoftTeams ? `channel-name, General, etc.` : `#channel-name, #general, etc.`,
placeholder:
props.workspaceType === WorkspaceType.MicrosoftTeams
? `channel-name, General, etc.`
: `#channel-name, #general, etc.`,
required: true,
showIf: (formValue: FormValues<BaseNotificationRule>) => {
return Boolean(formValue.shouldPostToExistingChannel) || false;