2024-02-08 06:13:29 -08:00
|
|
|
import type { EntityManager, FindOptionsWhere } from '@n8n/typeorm';
|
2023-12-22 07:20:30 -08:00
|
|
|
import type { SharedCredentials } from '@db/entities/SharedCredentials';
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { User } from '@db/entities/User';
|
2024-01-31 00:48:48 -08:00
|
|
|
import { type CredentialsGetSharedOptions } from './credentials.service';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
|
2024-01-02 08:53:24 -08:00
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
2024-01-31 00:48:48 -08:00
|
|
|
import { CredentialsEntity } from '@/databases/entities/CredentialsEntity';
|
|
|
|
import { Service } from 'typedi';
|
2022-09-21 01:20:29 -07:00
|
|
|
|
2024-01-31 00:48:48 -08:00
|
|
|
@Service()
|
|
|
|
export class EnterpriseCredentialsService {
|
|
|
|
constructor(
|
|
|
|
private readonly userRepository: UserRepository,
|
|
|
|
private readonly sharedCredentialsRepository: SharedCredentialsRepository,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async isOwned(user: User, credentialId: string) {
|
2023-11-29 06:48:36 -08:00
|
|
|
const sharing = await this.getSharing(user, credentialId, { allowGlobalScope: false }, [
|
|
|
|
'credentials',
|
|
|
|
]);
|
2022-09-21 01:20:29 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
if (!sharing || sharing.role !== 'credential:owner') return { ownsCredential: false };
|
2022-09-21 01:20:29 -07:00
|
|
|
|
|
|
|
const { credentials: credential } = sharing;
|
|
|
|
|
|
|
|
return { ownsCredential: true, credential };
|
|
|
|
}
|
|
|
|
|
2022-11-21 23:37:52 -08:00
|
|
|
/**
|
|
|
|
* Retrieve the sharing that matches a user and a credential.
|
|
|
|
*/
|
2024-01-31 00:48:48 -08:00
|
|
|
async getSharing(
|
2022-11-21 23:37:52 -08:00
|
|
|
user: User,
|
2023-01-02 08:42:32 -08:00
|
|
|
credentialId: string,
|
2023-11-29 06:48:36 -08:00
|
|
|
options: CredentialsGetSharedOptions,
|
2022-11-21 23:37:52 -08:00
|
|
|
relations: string[] = ['credentials'],
|
2024-01-31 00:48:48 -08:00
|
|
|
) {
|
2023-01-13 09:12:22 -08:00
|
|
|
const where: FindOptionsWhere<SharedCredentials> = { credentialsId: credentialId };
|
2022-11-21 23:37:52 -08:00
|
|
|
|
2023-11-29 06:48:36 -08:00
|
|
|
// Omit user from where if the requesting user has relevant
|
|
|
|
// global credential permissions. This allows the user to
|
|
|
|
// access credentials they don't own.
|
2023-12-19 04:52:42 -08:00
|
|
|
if (!options.allowGlobalScope || !user.hasGlobalScope(options.globalScope)) {
|
2023-01-13 09:12:22 -08:00
|
|
|
where.userId = user.id;
|
2022-11-21 23:37:52 -08:00
|
|
|
}
|
|
|
|
|
2024-01-31 00:48:48 -08:00
|
|
|
return await this.sharedCredentialsRepository.findOne({
|
2023-01-02 08:42:32 -08:00
|
|
|
where,
|
|
|
|
relations,
|
|
|
|
});
|
2022-11-21 23:37:52 -08:00
|
|
|
}
|
|
|
|
|
2024-01-31 00:48:48 -08:00
|
|
|
async getSharings(transaction: EntityManager, credentialId: string, relations = ['shared']) {
|
2023-01-02 08:42:32 -08:00
|
|
|
const credential = await transaction.findOne(CredentialsEntity, {
|
|
|
|
where: { id: credentialId },
|
2023-11-29 08:32:27 -08:00
|
|
|
relations,
|
2022-09-21 01:20:29 -07:00
|
|
|
});
|
2024-01-31 00:48:48 -08:00
|
|
|
|
2022-09-21 01:20:29 -07:00
|
|
|
return credential?.shared ?? [];
|
|
|
|
}
|
|
|
|
|
2024-01-31 00:48:48 -08:00
|
|
|
async share(transaction: EntityManager, credential: CredentialsEntity, shareWithIds: string[]) {
|
|
|
|
const users = await this.userRepository.getByIds(transaction, shareWithIds);
|
2022-09-21 01:20:29 -07:00
|
|
|
|
|
|
|
const newSharedCredentials = users
|
|
|
|
.filter((user) => !user.isPending)
|
|
|
|
.map((user) =>
|
2024-01-31 00:48:48 -08:00
|
|
|
this.sharedCredentialsRepository.create({
|
2023-01-13 09:12:22 -08:00
|
|
|
credentialsId: credential.id,
|
|
|
|
userId: user.id,
|
2024-01-24 04:38:57 -08:00
|
|
|
role: 'credential:user',
|
2022-09-21 01:20:29 -07:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await transaction.save(newSharedCredentials);
|
2022-09-21 01:20:29 -07:00
|
|
|
}
|
|
|
|
}
|