1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/memory/UserManager.ts

112 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {UserDTO, UserRoles} from '../../../common/entities/UserDTO';
import {IUserManager} from '../interfaces/IUserManager';
import {ProjectPath} from '../../ProjectPath';
import {Utils} from '../../../common/Utils';
import * as path from 'path';
import * as fs from 'fs';
import {PasswordHelper} from '../PasswordHelper';
2016-05-09 23:04:56 +08:00
export class UserManager implements IUserManager {
2017-07-26 05:39:50 +08:00
private db: { users?: UserDTO[], idCounter?: number } = {};
2018-11-29 06:49:33 +08:00
private readonly dbPath: string;
2017-07-04 01:17:49 +08:00
generateId(): string {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
2017-07-04 01:17:49 +08:00
return s4() + s4() + s4() + s4();
}
2017-07-04 01:17:49 +08:00
constructor() {
2017-07-26 05:39:50 +08:00
this.dbPath = path.join(ProjectPath.Root, 'users.db');
if (fs.existsSync(this.dbPath)) {
this.loadDB();
}
2017-07-26 05:39:50 +08:00
if (!this.db.idCounter) {
this.db.idCounter = 1;
}
2016-04-22 19:23:44 +08:00
2017-07-26 05:39:50 +08:00
if (!this.db.users) {
this.db.users = [];
// TODO: remove defaults
2018-03-31 03:30:30 +08:00
this.createUser(<UserDTO>{name: 'admin', password: 'admin', role: UserRoles.Admin});
2016-04-22 19:23:44 +08:00
}
2017-07-26 05:39:50 +08:00
this.saveDB();
2017-07-04 01:17:49 +08:00
}
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
public async findOne(filter: any) {
const result = await this.find(filter);
2016-04-22 19:23:44 +08:00
if (result.length === 0) {
throw new Error('UserDTO not found');
2016-04-22 19:23:44 +08:00
}
2017-07-04 01:17:49 +08:00
return result[0];
}
public async find(filter: any) {
const pass = filter.password;
2017-07-10 04:36:25 +08:00
delete filter.password;
2017-07-26 05:39:50 +08:00
const users = this.db.users.slice();
2017-07-11 00:08:39 +08:00
let i = users.length;
while (i--) {
2017-07-11 15:01:59 +08:00
if (pass && !(PasswordHelper.comparePassword(pass, users[i].password))) {
2017-07-11 00:08:39 +08:00
users.splice(i, 1);
continue;
2017-07-10 04:36:25 +08:00
}
if (Utils.equalsFilter(users[i], filter) === false) {
2017-07-11 00:08:39 +08:00
users.splice(i, 1);
}
}
return users;
2017-07-04 01:17:49 +08:00
}
public async createUser(user: UserDTO) {
2017-07-26 05:39:50 +08:00
user.id = this.db.idCounter++;
2017-07-11 15:01:59 +08:00
user.password = PasswordHelper.cryptPassword(user.password);
2017-07-26 05:39:50 +08:00
this.db.users.push(user);
this.saveDB();
2017-07-04 01:17:49 +08:00
return user;
}
2017-07-26 05:39:50 +08:00
public async deleteUser(id: number) {
const deleted = this.db.users.filter((u: UserDTO) => u.id === id);
this.db.users = this.db.users.filter((u: UserDTO) => u.id !== id);
2017-07-26 05:39:50 +08:00
this.saveDB();
2017-07-04 01:17:49 +08:00
if (deleted.length > 0) {
return deleted[0];
2016-04-22 19:23:44 +08:00
}
2017-07-04 01:17:49 +08:00
return null;
}
2016-05-09 23:04:56 +08:00
2017-07-04 01:17:49 +08:00
public async changeRole(id: number, newRole: UserRoles): Promise<UserDTO> {
2017-07-26 05:39:50 +08:00
for (let i = 0; i < this.db.users.length; i++) {
if (this.db.users[i].id === id) {
2017-07-26 05:39:50 +08:00
this.db.users[i].role = newRole;
this.saveDB();
return this.db.users[i];
2017-07-04 01:17:49 +08:00
}
2016-04-22 19:23:44 +08:00
}
2017-07-04 01:17:49 +08:00
}
2016-05-09 23:04:56 +08:00
2017-07-04 01:17:49 +08:00
public async changePassword(request: any) {
throw new Error('not implemented'); // TODO: implement
2017-07-04 01:17:49 +08:00
}
2016-04-22 19:23:44 +08:00
2017-07-26 05:39:50 +08:00
private loadDB() {
const data = fs.readFileSync(this.dbPath, 'utf8');
this.db = JSON.parse(data);
}
private saveDB() {
fs.writeFileSync(this.dbPath, JSON.stringify(this.db));
}
2017-07-04 01:17:49 +08:00
}