2024-01-24 04:38:57 -08:00
|
|
|
import { Container } from 'typedi';
|
2023-12-22 07:20:30 -08:00
|
|
|
import type { EntityManager, FindOptionsWhere } from 'typeorm';
|
2022-11-09 06:25:00 -08:00
|
|
|
import { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
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';
|
2023-11-29 06:48:36 -08:00
|
|
|
import { CredentialsService, 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';
|
2022-09-21 01:20:29 -07:00
|
|
|
|
|
|
|
export class EECredentialsService extends CredentialsService {
|
|
|
|
static async isOwned(
|
|
|
|
user: User,
|
|
|
|
credentialId: string,
|
|
|
|
): Promise<{ ownsCredential: boolean; credential?: CredentialsEntity }> {
|
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.
|
|
|
|
*/
|
|
|
|
static async getSharing(
|
|
|
|
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'],
|
2023-01-13 09:12:22 -08:00
|
|
|
): Promise<SharedCredentials | null> {
|
|
|
|
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-17 07:08:50 -08:00
|
|
|
return await Container.get(SharedCredentialsRepository).findOne({
|
2023-01-02 08:42:32 -08:00
|
|
|
where,
|
|
|
|
relations,
|
|
|
|
});
|
2022-11-21 23:37:52 -08:00
|
|
|
}
|
|
|
|
|
2022-09-21 01:20:29 -07:00
|
|
|
static async getSharings(
|
|
|
|
transaction: EntityManager,
|
|
|
|
credentialId: string,
|
2023-11-29 08:32:27 -08:00
|
|
|
relations = ['shared'],
|
2022-09-21 01:20:29 -07:00
|
|
|
): Promise<SharedCredentials[]> {
|
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
|
|
|
});
|
|
|
|
return credential?.shared ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
static async share(
|
|
|
|
transaction: EntityManager,
|
|
|
|
credential: CredentialsEntity,
|
|
|
|
shareWithIds: string[],
|
|
|
|
): Promise<SharedCredentials[]> {
|
2024-01-02 08:53:24 -08:00
|
|
|
const users = await Container.get(UserRepository).getByIds(transaction, shareWithIds);
|
2022-09-21 01:20:29 -07:00
|
|
|
|
|
|
|
const newSharedCredentials = users
|
|
|
|
.filter((user) => !user.isPending)
|
|
|
|
.map((user) =>
|
2023-11-10 06:04:26 -08:00
|
|
|
Container.get(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
|
|
|
}
|
|
|
|
}
|