78 lines
2.8 KiB
TypeScript
78 lines
2.8 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { MailerService } from '@nestjs-modules/mailer';
|
|
|
|
@Injectable()
|
|
export class PortalMailService {
|
|
constructor(
|
|
private readonly mailer: MailerService,
|
|
private readonly config: ConfigService,
|
|
) {}
|
|
|
|
async sendVerificationMail(to: string, token: string): Promise<void> {
|
|
const url = `${this.publicWebUrl}/verify-email?token=${encodeURIComponent(token)}`;
|
|
await this.mailer.sendMail({
|
|
to,
|
|
subject: 'LDAP Portal: E-Mail bestaetigen',
|
|
html: `<p>Bitte bestaetige deine Registrierung:</p><p><a href="${url}">${url}</a></p>`,
|
|
text: `Bitte bestaetige deine Registrierung: ${url}`,
|
|
});
|
|
}
|
|
|
|
async sendPasswordResetMail(to: string, token: string): Promise<void> {
|
|
const url = `${this.publicWebUrl}/reset-password?token=${encodeURIComponent(token)}`;
|
|
await this.mailer.sendMail({
|
|
to,
|
|
subject: 'LDAP Portal: Passwort zuruecksetzen',
|
|
html: `<p>Du kannst dein Passwort ueber diesen Link zuruecksetzen:</p><p><a href="${url}">${url}</a></p>`,
|
|
text: `Du kannst dein Passwort ueber diesen Link zuruecksetzen: ${url}`,
|
|
});
|
|
}
|
|
|
|
async sendEmailChangeMail(to: string, token: string): Promise<void> {
|
|
const url = `${this.publicWebUrl}/account/email?token=${encodeURIComponent(token)}`;
|
|
await this.mailer.sendMail({
|
|
to,
|
|
subject: 'LDAP Portal: neue E-Mail bestaetigen',
|
|
html: `<p>Bitte bestaetige deine neue E-Mail-Adresse:</p><p><a href="${url}">${url}</a></p>`,
|
|
text: `Bitte bestaetige deine neue E-Mail-Adresse: ${url}`,
|
|
});
|
|
}
|
|
|
|
async sendRegistrationPendingApprovalMail(
|
|
to: string[],
|
|
registration: { email: string; displayName: string },
|
|
): Promise<void> {
|
|
const url = `${this.publicWebUrl}/admin/registrations`;
|
|
await this.mailer.sendMail({
|
|
to,
|
|
subject: 'LDAP Portal: Registrierung wartet auf Freigabe',
|
|
html: `
|
|
<p>Eine Registrierung wurde per E-Mail bestaetigt und wartet jetzt auf Freigabe.</p>
|
|
<p><strong>Name:</strong> ${this.escapeHtml(registration.displayName)}<br>
|
|
<strong>E-Mail:</strong> ${this.escapeHtml(registration.email)}</p>
|
|
<p><a href="${url}">${url}</a></p>
|
|
`,
|
|
text: [
|
|
'Eine Registrierung wurde per E-Mail bestaetigt und wartet jetzt auf Freigabe.',
|
|
`Name: ${registration.displayName}`,
|
|
`E-Mail: ${registration.email}`,
|
|
`Admin-Bereich: ${url}`,
|
|
].join('\n'),
|
|
});
|
|
}
|
|
|
|
private get publicWebUrl(): string {
|
|
return this.config.get<string>('PUBLIC_WEB_URL') ?? 'http://localhost:4200';
|
|
}
|
|
|
|
private escapeHtml(value: string): string {
|
|
return value
|
|
.replaceAll('&', '&')
|
|
.replaceAll('<', '<')
|
|
.replaceAll('>', '>')
|
|
.replaceAll('"', '"')
|
|
.replaceAll("'", ''');
|
|
}
|
|
}
|