2024-05-22 16:23:47 -04:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2024-05-26 18:15:52 -04:00
|
|
|
import { SALT_ROUNDS } from 'src/constants';
|
|
|
|
|
import { UserAdminResponseDto, mapUserAdmin } from 'src/dtos/user.dto';
|
2024-10-01 16:03:55 -04:00
|
|
|
import { IConfigRepository } from 'src/interfaces/config.interface';
|
2024-05-22 16:23:47 -04:00
|
|
|
import { ICryptoRepository } from 'src/interfaces/crypto.interface';
|
|
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
|
|
|
|
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
|
|
|
|
|
import { IUserRepository } from 'src/interfaces/user.interface';
|
2024-09-30 17:31:21 -04:00
|
|
|
import { BaseService } from 'src/services/base.service';
|
2024-05-22 16:23:47 -04:00
|
|
|
|
|
|
|
|
@Injectable()
|
2024-09-30 17:31:21 -04:00
|
|
|
export class CliService extends BaseService {
|
2024-05-22 16:23:47 -04:00
|
|
|
constructor(
|
2024-10-01 16:03:55 -04:00
|
|
|
@Inject(IConfigRepository) configRepository: IConfigRepository,
|
2024-05-22 16:23:47 -04:00
|
|
|
@Inject(ICryptoRepository) private cryptoRepository: ICryptoRepository,
|
|
|
|
|
@Inject(ISystemMetadataRepository) systemMetadataRepository: ISystemMetadataRepository,
|
|
|
|
|
@Inject(IUserRepository) private userRepository: IUserRepository,
|
2024-09-30 17:31:21 -04:00
|
|
|
@Inject(ILoggerRepository) logger: ILoggerRepository,
|
2024-05-22 16:23:47 -04:00
|
|
|
) {
|
2024-10-01 16:03:55 -04:00
|
|
|
super(configRepository, systemMetadataRepository, logger);
|
2024-05-22 16:23:47 -04:00
|
|
|
this.logger.setContext(CliService.name);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 18:15:52 -04:00
|
|
|
async listUsers(): Promise<UserAdminResponseDto[]> {
|
2024-05-22 16:23:47 -04:00
|
|
|
const users = await this.userRepository.getList({ withDeleted: true });
|
2024-05-26 18:15:52 -04:00
|
|
|
return users.map((user) => mapUserAdmin(user));
|
2024-05-22 16:23:47 -04:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 18:15:52 -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');
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 18:15:52 -04:00
|
|
|
const providedPassword = await ask(mapUserAdmin(admin));
|
2024-05-22 16:23:47 -04:00
|
|
|
const password = providedPassword || this.cryptoRepository.newPassword(24);
|
2024-05-26 18:15:52 -04:00
|
|
|
const hashedPassword = await this.cryptoRepository.hashBcrypt(password, SALT_ROUNDS);
|
2024-05-22 16:23:47 -04:00
|
|
|
|
2024-05-26 18:15:52 -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> {
|
2024-09-30 17:31:21 -04:00
|
|
|
const config = await this.getConfig({ withCache: false });
|
2024-05-22 16:23:47 -04:00
|
|
|
config.passwordLogin.enabled = false;
|
2024-09-30 17:31:21 -04:00
|
|
|
await this.updateConfig(config);
|
2024-05-22 16:23:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async enablePasswordLogin(): Promise<void> {
|
2024-09-30 17:31:21 -04:00
|
|
|
const config = await this.getConfig({ withCache: false });
|
2024-05-22 16:23:47 -04:00
|
|
|
config.passwordLogin.enabled = true;
|
2024-09-30 17:31:21 -04:00
|
|
|
await this.updateConfig(config);
|
2024-05-22 16:23:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async disableOAuthLogin(): Promise<void> {
|
2024-09-30 17:31:21 -04:00
|
|
|
const config = await this.getConfig({ withCache: false });
|
2024-05-22 16:23:47 -04:00
|
|
|
config.oauth.enabled = false;
|
2024-09-30 17:31:21 -04:00
|
|
|
await this.updateConfig(config);
|
2024-05-22 16:23:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async enableOAuthLogin(): Promise<void> {
|
2024-09-30 17:31:21 -04:00
|
|
|
const config = await this.getConfig({ withCache: false });
|
2024-05-22 16:23:47 -04:00
|
|
|
config.oauth.enabled = true;
|
2024-09-30 17:31:21 -04:00
|
|
|
await this.updateConfig(config);
|
2024-05-22 16:23:47 -04:00
|
|
|
}
|
|
|
|
|
}
|