mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +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:
@@ -38,18 +38,14 @@ import type { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
||||
import { AuthIdentity } from '@db/entities/AuthIdentity';
|
||||
import type { PostHogClient } from '@/posthog';
|
||||
import { isSamlLicensedAndEnabled } from '../sso/saml/samlHelpers';
|
||||
import type {
|
||||
SharedCredentialsRepository,
|
||||
SharedWorkflowRepository,
|
||||
UserRepository,
|
||||
} from '@db/repositories';
|
||||
import { UserService } from '@/user/user.service';
|
||||
import type { SharedCredentialsRepository, SharedWorkflowRepository } from '@db/repositories';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { License } from '@/License';
|
||||
import { Container } from 'typedi';
|
||||
import { RESPONSE_ERROR_MESSAGES } from '@/constants';
|
||||
import type { JwtService } from '@/services/jwt.service';
|
||||
import type { RoleService } from '@/services/role.service';
|
||||
import { JwtService } from '@/services/jwt.service';
|
||||
import { RoleService } from '@/services/role.service';
|
||||
import { UserService } from '@/services/user.service';
|
||||
|
||||
@Authorized(['global', 'owner'])
|
||||
@RestController('/users')
|
||||
@@ -62,8 +58,6 @@ export class UsersController {
|
||||
|
||||
private internalHooks: IInternalHooksClass;
|
||||
|
||||
private userRepository: UserRepository;
|
||||
|
||||
private sharedCredentialsRepository: SharedCredentialsRepository;
|
||||
|
||||
private sharedWorkflowRepository: SharedWorkflowRepository;
|
||||
@@ -78,6 +72,8 @@ export class UsersController {
|
||||
|
||||
private roleService: RoleService;
|
||||
|
||||
private userService: UserService;
|
||||
|
||||
constructor({
|
||||
config,
|
||||
logger,
|
||||
@@ -86,33 +82,29 @@ export class UsersController {
|
||||
repositories,
|
||||
activeWorkflowRunner,
|
||||
mailer,
|
||||
jwtService,
|
||||
postHog,
|
||||
roleService,
|
||||
}: {
|
||||
config: Config;
|
||||
logger: ILogger;
|
||||
externalHooks: IExternalHooksClass;
|
||||
internalHooks: IInternalHooksClass;
|
||||
repositories: Pick<IDatabaseCollections, 'User' | 'SharedCredentials' | 'SharedWorkflow'>;
|
||||
repositories: Pick<IDatabaseCollections, 'SharedCredentials' | 'SharedWorkflow'>;
|
||||
activeWorkflowRunner: ActiveWorkflowRunner;
|
||||
mailer: UserManagementMailer;
|
||||
jwtService: JwtService;
|
||||
postHog?: PostHogClient;
|
||||
roleService: RoleService;
|
||||
}) {
|
||||
this.config = config;
|
||||
this.logger = logger;
|
||||
this.externalHooks = externalHooks;
|
||||
this.internalHooks = internalHooks;
|
||||
this.userRepository = repositories.User;
|
||||
this.sharedCredentialsRepository = repositories.SharedCredentials;
|
||||
this.sharedWorkflowRepository = repositories.SharedWorkflow;
|
||||
this.activeWorkflowRunner = activeWorkflowRunner;
|
||||
this.mailer = mailer;
|
||||
this.jwtService = jwtService;
|
||||
this.jwtService = Container.get(JwtService);
|
||||
this.postHog = postHog;
|
||||
this.roleService = roleService;
|
||||
this.roleService = Container.get(RoleService);
|
||||
this.userService = Container.get(UserService);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,7 +177,7 @@ export class UsersController {
|
||||
}
|
||||
|
||||
// remove/exclude existing users from creation
|
||||
const existingUsers = await this.userRepository.find({
|
||||
const existingUsers = await this.userService.findMany({
|
||||
where: { email: In(Object.keys(createUsers)) },
|
||||
});
|
||||
existingUsers.forEach((user) => {
|
||||
@@ -202,7 +194,7 @@ export class UsersController {
|
||||
this.logger.debug(total > 1 ? `Creating ${total} user shells...` : 'Creating 1 user shell...');
|
||||
|
||||
try {
|
||||
await this.userRepository.manager.transaction(async (transactionManager) =>
|
||||
await this.userService.getManager().transaction(async (transactionManager) =>
|
||||
Promise.all(
|
||||
usersToSetUp.map(async (email) => {
|
||||
const newUser = Object.assign(new User(), {
|
||||
@@ -323,7 +315,7 @@ export class UsersController {
|
||||
|
||||
const validPassword = validatePassword(password);
|
||||
|
||||
const users = await this.userRepository.find({
|
||||
const users = await this.userService.findMany({
|
||||
where: { id: In([inviterId, inviteeId]) },
|
||||
relations: ['globalRole'],
|
||||
});
|
||||
@@ -353,7 +345,7 @@ export class UsersController {
|
||||
invitee.lastName = lastName;
|
||||
invitee.password = await hashPassword(validPassword);
|
||||
|
||||
const updatedUser = await this.userRepository.save(invitee);
|
||||
const updatedUser = await this.userService.save(invitee);
|
||||
|
||||
await issueCookie(res, updatedUser);
|
||||
|
||||
@@ -371,7 +363,7 @@ export class UsersController {
|
||||
@Authorized('any')
|
||||
@Get('/')
|
||||
async listUsers(req: UserRequest.List) {
|
||||
const users = await this.userRepository.find({ relations: ['globalRole', 'authIdentities'] });
|
||||
const users = await this.userService.findMany({ relations: ['globalRole', 'authIdentities'] });
|
||||
return users.map(
|
||||
(user): PublicUser =>
|
||||
addInviteLinkToUser(sanitizeUser(user, ['personalizationAnswers']), req.user.id),
|
||||
@@ -381,7 +373,7 @@ export class UsersController {
|
||||
@Authorized(['global', 'owner'])
|
||||
@Get('/:id/password-reset-link')
|
||||
async getUserPasswordResetLink(req: UserRequest.PasswordResetLink) {
|
||||
const user = await this.userRepository.findOneOrFail({
|
||||
const user = await this.userService.findOneOrFail({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
if (!user) {
|
||||
@@ -397,7 +389,7 @@ export class UsersController {
|
||||
|
||||
const baseUrl = getInstanceBaseUrl();
|
||||
|
||||
const link = await UserService.generatePasswordResetUrl(baseUrl, resetPasswordToken);
|
||||
const link = this.userService.generatePasswordResetUrl(baseUrl, resetPasswordToken);
|
||||
return {
|
||||
link,
|
||||
};
|
||||
@@ -410,9 +402,9 @@ export class UsersController {
|
||||
|
||||
const id = req.params.id;
|
||||
|
||||
await UserService.updateUserSettings(id, payload);
|
||||
await this.userService.updateSettings(id, payload);
|
||||
|
||||
const user = await this.userRepository.findOneOrFail({
|
||||
const user = await this.userService.findOneOrFail({
|
||||
select: ['settings'],
|
||||
where: { id },
|
||||
});
|
||||
@@ -443,7 +435,7 @@ export class UsersController {
|
||||
);
|
||||
}
|
||||
|
||||
const users = await this.userRepository.find({
|
||||
const users = await this.userService.findMany({
|
||||
where: { id: In([transferId, idToDelete]) },
|
||||
});
|
||||
|
||||
@@ -475,7 +467,7 @@ export class UsersController {
|
||||
if (transferId) {
|
||||
const transferee = users.find((user) => user.id === transferId);
|
||||
|
||||
await this.userRepository.manager.transaction(async (transactionManager) => {
|
||||
await this.userService.getManager().transaction(async (transactionManager) => {
|
||||
// Get all workflow ids belonging to user to delete
|
||||
const sharedWorkflowIds = await transactionManager
|
||||
.getRepository(SharedWorkflow)
|
||||
@@ -550,7 +542,7 @@ export class UsersController {
|
||||
}),
|
||||
]);
|
||||
|
||||
await this.userRepository.manager.transaction(async (transactionManager) => {
|
||||
await this.userService.getManager().transaction(async (transactionManager) => {
|
||||
const ownedWorkflows = await Promise.all(
|
||||
ownedSharedWorkflows.map(async ({ workflow }) => {
|
||||
if (workflow.active) {
|
||||
@@ -597,7 +589,7 @@ export class UsersController {
|
||||
throw new InternalServerError('Email sending must be set up in order to invite other users');
|
||||
}
|
||||
|
||||
const reinvitee = await this.userRepository.findOneBy({ id: idToReinvite });
|
||||
const reinvitee = await this.userService.findOneBy({ id: idToReinvite });
|
||||
if (!reinvitee) {
|
||||
this.logger.debug(
|
||||
'Request to reinvite a user failed because the ID of the reinvitee was not found in database',
|
||||
|
||||
Reference in New Issue
Block a user