mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
feat(core): Add filtering, selection and pagination to users (#6994)
https://linear.app/n8n/issue/PAY-646
This commit is contained in:
@@ -4,6 +4,9 @@ import { In } from 'typeorm';
|
||||
import { User } from '@db/entities/User';
|
||||
import type { IUserSettings } from 'n8n-workflow';
|
||||
import { UserRepository } from '@/databases/repositories';
|
||||
import { getInstanceBaseUrl } from '@/UserManagement/UserManagementHelper';
|
||||
import type { PublicUser } from '@/Interfaces';
|
||||
import type { PostHogClient } from '@/posthog';
|
||||
|
||||
@Service()
|
||||
export class UserService {
|
||||
@@ -18,7 +21,7 @@ export class UserService {
|
||||
}
|
||||
|
||||
async findMany(options: FindManyOptions<User>) {
|
||||
return this.userRepository.find({ relations: ['globalRole'], ...options });
|
||||
return this.userRepository.find(options);
|
||||
}
|
||||
|
||||
async findOneBy(options: FindOptionsWhere<User>) {
|
||||
@@ -59,4 +62,54 @@ export class UserService {
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
async toPublic(user: User, options?: { withInviteUrl?: boolean; posthog?: PostHogClient }) {
|
||||
const { password, updatedAt, apiKey, authIdentities, ...rest } = user;
|
||||
|
||||
const ldapIdentity = authIdentities?.find((i) => i.providerType === 'ldap');
|
||||
|
||||
let publicUser: PublicUser = {
|
||||
...rest,
|
||||
signInType: ldapIdentity ? 'ldap' : 'email',
|
||||
hasRecoveryCodesLeft: !!user.mfaRecoveryCodes?.length,
|
||||
};
|
||||
|
||||
if (options?.withInviteUrl && publicUser.isPending) {
|
||||
publicUser = this.addInviteUrl(publicUser, user.id);
|
||||
}
|
||||
|
||||
if (options?.posthog) {
|
||||
publicUser = await this.addFeatureFlags(publicUser, options.posthog);
|
||||
}
|
||||
|
||||
return publicUser;
|
||||
}
|
||||
|
||||
private addInviteUrl(user: PublicUser, inviterId: string) {
|
||||
const url = new URL(getInstanceBaseUrl());
|
||||
url.pathname = '/signup';
|
||||
url.searchParams.set('inviterId', inviterId);
|
||||
url.searchParams.set('inviteeId', user.id);
|
||||
|
||||
user.inviteAcceptUrl = url.toString();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private async addFeatureFlags(publicUser: PublicUser, posthog: PostHogClient) {
|
||||
// native PostHog implementation has default 10s timeout and 3 retries.. which cannot be updated without affecting other functionality
|
||||
// https://github.com/PostHog/posthog-js-lite/blob/a182de80a433fb0ffa6859c10fb28084d0f825c2/posthog-core/src/index.ts#L67
|
||||
const timeoutPromise = new Promise<PublicUser>((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(publicUser);
|
||||
}, 1500);
|
||||
});
|
||||
|
||||
const fetchPromise = new Promise<PublicUser>(async (resolve) => {
|
||||
publicUser.featureFlags = await posthog.getFeatureFlags(publicUser);
|
||||
resolve(publicUser);
|
||||
});
|
||||
|
||||
return Promise.race([fetchPromise, timeoutPromise]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user