2022-03-12 15:10:22 +01:00
|
|
|
import { Logger, Module, OnModuleInit } from '@nestjs/common';
|
2022-02-21 14:53:21 +01:00
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
2022-03-12 15:10:22 +01:00
|
|
|
import { HasFailed } from 'picsur-shared/dist/types';
|
2022-03-23 20:40:31 +01:00
|
|
|
import { generateRandomString } from 'picsur-shared/dist/util/random';
|
2022-03-28 12:27:25 +02:00
|
|
|
import { AuthConfigService } from '../../config/early/auth.config.service';
|
|
|
|
|
import { EarlyConfigModule } from '../../config/early/earlyconfig.module';
|
2022-03-03 22:48:03 +01:00
|
|
|
import { EUserBackend } from '../../models/entities/user.entity';
|
2022-04-13 17:28:48 +02:00
|
|
|
import { PreferenceModule } from '../preferencesdb/preferencedb.module';
|
2022-03-12 15:10:22 +01:00
|
|
|
import { RolesModule } from '../roledb/roledb.module';
|
2022-02-21 22:36:47 +01:00
|
|
|
import { UsersService } from './userdb.service';
|
2022-02-21 14:53:21 +01:00
|
|
|
|
|
|
|
|
@Module({
|
2022-03-12 15:10:22 +01:00
|
|
|
imports: [
|
2022-03-28 12:27:25 +02:00
|
|
|
EarlyConfigModule,
|
2022-03-12 15:10:22 +01:00
|
|
|
RolesModule,
|
2022-04-13 17:28:48 +02:00
|
|
|
PreferenceModule,
|
2022-03-12 15:10:22 +01:00
|
|
|
TypeOrmModule.forFeature([EUserBackend]),
|
|
|
|
|
],
|
2022-03-30 13:41:33 +02:00
|
|
|
providers: [UsersService],
|
|
|
|
|
exports: [UsersService],
|
2022-02-21 14:53:21 +01:00
|
|
|
})
|
2022-03-12 15:10:22 +01:00
|
|
|
export class UsersModule implements OnModuleInit {
|
|
|
|
|
private readonly logger = new Logger('UsersModule');
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private usersService: UsersService,
|
|
|
|
|
private authConfigService: AuthConfigService,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
async onModuleInit() {
|
2022-03-27 23:56:25 +02:00
|
|
|
await this.ensureUserExists(
|
|
|
|
|
'guest',
|
|
|
|
|
// Guest should never be able to login
|
|
|
|
|
// It should be prevented even if you know the password
|
|
|
|
|
// But to be sure, we set it to a random string
|
|
|
|
|
generateRandomString(128),
|
2022-03-23 20:40:31 +01:00
|
|
|
['guest'],
|
|
|
|
|
);
|
2022-03-27 23:56:25 +02:00
|
|
|
await this.ensureUserExists(
|
|
|
|
|
'admin',
|
|
|
|
|
this.authConfigService.getDefaultAdminPassword(),
|
|
|
|
|
['user', 'admin'],
|
|
|
|
|
);
|
2022-03-23 20:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-27 23:56:25 +02:00
|
|
|
private async ensureUserExists(
|
|
|
|
|
username: string,
|
|
|
|
|
password: string,
|
|
|
|
|
roles: string[],
|
|
|
|
|
) {
|
|
|
|
|
this.logger.debug(`Ensuring user "${username}" exists`);
|
2022-03-12 15:10:22 +01:00
|
|
|
|
2022-03-23 20:40:31 +01:00
|
|
|
const exists = await this.usersService.exists(username);
|
|
|
|
|
if (exists) return;
|
|
|
|
|
|
|
|
|
|
const newUser = await this.usersService.create(
|
|
|
|
|
username,
|
|
|
|
|
password,
|
2022-03-27 23:56:25 +02:00
|
|
|
roles,
|
2022-04-13 21:14:24 +02:00
|
|
|
true,
|
2022-03-23 20:40:31 +01:00
|
|
|
);
|
|
|
|
|
if (HasFailed(newUser)) {
|
2022-03-12 15:10:22 +01:00
|
|
|
this.logger.error(
|
2022-03-27 23:56:25 +02:00
|
|
|
`Failed to create user "${username}" because: ${newUser.getReason()}`,
|
2022-03-12 15:10:22 +01:00
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|