1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-25 10:43:13 +02:00

refactor(server): make access core singleton (#4609)

This commit is contained in:
Daniel Dietzler 2023-10-23 14:37:51 +02:00 committed by GitHub
parent 36b3521be8
commit 50bc92aac0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 28 additions and 10 deletions

View File

@ -37,8 +37,22 @@ export enum Permission {
PERSON_MERGE = 'person.merge',
}
let instance: AccessCore | null;
export class AccessCore {
constructor(private repository: IAccessRepository) {}
private constructor(private repository: IAccessRepository) {}
static create(repository: IAccessRepository) {
if (!instance) {
instance = new AccessCore(repository);
}
return instance;
}
static reset() {
instance = null;
}
requireUploadAccess(authUser: AuthUserDto | null): AuthUserDto {
if (!authUser || (authUser.isPublicUser && !authUser.isAllowUpload)) {

View File

@ -31,7 +31,7 @@ export class AlbumService {
@Inject(IJobRepository) private jobRepository: IJobRepository,
@Inject(IUserRepository) private userRepository: IUserRepository,
) {
this.access = new AccessCore(accessRepository);
this.access = AccessCore.create(accessRepository);
}
async getCount(authUser: AuthUserDto): Promise<AlbumCountResponseDto> {

View File

@ -89,7 +89,7 @@ export class AssetService {
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
@Inject(ICommunicationRepository) private communicationRepository: ICommunicationRepository,
) {
this.access = new AccessCore(accessRepository);
this.access = AccessCore.create(accessRepository);
this.configCore = SystemConfigCore.create(configRepository);
this.storageCore = new StorageCore(storageRepository, assetRepository, moveRepository, personRepository);
}

View File

@ -40,7 +40,7 @@ export class AuditService {
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
@Inject(IUserRepository) private userRepository: IUserRepository,
) {
this.access = new AccessCore(accessRepository);
this.access = AccessCore.create(accessRepository);
}
async handleCleanup(): Promise<boolean> {

View File

@ -43,7 +43,7 @@ export class LibraryService {
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
@Inject(IUserRepository) private userRepository: IUserRepository,
) {
this.access = new AccessCore(accessRepository);
this.access = AccessCore.create(accessRepository);
}
async getStatistics(authUser: AuthUserDto, id: string): Promise<LibraryStatsResponseDto> {

View File

@ -56,7 +56,7 @@ export class PersonService {
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
@Inject(IJobRepository) private jobRepository: IJobRepository,
) {
this.access = new AccessCore(accessRepository);
this.access = AccessCore.create(accessRepository);
this.configCore = SystemConfigCore.create(configRepository);
this.storageCore = new StorageCore(storageRepository, assetRepository, moveRepository, repository);
}

View File

@ -16,7 +16,7 @@ export class SharedLinkService {
@Inject(ICryptoRepository) private cryptoRepository: ICryptoRepository,
@Inject(ISharedLinkRepository) private repository: ISharedLinkRepository,
) {
this.access = new AccessCore(accessRepository);
this.access = AccessCore.create(accessRepository);
}
getAll(authUser: AuthUserDto): Promise<SharedLinkResponseDto[]> {

View File

@ -68,7 +68,7 @@ export class AssetService {
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
) {
this.assetCore = new AssetCore(_assetRepository, jobRepository);
this.access = new AccessCore(accessRepository);
this.access = AccessCore.create(accessRepository);
}
public async uploadFile(

View File

@ -1,4 +1,4 @@
import { IAccessRepository } from '@app/domain';
import { AccessCore, IAccessRepository } from '@app/domain';
export interface IAccessRepositoryMock {
asset: jest.Mocked<IAccessRepository['asset']>;
@ -8,7 +8,11 @@ export interface IAccessRepositoryMock {
person: jest.Mocked<IAccessRepository['person']>;
}
export const newAccessRepositoryMock = (): IAccessRepositoryMock => {
export const newAccessRepositoryMock = (reset = true): IAccessRepositoryMock => {
if (reset) {
AccessCore.reset();
}
return {
asset: {
hasOwnerAccess: jest.fn(),