1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

Creating enforced users in the config file fixes #220, fixes #393,

This commit is contained in:
Patrik J. Braun 2022-01-13 23:55:29 +01:00
parent 938a1386fc
commit e956199c8e
4 changed files with 49 additions and 15 deletions

View File

@ -96,22 +96,29 @@ export class SQLConnection {
public static async init(): Promise<void> {
const connection = await this.getConnection();
// Add dummy Admin to the db
// Adding enforced users to the db
const userRepository = connection.getRepository(UserEntity);
const admins = await userRepository.find({role: UserRoles.Admin});
if (admins.length === 0) {
const a = new UserEntity();
a.name = 'admin';
a.password = PasswordHelper.cryptPassword('admin');
a.role = UserRoles.Admin;
await userRepository.save(a);
}
const defAdmins = await userRepository.find({name: 'admin', role: UserRoles.Admin});
for (const a of defAdmins) {
if (PasswordHelper.comparePassword('admin', a.password)) {
NotificationManager.error('Using default admin user!', 'You are using the default admin/admin user/password, please change or remove it.');
for (const uc of Config.Server.Database.enforcedUsers) {
const user = await userRepository.findOne({name: uc.name});
if (!user) {
Logger.info(LOG_TAG, 'Saving enforced user: ' + uc.name);
const a = new UserEntity();
a.name = uc.name;
// encrypt password and save back to the db
if (!uc.encryptedPassword) {
uc.encryptedPassword = PasswordHelper.cryptPassword(uc.password);
uc.password = '';
await Config.save();
}
a.password = uc.encryptedPassword;
a.role = uc.role;
await userRepository.save(a);
}
}
const defAdmin = await userRepository.findOne({name: 'admin', role: UserRoles.Admin});
if (PasswordHelper.comparePassword('admin', defAdmin.password)) {
NotificationManager.error('Using default admin user!', 'You are using the default admin/admin user/password, please change or remove it.');
}
}

View File

@ -8,7 +8,7 @@ export class UserEntity implements UserDTO {
@PrimaryGeneratedColumn()
id: number;
@Column()
@Column({unique: true})
name: string;
@Column()

View File

@ -7,6 +7,7 @@ import {ConfigProperty} from 'typeconfig/src/decorators/property/ConfigPropoerty
import {DefaultsJobs} from '../../entities/job/JobDTO';
import {SearchQueryDTO} from '../../entities/SearchQueryDTO';
import {SortingMethods} from '../../entities/SortingMethods';
import {UserRoles} from '../../entities/UserDTO';
export enum DatabaseType {
memory = 1, mysql = 2, sqlite = 3
@ -56,6 +57,28 @@ export class SQLiteConfig {
DBFileName: string = 'sqlite.db';
}
@SubConfigClass()
export class UserConfig {
@ConfigProperty()
name: string;
@ConfigProperty({type: UserRoles})
role: UserRoles;
@ConfigProperty()
password: string;
@ConfigProperty()
encryptedPassword: string;
constructor(name: string, password: string, role: UserRoles) {
this.name = name;
this.role = role;
this.password = password;
}
}
@SubConfigClass()
export class ServerDataBaseConfig {
@ -78,6 +101,10 @@ export class ServerDataBaseConfig {
@ConfigProperty()
mysql?: MySQLConfig = new MySQLConfig();
@ConfigProperty({arrayType: UserConfig})
enforcedUsers: UserConfig[] = [new UserConfig('admin', 'admin', UserRoles.Admin)];
}
@SubConfigClass()

View File

@ -3,7 +3,7 @@ import 'reflect-metadata';
import {SortingMethods} from '../../entities/SortingMethods';
import {UserRoles} from '../../entities/UserDTO';
import {ConfigProperty, SubConfigClass} from 'typeconfig/common';
import {DatabaseType, IPrivateConfig} from '../private/PrivateConfig';
import {IPrivateConfig} from '../private/PrivateConfig';
export enum MapProviders {