Files
immich/server/src/services/cli.service.ts

56 lines
1.9 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@nestjs/common';
import { SALT_ROUNDS } from 'src/constants';
import { UserAdminResponseDto, mapUserAdmin } from 'src/dtos/user.dto';
import { BaseService } from 'src/services/base.service';
2024-05-22 16:23:47 -04:00
@Injectable()
export class CliService extends BaseService {
async listUsers(): Promise<UserAdminResponseDto[]> {
2024-05-22 16:23:47 -04:00
const users = await this.userRepository.getList({ withDeleted: true });
return users.map((user) => mapUserAdmin(user));
2024-05-22 16:23:47 -04:00
}
async resetAdminPassword(ask: (admin: UserAdminResponseDto) => Promise<string | undefined>) {
2024-05-22 16:23:47 -04:00
const admin = await this.userRepository.getAdmin();
if (!admin) {
throw new Error('Admin account does not exist');
}
const providedPassword = await ask(mapUserAdmin(admin));
const password = providedPassword || this.cryptoRepository.randomBytesAsText(24);
const hashedPassword = await this.cryptoRepository.hashBcrypt(password, SALT_ROUNDS);
2024-05-22 16:23:47 -04:00
await this.userRepository.update(admin.id, { password: hashedPassword });
2024-05-22 16:23:47 -04:00
return { admin, password, provided: !!providedPassword };
}
async disablePasswordLogin(): Promise<void> {
const config = await this.getConfig({ withCache: false });
2024-05-22 16:23:47 -04:00
config.passwordLogin.enabled = false;
await this.updateConfig(config);
2024-05-22 16:23:47 -04:00
}
async enablePasswordLogin(): Promise<void> {
const config = await this.getConfig({ withCache: false });
2024-05-22 16:23:47 -04:00
config.passwordLogin.enabled = true;
await this.updateConfig(config);
2024-05-22 16:23:47 -04:00
}
async disableOAuthLogin(): Promise<void> {
const config = await this.getConfig({ withCache: false });
2024-05-22 16:23:47 -04:00
config.oauth.enabled = false;
await this.updateConfig(config);
2024-05-22 16:23:47 -04:00
}
async enableOAuthLogin(): Promise<void> {
const config = await this.getConfig({ withCache: false });
2024-05-22 16:23:47 -04:00
config.oauth.enabled = true;
await this.updateConfig(config);
2024-05-22 16:23:47 -04:00
}
cleanup() {
return this.databaseRepository.shutdown();
}
2024-05-22 16:23:47 -04:00
}