mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 09:36:44 +00:00
refactor(core): Move some request DTOs to @n8n/api-types (no-changelog) (#10880)
This commit is contained in:
committed by
GitHub
parent
583d3a7acb
commit
769ddfdd1d
@@ -1,12 +1,16 @@
|
||||
import {
|
||||
PasswordUpdateRequestDto,
|
||||
SettingsUpdateRequestDto,
|
||||
UserUpdateRequestDto,
|
||||
} from '@n8n/api-types';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { type RequestHandler, Response } from 'express';
|
||||
import validator from 'validator';
|
||||
|
||||
import { AuthService } from '@/auth/auth.service';
|
||||
import type { User } from '@/databases/entities/user';
|
||||
import { UserRepository } from '@/databases/repositories/user.repository';
|
||||
import { Delete, Get, Patch, Post, RestController } from '@/decorators';
|
||||
import { Body, Delete, Get, Patch, Post, RestController } from '@/decorators';
|
||||
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
||||
import { InvalidMfaCodeError } from '@/errors/response-errors/invalid-mfa-code.error';
|
||||
import { EventService } from '@/events/event.service';
|
||||
@@ -16,12 +20,7 @@ import type { PublicUser } from '@/interfaces';
|
||||
import { Logger } from '@/logger';
|
||||
import { MfaService } from '@/mfa/mfa.service';
|
||||
import { isApiEnabled } from '@/public-api';
|
||||
import {
|
||||
AuthenticatedRequest,
|
||||
MeRequest,
|
||||
UserSettingsUpdatePayload,
|
||||
UserUpdatePayload,
|
||||
} from '@/requests';
|
||||
import { AuthenticatedRequest, MeRequest } from '@/requests';
|
||||
import { PasswordUtility } from '@/services/password.utility';
|
||||
import { UserService } from '@/services/user.service';
|
||||
import { isSamlLicensedAndEnabled } from '@/sso/saml/saml-helpers';
|
||||
@@ -55,30 +54,14 @@ export class MeController {
|
||||
* Update the logged-in user's properties, except password.
|
||||
*/
|
||||
@Patch('/')
|
||||
async updateCurrentUser(req: MeRequest.UserUpdate, res: Response): Promise<PublicUser> {
|
||||
async updateCurrentUser(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response,
|
||||
@Body payload: UserUpdateRequestDto,
|
||||
): Promise<PublicUser> {
|
||||
const { id: userId, email: currentEmail, mfaEnabled } = req.user;
|
||||
|
||||
const payload = plainToInstance(UserUpdatePayload, req.body, { excludeExtraneousValues: true });
|
||||
|
||||
const { email } = payload;
|
||||
if (!email) {
|
||||
this.logger.debug('Request to update user email failed because of missing email in payload', {
|
||||
userId,
|
||||
payload,
|
||||
});
|
||||
throw new BadRequestError('Email is mandatory');
|
||||
}
|
||||
|
||||
if (!validator.isEmail(email)) {
|
||||
this.logger.debug('Request to update user email failed because of invalid email in payload', {
|
||||
userId,
|
||||
invalidEmail: email,
|
||||
});
|
||||
throw new BadRequestError('Invalid email address');
|
||||
}
|
||||
|
||||
await validateEntity(payload);
|
||||
|
||||
const isEmailBeingChanged = email !== currentEmail;
|
||||
|
||||
// If SAML is enabled, we don't allow the user to change their email address
|
||||
@@ -134,9 +117,13 @@ export class MeController {
|
||||
* Update the logged-in user's password.
|
||||
*/
|
||||
@Patch('/password', { rateLimit: true })
|
||||
async updatePassword(req: MeRequest.Password, res: Response) {
|
||||
async updatePassword(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response,
|
||||
@Body payload: PasswordUpdateRequestDto,
|
||||
) {
|
||||
const { user } = req;
|
||||
const { currentPassword, newPassword, mfaCode } = req.body;
|
||||
const { currentPassword, newPassword, mfaCode } = payload;
|
||||
|
||||
// If SAML is enabled, we don't allow the user to change their password
|
||||
if (isSamlLicensedAndEnabled()) {
|
||||
@@ -270,13 +257,11 @@ export class MeController {
|
||||
* Update the logged-in user's settings.
|
||||
*/
|
||||
@Patch('/settings')
|
||||
async updateCurrentUserSettings(req: MeRequest.UserSettingsUpdate): Promise<User['settings']> {
|
||||
const payload = plainToInstance(UserSettingsUpdatePayload, req.body, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
|
||||
await validateEntity(payload);
|
||||
|
||||
async updateCurrentUserSettings(
|
||||
req: AuthenticatedRequest,
|
||||
_: Response,
|
||||
@Body payload: SettingsUpdateRequestDto,
|
||||
): Promise<User['settings']> {
|
||||
const { id } = req.user;
|
||||
|
||||
await this.userService.updateSettings(id, payload);
|
||||
|
||||
Reference in New Issue
Block a user