mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
refactor(core): Continue moving typeorm operators to repositories (no-changelog) (#8186)
Follow-up to: #8163
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import type { FindManyOptions } from 'typeorm';
|
||||
import { In, Not } from 'typeorm';
|
||||
import { In } from 'typeorm';
|
||||
import { User } from '@db/entities/User';
|
||||
import { SharedCredentials } from '@db/entities/SharedCredentials';
|
||||
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
||||
@@ -21,6 +20,7 @@ import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
||||
import { License } from '@/License';
|
||||
import { ExternalHooks } from '@/ExternalHooks';
|
||||
import { InternalHooks } from '@/InternalHooks';
|
||||
import { UserRepository } from '@/databases/repositories/user.repository';
|
||||
|
||||
@Authorized()
|
||||
@RestController('/users')
|
||||
@@ -35,6 +35,7 @@ export class UsersController {
|
||||
private readonly roleService: RoleService,
|
||||
private readonly userService: UserService,
|
||||
private readonly license: License,
|
||||
private readonly userRepository: UserRepository,
|
||||
) {}
|
||||
|
||||
static ERROR_MESSAGES = {
|
||||
@@ -49,44 +50,6 @@ export class UsersController {
|
||||
},
|
||||
} as const;
|
||||
|
||||
private async toFindManyOptions(listQueryOptions?: ListQuery.Options) {
|
||||
const findManyOptions: FindManyOptions<User> = {};
|
||||
|
||||
if (!listQueryOptions) {
|
||||
findManyOptions.relations = ['globalRole', 'authIdentities'];
|
||||
return findManyOptions;
|
||||
}
|
||||
|
||||
const { filter, select, take, skip } = listQueryOptions;
|
||||
|
||||
if (select) findManyOptions.select = select;
|
||||
if (take) findManyOptions.take = take;
|
||||
if (skip) findManyOptions.skip = skip;
|
||||
|
||||
if (take && !select) {
|
||||
findManyOptions.relations = ['globalRole', 'authIdentities'];
|
||||
}
|
||||
|
||||
if (take && select && !select?.id) {
|
||||
findManyOptions.select = { ...findManyOptions.select, id: true }; // pagination requires id
|
||||
}
|
||||
|
||||
if (filter) {
|
||||
const { isOwner, ...otherFilters } = filter;
|
||||
|
||||
findManyOptions.where = otherFilters;
|
||||
|
||||
if (isOwner !== undefined) {
|
||||
const ownerRole = await this.roleService.findGlobalOwnerRole();
|
||||
|
||||
findManyOptions.relations = ['globalRole'];
|
||||
findManyOptions.where.globalRole = { id: isOwner ? ownerRole.id : Not(ownerRole.id) };
|
||||
}
|
||||
}
|
||||
|
||||
return findManyOptions;
|
||||
}
|
||||
|
||||
private removeSupplementaryFields(
|
||||
publicUsers: Array<Partial<PublicUser>>,
|
||||
listQueryOptions: ListQuery.Options,
|
||||
@@ -122,9 +85,14 @@ export class UsersController {
|
||||
async listUsers(req: ListQuery.Request) {
|
||||
const { listQueryOptions } = req;
|
||||
|
||||
const findManyOptions = await this.toFindManyOptions(listQueryOptions);
|
||||
const globalOwner = await this.roleService.findGlobalOwnerRole();
|
||||
|
||||
const users = await this.userService.findMany(findManyOptions);
|
||||
const findManyOptions = await this.userRepository.toFindManyOptions(
|
||||
listQueryOptions,
|
||||
globalOwner.id,
|
||||
);
|
||||
|
||||
const users = await this.userRepository.find(findManyOptions);
|
||||
|
||||
const publicUsers: Array<Partial<PublicUser>> = await Promise.all(
|
||||
users.map(async (u) =>
|
||||
@@ -140,8 +108,9 @@ export class UsersController {
|
||||
@Get('/:id/password-reset-link')
|
||||
@RequireGlobalScope('user:resetPassword')
|
||||
async getUserPasswordResetLink(req: UserRequest.PasswordResetLink) {
|
||||
const user = await this.userService.findOneOrFail({
|
||||
const user = await this.userRepository.findOneOrFail({
|
||||
where: { id: req.params.id },
|
||||
relations: ['globalRole'],
|
||||
});
|
||||
if (!user) {
|
||||
throw new NotFoundError('User not found');
|
||||
@@ -160,9 +129,10 @@ export class UsersController {
|
||||
|
||||
await this.userService.updateSettings(id, payload);
|
||||
|
||||
const user = await this.userService.findOneOrFail({
|
||||
const user = await this.userRepository.findOneOrFail({
|
||||
select: ['settings'],
|
||||
where: { id },
|
||||
relations: ['globalRole'],
|
||||
});
|
||||
|
||||
return user.settings;
|
||||
@@ -192,10 +162,9 @@ export class UsersController {
|
||||
);
|
||||
}
|
||||
|
||||
const users = await this.userService.findMany({
|
||||
where: { id: In([transferId, idToDelete]) },
|
||||
relations: ['globalRole'],
|
||||
});
|
||||
const userIds = transferId ? [transferId, idToDelete] : [idToDelete];
|
||||
|
||||
const users = await this.userRepository.findManybyIds(userIds);
|
||||
|
||||
if (!users.length || (transferId && users.length !== 2)) {
|
||||
throw new NotFoundError(
|
||||
@@ -354,8 +323,9 @@ export class UsersController {
|
||||
throw new UnauthorizedError(NO_USER_TO_OWNER);
|
||||
}
|
||||
|
||||
const targetUser = await this.userService.findOne({
|
||||
const targetUser = await this.userRepository.findOne({
|
||||
where: { id: req.params.id },
|
||||
relations: ['globalRole'],
|
||||
});
|
||||
|
||||
if (targetUser === null) {
|
||||
|
||||
Reference in New Issue
Block a user