You've already forked pigallery2
mirror of
https://github.com/bpatrik/pigallery2.git
synced 2025-12-07 23:23:49 +02:00
implementing mysql user management
This commit is contained in:
65
backend/model/mysql/MySQLConnection.ts
Normal file
65
backend/model/mysql/MySQLConnection.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import "reflect-metadata";
|
||||
import {createConnection, Connection} from "typeorm";
|
||||
import {Config} from "../../config/Config";
|
||||
import {UserEntity} from "./enitites/UserEntity";
|
||||
import {UserRoles} from "../../../common/entities/UserDTO";
|
||||
|
||||
|
||||
export class MySQLConnection {
|
||||
|
||||
constructor() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static connection: Connection = null;
|
||||
|
||||
public static getConnection(): Promise<Connection> {
|
||||
return new Promise<Connection>((resolve, reject) => {
|
||||
|
||||
if (this.connection != null) {
|
||||
return resolve(this.connection);
|
||||
}
|
||||
|
||||
createConnection({
|
||||
driver: {
|
||||
type: "mysql",
|
||||
host: Config.Server.database.mysql.host,
|
||||
port: 3306,
|
||||
username: Config.Server.database.mysql.username,
|
||||
password: Config.Server.database.mysql.password,
|
||||
database: Config.Server.database.mysql.database
|
||||
},
|
||||
entities: [
|
||||
UserEntity
|
||||
],
|
||||
autoSchemaSync: true,
|
||||
}).then((conn) => {
|
||||
this.connection = conn;
|
||||
return resolve(this.connection);
|
||||
|
||||
}).catch(err => reject(err));
|
||||
});
|
||||
}
|
||||
|
||||
public static init(): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
this.getConnection().then(async connection => {
|
||||
|
||||
let userRepository = connection.getRepository(UserEntity);
|
||||
let admins = await userRepository.find({role: UserRoles.Admin});
|
||||
if (admins.length == 0) {
|
||||
let a = new UserEntity();
|
||||
a.name = "admin";
|
||||
a.password = "admin";
|
||||
a.role = UserRoles.Admin;
|
||||
await userRepository.persist(a);
|
||||
}
|
||||
|
||||
resolve();
|
||||
}).catch(err => reject(err));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user