2016-12-27 23:09:47 +08:00
|
|
|
import {UserDTO, UserRoles} from "../../../common/entities/UserDTO";
|
|
|
|
import {IUserManager} from "../interfaces/IUserManager";
|
|
|
|
import {UserEntity} from "./enitites/UserEntity";
|
|
|
|
import {MySQLConnection} from "./MySQLConnection";
|
2017-07-10 04:36:25 +08:00
|
|
|
import {PasswordHelper} from "../PasswordHelper";
|
|
|
|
|
2016-12-27 23:09:47 +08:00
|
|
|
|
|
|
|
export class UserManager implements IUserManager {
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
constructor() {
|
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public async findOne(filter: any) {
|
|
|
|
const connection = await MySQLConnection.getConnection();
|
2017-07-10 04:36:25 +08:00
|
|
|
let pass = filter.password;
|
|
|
|
delete filter.password;
|
2017-07-04 01:17:49 +08:00
|
|
|
const user = (await connection.getRepository(UserEntity).findOne(filter));
|
2017-07-10 04:36:25 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
if (user.permissions && user.permissions != null) {
|
|
|
|
user.permissions = <any>JSON.parse(<any>user.permissions);
|
2016-12-27 23:09:47 +08:00
|
|
|
}
|
2017-07-10 04:36:25 +08:00
|
|
|
|
|
|
|
if (pass && !PasswordHelper.comparePassword(pass, user.password)) {
|
|
|
|
throw "No entry found";
|
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
return user;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public async find(filter: any) {
|
|
|
|
const connection = await MySQLConnection.getConnection();
|
|
|
|
return (await connection.getRepository(UserEntity).find(filter)).map(user => {
|
|
|
|
if (user.permissions && user.permissions != null) {
|
|
|
|
user.permissions = <any>JSON.parse(<any>user.permissions);
|
|
|
|
}
|
|
|
|
return user;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async createUser(user: UserDTO) {
|
|
|
|
const connection = await MySQLConnection.getConnection();
|
|
|
|
if (user.permissions && user.permissions != null) {
|
|
|
|
user.permissions = <any>JSON.stringify(<any>user.permissions);
|
2016-12-27 23:09:47 +08:00
|
|
|
}
|
2017-07-11 15:01:59 +08:00
|
|
|
user.password = PasswordHelper.cryptPassword(user.password);
|
2017-07-04 01:17:49 +08:00
|
|
|
return await connection.getRepository(UserEntity).persist(user);
|
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public async deleteUser(id: number) {
|
|
|
|
const connection = await MySQLConnection.getConnection();
|
|
|
|
const user = await connection.getRepository(UserEntity).findOne({id: id});
|
|
|
|
return await connection.getRepository(UserEntity).remove(user);
|
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public async changeRole(id: number, newRole: UserRoles) {
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
const connection = await MySQLConnection.getConnection();
|
|
|
|
let userRepository = connection.getRepository(UserEntity);
|
|
|
|
const user = await userRepository.findOne({id: id});
|
|
|
|
user.role = newRole;
|
|
|
|
return await userRepository.persist(user);
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public async changePassword(request: any) {
|
|
|
|
throw new Error("not implemented"); //TODO: implement
|
|
|
|
}
|
2016-12-27 23:09:47 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|