mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-20 03:12:15 +00:00
refactor(core): Move all user DB access to UserRepository (#6910)
Prep for https://linear.app/n8n/issue/PAY-646
This commit is contained in:
@@ -18,18 +18,18 @@ import type { UserManagementMailer } from '@/UserManagement/email';
|
||||
import { Response } from 'express';
|
||||
import type { ILogger } from 'n8n-workflow';
|
||||
import type { Config } from '@/config';
|
||||
import type { UserRepository } from '@db/repositories';
|
||||
import { PasswordResetRequest } from '@/requests';
|
||||
import type { IDatabaseCollections, IExternalHooksClass, IInternalHooksClass } from '@/Interfaces';
|
||||
import type { IExternalHooksClass, IInternalHooksClass } from '@/Interfaces';
|
||||
import { issueCookie } from '@/auth/jwt';
|
||||
import { isLdapEnabled } from '@/Ldap/helpers';
|
||||
import { isSamlCurrentAuthenticationMethod } from '@/sso/ssoHelpers';
|
||||
import { UserService } from '@/user/user.service';
|
||||
import { UserService } from '@/services/user.service';
|
||||
import { License } from '@/License';
|
||||
import { Container } from 'typedi';
|
||||
import { RESPONSE_ERROR_MESSAGES } from '@/constants';
|
||||
import { TokenExpiredError } from 'jsonwebtoken';
|
||||
import type { JwtService, JwtPayload } from '@/services/jwt.service';
|
||||
import type { JwtPayload } from '@/services/jwt.service';
|
||||
import { JwtService } from '@/services/jwt.service';
|
||||
|
||||
@RestController()
|
||||
export class PasswordResetController {
|
||||
@@ -43,34 +43,30 @@ export class PasswordResetController {
|
||||
|
||||
private readonly mailer: UserManagementMailer;
|
||||
|
||||
private readonly userRepository: UserRepository;
|
||||
|
||||
private readonly jwtService: JwtService;
|
||||
|
||||
private readonly userService: UserService;
|
||||
|
||||
constructor({
|
||||
config,
|
||||
logger,
|
||||
externalHooks,
|
||||
internalHooks,
|
||||
mailer,
|
||||
repositories,
|
||||
jwtService,
|
||||
}: {
|
||||
config: Config;
|
||||
logger: ILogger;
|
||||
externalHooks: IExternalHooksClass;
|
||||
internalHooks: IInternalHooksClass;
|
||||
mailer: UserManagementMailer;
|
||||
repositories: Pick<IDatabaseCollections, 'User'>;
|
||||
jwtService: JwtService;
|
||||
}) {
|
||||
this.config = config;
|
||||
this.logger = logger;
|
||||
this.externalHooks = externalHooks;
|
||||
this.internalHooks = internalHooks;
|
||||
this.mailer = mailer;
|
||||
this.userRepository = repositories.User;
|
||||
this.jwtService = jwtService;
|
||||
this.jwtService = Container.get(JwtService);
|
||||
this.userService = Container.get(UserService);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,7 +101,7 @@ export class PasswordResetController {
|
||||
}
|
||||
|
||||
// User should just be able to reset password if one is already present
|
||||
const user = await this.userRepository.findOne({
|
||||
const user = await this.userService.findOne({
|
||||
where: {
|
||||
email,
|
||||
password: Not(IsNull()),
|
||||
@@ -154,7 +150,7 @@ export class PasswordResetController {
|
||||
},
|
||||
);
|
||||
|
||||
const url = await UserService.generatePasswordResetUrl(baseUrl, resetPasswordToken);
|
||||
const url = this.userService.generatePasswordResetUrl(baseUrl, resetPasswordToken);
|
||||
|
||||
try {
|
||||
await this.mailer.passwordReset({
|
||||
@@ -204,10 +200,8 @@ export class PasswordResetController {
|
||||
|
||||
const decodedToken = this.verifyResetPasswordToken(resetPasswordToken);
|
||||
|
||||
const user = await this.userRepository.findOne({
|
||||
where: {
|
||||
id: decodedToken.sub,
|
||||
},
|
||||
const user = await this.userService.findOne({
|
||||
where: { id: decodedToken.sub },
|
||||
relations: ['globalRole'],
|
||||
});
|
||||
|
||||
@@ -255,7 +249,7 @@ export class PasswordResetController {
|
||||
|
||||
const decodedToken = this.verifyResetPasswordToken(resetPasswordToken);
|
||||
|
||||
const user = await this.userRepository.findOne({
|
||||
const user = await this.userService.findOne({
|
||||
where: { id: decodedToken.sub },
|
||||
relations: ['authIdentities'],
|
||||
});
|
||||
@@ -272,9 +266,7 @@ export class PasswordResetController {
|
||||
|
||||
const passwordHash = await hashPassword(validPassword);
|
||||
|
||||
await this.userRepository.update(user.id, {
|
||||
password: passwordHash,
|
||||
});
|
||||
await this.userService.update(user.id, { password: passwordHash });
|
||||
|
||||
this.logger.info('User password updated successfully', { userId: user.id });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user