Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
feat(backend:users): add email notifications when account is locked
johaven954bb1061e63
diff --git a/backend/src/applications/notifications/constants/notifications.ts b/backend/src/applications/notifications/constants/notifications.tsindex 0f61379d..cc0bbb40 100644--- a/backend/src/applications/notifications/constants/notifications.ts+++ b/backend/src/applications/notifications/constants/notifications.ts@@ -13,6 +13,7 @@ export enum NOTIFICATION_APP {SHARES = 'shares',LINKS = 'links',SYNC = 'sync',+ AUTH_LOCKED = 'auth_locked',AUTH_2FA = 'auth_2fa'}@@ -46,5 +47,8 @@ export const NOTIFICATION_APP_EVENT = {AUTH_2FA: {[ACTION.ADD]: 'Two-factor authentication (2FA) on your account has been enabled',[ACTION.DELETE]: 'Two-factor authentication (2FA) on your account has been disabled'+ },+ AUTH_LOCKED: {+ [ACTION.DELETE]: 'Your account has been locked after several unsuccessful authentication attempts'}}diff --git a/backend/src/applications/notifications/i18n/fr.ts b/backend/src/applications/notifications/i18n/fr.tsindex 19de5767..5434a98a 100644--- a/backend/src/applications/notifications/i18n/fr.ts+++ b/backend/src/applications/notifications/i18n/fr.ts@@ -34,6 +34,9 @@ export const fr = {'Access your syncs from': 'Accédez à vos synchronisations depuis','You are no longer synchronizing': 'Vous ne synchronisez plus','Security notification': 'Notification de sécurité',+ 'Your account has been locked after several unsuccessful authentication attempts':+ 'Votre compte a été verrouillé après plusieurs tentatives d’authentification infructueuses',+ 'This security notification concerns your Sync-in account. Please contact an administrator to perform the analysis and unlock your account.': `Cette notification de sécurité concerne votre compte Sync-in. Merci de contacter un administrateur afin qu’il procède à l’analyse et au déverrouillage de votre compte.`,'Two-factor authentication (2FA) on your account has been disabled': `L’authentification à deux facteurs (2FA) sur votre compte a été désactivée`,'Two-factor authentication (2FA) on your account has been enabled': `L’authentification à deux facteurs (2FA) sur votre compte a été activée`,'You received this notification because the security of your Sync-in account has changed. If you think this was a mistake, please review your security settings or contact your administrator.': `Vous recevez cette notification car la sécurité de votre compte Sync-in a été modifiée. Si vous pensez qu’il s’agit d’une erreur, vérifiez vos paramètres de sécurité ou contactez votre administrateur.`,diff --git a/backend/src/applications/notifications/mails/models.ts b/backend/src/applications/notifications/mails/models.tsindex 70c46c3f..3fbe6338 100644--- a/backend/src/applications/notifications/mails/models.ts+++ b/backend/src/applications/notifications/mails/models.ts@@ -182,3 +182,19 @@ export function auth2FaMail(language: string, notification: NotificationContent)return [tr.title, mailTemplate(content, footer)]}++export function authLocked(language: string, notification: NotificationContent): [string, string] {+ const tr = translateObject(language, {+ title: 'Security notification',+ footer:+ 'This security notification concerns your Sync-in account. Please contact an administrator to perform the analysis and unlock your account.',+ event: notification.event,+ addressIp: 'Address IP'+ })++ const content = `${tr.event}<br><br>${tr.addressIp}: ${notification.url}<br>`++ const footer = `<br>${tr.footer}<br>`++ return [tr.title, mailTemplate(content, footer)]+}diff --git a/backend/src/applications/notifications/services/notifications-manager.service.ts b/backend/src/applications/notifications/services/notifications-manager.service.tsindex 9948b8a5..cf4e5827 100644--- a/backend/src/applications/notifications/services/notifications-manager.service.ts+++ b/backend/src/applications/notifications/services/notifications-manager.service.ts@@ -115,6 +115,8 @@ export class NotificationsManager {return syncMail(language, content, { currentUrl: options.currentUrl, action: options.action })case NOTIFICATION_APP.AUTH_2FA:return auth2FaMail(language, content)+ case NOTIFICATION_APP.AUTH_LOCKED:+ return authLocked(language, content)default:this.logger.error(`${this.genMail.name} - case not handled : ${content.app}`)}diff --git a/backend/src/applications/users/services/users-manager.service.spec.ts b/backend/src/applications/users/services/users-manager.service.spec.tsindex 37751205..a1129fc4 100755--- a/backend/src/applications/users/services/users-manager.service.spec.ts+++ b/backend/src/applications/users/services/users-manager.service.spec.ts@@ -16,6 +16,7 @@ import { Cache } from '../../../infrastructure/cache/services/cache.service'import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'import * as filesUtilsModule from '../../files/utils/files'import { fileName, isPathExists } from '../../files/utils/files'+import { NotificationsManager } from '../../notifications/services/notifications-manager.service'import { MEMBER_TYPE } from '../constants/member'import { USER_GROUP_ROLE, USER_MAX_PASSWORD_ATTEMPTS, USER_ROLE } from '../constants/user'import { CreateUserDto } from '../dto/create-or-update-user.dto'@@ -69,6 +70,10 @@ describe(UsersManager.name, () => {}}+ const notificationsManager = {+ sendEmailNotification: jest.fn().mockResolvedValue(undefined)+ }+beforeAll(async () => {const module: TestingModule = await Test.createTestingModule({providers: [@@ -76,6 +81,7 @@ describe(UsersManager.name, () => {AdminUsersQueries,UsersManager,UsersQueries,+ { provide: NotificationsManager, useValue: notificationsManager },{ provide: AuthManager, useValue: {} },{ provide: DB_TOKEN_PROVIDER, useValue: {} },{ provide: Cache, useValue: {} }diff --git a/backend/src/applications/users/services/users-manager.service.ts b/backend/src/applications/users/services/users-manager.service.tsindex 19e59648..159b251d 100755--- a/backend/src/applications/users/services/users-manager.service.ts+++ b/backend/src/applications/users/services/users-manager.service.ts@@ -15,10 +15,13 @@ import { pipeline } from 'node:stream/promises'import { LoginResponseDto } from '../../../authentication/dto/login-response.dto'import { FastifyAuthenticatedRequest } from '../../../authentication/interfaces/auth-request.interface'import { JwtIdentityPayload } from '../../../authentication/interfaces/jwt-payload.interface'+import { ACTION } from '../../../common/constants'import { comparePassword } from '../../../common/functions'import { generateAvatar, pngMimeType, svgMimeType } from '../../../common/image'import { configuration, serverConfig } from '../../../configuration/config.environment'import { isPathExists, moveFiles } from '../../files/utils/files'+import { NOTIFICATION_APP, NOTIFICATION_APP_EVENT } from '../../notifications/constants/notifications'+import { NotificationsManager } from '../../notifications/services/notifications-manager.service'import { MEMBER_TYPE } from '../constants/member'import { USER_GROUP_ROLE, USER_MAX_PASSWORD_ATTEMPTS, USER_ONLINE_STATUS, USER_ROLE } from '../constants/user'import { UserCreateOrUpdateGroupDto } from '../dto/create-or-update-group.dto'@@ -45,7 +48,8 @@ export class UsersManager {constructor(public readonly usersQueries: UsersQueries,- private readonly adminUsersManager: AdminUsersManager+ private readonly adminUsersManager: AdminUsersManager,+ private readonly notificationsManager: NotificationsManager) {}async fromUserId(id: number): Promise<UserModel> {@@ -80,6 +84,7 @@ export class UsersManager {if (!user.isActive || user.passwordAttempts >= USER_MAX_PASSWORD_ATTEMPTS) {this.updateAccesses(user, ip, false).catch((e: Error) => this.logger.error(`${this.validateUserAccess.name} - ${e}`))this.logger.error(`${this.validateUserAccess.name} - user account *${user.login}* is locked`)+ this.notifyAccountLocked(user, ip)throw new HttpException('Account locked', HttpStatus.FORBIDDEN)}}@@ -422,4 +427,15 @@ export class UsersManager {searchMembers(user: UserModel, searchMembersDto: SearchMembersDto): Promise<Member[]> {return this.usersQueries.searchUsersOrGroups(searchMembersDto, user.id)}++ private notifyAccountLocked(user: UserModel, ip: string) {+ this.notificationsManager+ .sendEmailNotification([user], {+ app: NOTIFICATION_APP.AUTH_LOCKED,+ event: NOTIFICATION_APP_EVENT.AUTH_LOCKED[ACTION.DELETE],+ element: null,+ url: ip+ })+ .catch((e: Error) => this.logger.error(`${this.validateUserAccess.name} - ${e}`))+ }}