Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
120 lines
3.8 KiB
TypeScript
120 lines
3.8 KiB
TypeScript
import type { ISocketConnection } from '@rocket.chat/core-typings';
|
|
import { Users } from '@rocket.chat/models';
|
|
import { Accounts } from 'meteor/accounts-base';
|
|
import { Meteor } from 'meteor/meteor';
|
|
import moment from 'moment';
|
|
import { UAParser } from 'ua-parser-js';
|
|
|
|
import { t } from '../../../../app/utils/lib/i18n';
|
|
import * as Mailer from '../../../../server/lib/notifications/email/api';
|
|
import { UAParserDesktop, UAParserMobile } from '../../../../server/lib/statistics/lib/UAParserCustom';
|
|
import { getUserPreference } from '../../../../server/lib/utils/lib/getUserPreference';
|
|
import { deviceManagementEvents } from '../../../../server/services/device-management/events';
|
|
import { settings } from '../../../../server/settings';
|
|
|
|
let mailTemplates: string;
|
|
|
|
Meteor.startup(() => {
|
|
Mailer.getTemplate('Device_Management_Email_Body', (template) => {
|
|
mailTemplates = template;
|
|
});
|
|
});
|
|
|
|
const uaParser = async (
|
|
uaString: ISocketConnection['httpHeaders']['user-agent'],
|
|
): Promise<UAParser.IResult & { app?: { name: string; version: string; bundle: string } }> => {
|
|
const ua = new UAParser(uaString);
|
|
return {
|
|
...ua.getResult(),
|
|
...(uaString && UAParserMobile.isMobileApp(uaString) && UAParserMobile.uaObject(uaString)),
|
|
...(uaString && UAParserDesktop.isDesktopApp(uaString) && UAParserDesktop.uaObject(uaString)),
|
|
};
|
|
};
|
|
|
|
export const listenSessionLogin = () => {
|
|
return deviceManagementEvents.on('device-login', async ({ userId, userAgent, clientAddress }) => {
|
|
const deviceEnabled = settings.get('Device_Management_Enable_Login_Emails');
|
|
|
|
if (!deviceEnabled) {
|
|
return;
|
|
}
|
|
|
|
const user = await Users.findOneByIdWithEmailAddress(userId, {
|
|
projection: { 'name': 1, 'username': 1, 'emails': 1, 'settings.preferences.receiveLoginDetectionEmail': 1 },
|
|
});
|
|
|
|
if (!user?.emails?.length) {
|
|
return;
|
|
}
|
|
|
|
const userReceiveLoginEmailPreference =
|
|
!settings.get('Device_Management_Allow_Login_Email_preference') ||
|
|
(await getUserPreference(userId, 'receiveLoginDetectionEmail', true));
|
|
|
|
if (!userReceiveLoginEmailPreference) {
|
|
return;
|
|
}
|
|
|
|
const dateFormat = settings.get('Message_TimeAndDateFormat');
|
|
|
|
const {
|
|
name,
|
|
username,
|
|
emails: [{ address: email }],
|
|
} = user;
|
|
|
|
const { browser, os, device, cpu, app } = await uaParser(userAgent);
|
|
|
|
const mailData = {
|
|
name,
|
|
username,
|
|
browserInfo: `${browser.name} ${browser.version}`,
|
|
osInfo: `${os.name}`,
|
|
deviceInfo: `${device.type || t('Device_Management_Device_Unknown')} ${device.vendor || ''} ${device.model || ''} ${
|
|
cpu.architecture || ''
|
|
}`,
|
|
ipInfo: clientAddress,
|
|
userAgent: '',
|
|
date: moment().format(String(dateFormat)),
|
|
};
|
|
|
|
switch (device.type) {
|
|
case 'mobile':
|
|
case 'tablet':
|
|
case 'smarttv':
|
|
mailData.browserInfo = `${browser.name} ${browser.version}`;
|
|
mailData.osInfo = `${os.name}`;
|
|
mailData.deviceInfo = `${device.type} ${device.vendor || ''} ${device.model || ''} ${cpu.architecture || ''}`;
|
|
break;
|
|
case 'mobile-app':
|
|
mailData.browserInfo = `Rocket.Chat App ${app?.bundle || app?.version}`;
|
|
mailData.osInfo = `${os.name}`;
|
|
mailData.deviceInfo = 'Mobile App';
|
|
break;
|
|
case 'desktop-app':
|
|
mailData.browserInfo = `Rocket.Chat ${app?.name || browser.name} ${app?.bundle || app?.version || browser.version}`;
|
|
mailData.osInfo = `${os.name}`;
|
|
mailData.deviceInfo = `Desktop App ${cpu.architecture || ''}`;
|
|
break;
|
|
default:
|
|
mailData.userAgent = userAgent || '';
|
|
break;
|
|
}
|
|
|
|
try {
|
|
await Mailer.send({
|
|
to: `${name} <${email}>`,
|
|
from: Accounts.emailTemplates.from,
|
|
subject: settings.get('Device_Management_Email_Subject'),
|
|
html: mailTemplates,
|
|
data: mailData,
|
|
});
|
|
} catch ({ message }: any) {
|
|
throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${message}`, {
|
|
method: 'listenSessionLogin',
|
|
message,
|
|
});
|
|
}
|
|
});
|
|
};
|