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

View File

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

View File

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