This commit is contained in:
fro1991
2026-01-02 22:00:52 +07:00
parent 6309e9b89f
commit f562ccf38b
4 changed files with 105 additions and 70 deletions

View File

@ -46,11 +46,11 @@ export class AuthController {
const userRepository = OrmHelper.DB.getRepository(User)
const userRefreshTokenRepository = OrmHelper.DB.getRepository(UserRefreshToken)
const user = await userRepository.findOne({
relations: ["roles", "roles.application"],
select: ["id", "email", "name", "employee_id", "password", "username", "roles"],
where: { username: param.username },
})
const user = await userRepository.findOne({
relations: ["roles", "roles.application"],
select: ["id", "email", "name", "password", "username", "roles"],
where: { username: param.username },
})
if (user != null && user.roles != null && user.roles.length > 0 && user.checkIfPasswordMatch(param.password)) {
const { browser, device, os } = UAParser(req.get("User-Agent"))
@ -85,37 +85,36 @@ export class AuthController {
return ReturnHelper.errorResponse(res, 403, 403, Language.lang.failed_access + ", you don't have any roles in this application")
} else {
// const userRole = await userRoleRepository.findOneBy({ id: role_id })
var hrms = {
uuid: null,
idx: null,
employee_id: null,
employeename: null,
title: null,
department_id: null,
departement_uuid: null,
department_name: null,
level_id: null,
level_name: null,
}
var hrms = {
uuid: null,
idx: null,
employee_id: null,
employeename: null,
title: null,
department_id: null,
departement_uuid: null,
department_name: null,
level_id: null,
level_name: null,
}
var privateKey = fs.readFileSync("src/helpers/key/private.key")
var privateKey = fs.readFileSync("src/helpers/key/private.key")
var token = jwt.sign(
{
exp: Math.floor(Date.now() / 1000) + Number(config.get("auth.access_token_lifetime")),
data: {
id: user.id,
username: user.username,
name: user.name,
id_role: role_id,
roles: roles.roles,
employee_id: user.employee_id,
hrms: hrms,
var token = jwt.sign(
{
exp: Math.floor(Date.now() / 1000) + Number(config.get("auth.access_token_lifetime")),
data: {
id: user.id,
username: user.username,
name: user.name,
id_role: role_id,
roles: roles.roles,
hrms: hrms,
},
},
},
privateKey,
{ algorithm: "RS256" }
)
privateKey,
{ algorithm: "RS256" }
)
delete user.password

View File

@ -1,6 +1,6 @@
import axios from "axios"
import config from "config"
import { Paging, User, UserRefreshToken, UserRole } from "entity"
import { HrmsEmployee, Paging, User, UserRefreshToken, UserRole } from "entity"
import exceljs from "exceljs"
import { NextFunction, Response } from "express"
import { Request } from "express-jwt"
@ -247,7 +247,7 @@ export class UserController {
password: Joi.string().pattern(new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$")).min(8).required().label("Password"),
retype_password: Joi.ref("password"),
name: Joi.string().max(64).required().label("Name"),
employee_id: Joi.string().max(64).optional().allow("").label("Employee ID"),
employee_id: Joi.string().uuid().optional().allow("").label("Employee ID"),
status: Joi.string().required().label("Status"),
})
@ -260,8 +260,8 @@ export class UserController {
data.email = param.email
data.status = param.status
if (param.employee_id && param.employee_id! + "") {
data.employee_id = param.employee_id
if (param.employee && param.employee.id! + "") {
data.employee = param.employee
}
data.hashPassword()
@ -571,7 +571,7 @@ export class UserController {
password: Joi.string().allow("").optional().label("Password"),
retype_password: Joi.ref("password"),
name: Joi.string().max(64).required().label("Name"),
employee_id: Joi.string().max(64).optional().allow("").label("Employee ID"),
employee_id: Joi.string().uuid().optional().allow("").label("Employee ID"),
status: Joi.string().required().label("Status"),
})
@ -585,13 +585,13 @@ export class UserController {
if (data != null) {
data.name = param.name
data.employee_id = param.employee_id
data.employee = param.employee
data.username = param.username
data.email = param.email
data.status = param.status
if (param.employee_id && param.employee_id != "") {
data.employee_id = param.employee_id
if (param.employee && param.employee.id != "") {
data.employee = param.employee
}
if (param.password) {

View File

@ -1,34 +1,69 @@
import config from 'config';
import { DataSource } from "typeorm";
import { Application, Menu, User, UserActivity, UserRefreshToken, UserRole } from "entity";
import { ILogObj, Logger } from 'tslog';
import config from "config"
import { DataSource } from "typeorm"
import {
Aldeia,
Suco,
Munisipo,
Administrativu,
Nationality,
Application, //
Menu,
User,
UserActivity,
UserRefreshToken,
UserRole,
HrmsAirport,
HrmsPosition,
HrmsEmployee,
HrmsDepartment,
HrmsShift,
} from "entity"
import { ILogObj, Logger } from "tslog"
export class OrmHelper {
static DB: DataSource = null
static DB: DataSource = null
static setup() {
const log: Logger<ILogObj> = new Logger({ name: '[OrmHelper]', type: 'pretty' });
static setup() {
const log: Logger<ILogObj> = new Logger({ name: "[OrmHelper]", type: "pretty" })
const engine: 'mysql' | 'postgres' = config.get("database.engine")
const engine: "mysql" | "postgres" = config.get("database.engine")
OrmHelper.DB = new DataSource({
type: engine,
host: config.get("database.host"),
port: Number(config.get("database.port")),
username: String(config.get("database.username")),
password: String(config.get("database.password")),
database: String(config.get("database.database")),
synchronize: true,
logging: config.get('database.logging'),
entities: [User, UserRole, UserActivity, UserRefreshToken, Menu, Application],
subscribers: [],
migrations: [],
})
OrmHelper.DB = new DataSource({
type: engine,
host: config.get("database.host"),
port: Number(config.get("database.port")),
username: String(config.get("database.username")),
password: String(config.get("database.password")),
database: String(config.get("database.database")),
synchronize: true,
logging: config.get("database.logging"),
entities: [
Aldeia,
Suco,
Munisipo,
Administrativu,
Nationality,
User, //
UserRole,
UserActivity,
UserRefreshToken,
Menu,
Application,
Nationality,
HrmsDepartment, //
HrmsAirport,
HrmsPosition,
HrmsShift,
HrmsEmployee,
],
subscribers: [],
migrations: [],
})
OrmHelper.DB.initialize()
.then(() => {
// here you can start to work with your database
})
.catch((error: any) => log.error(error))
}
}
OrmHelper.DB.initialize()
.then(() => {
// here you can start to work with your database
})
.catch((error: any) => log.error(error))
}
}