Skip to content
Snippets Groups Projects
Commit 81b631d8 authored by Renzo Beux's avatar Renzo Beux
Browse files

Auditor added

parent a1984911
No related branches found
No related tags found
No related merge requests found
PORT=8000
INSTANCE=PROD
PORT=7999
INSTANCE=TEST
HOST=localhost
USER=root
PASSWORD=rootroot
DB=parameter_database
AUTH_BASE_URL=
\ No newline at end of file
USER=developer
PASSWORD=password
DB=core_database_test
AUTH_BASE_URL=localhost:7998/users
\ No newline at end of file
import { Sequelize } from 'sequelize';
const DB: string = process.env.DB || 'parameter_database';
const DB: string = process.env.DB || 'core_database';
const USER: string = process.env.USER || 'root';
const PASSWORD: string = process.env.PASSWORD || 'password';
const HOST: string = process.env.HOST || 'localhost';
......
......@@ -4,6 +4,7 @@ import path from 'path';
import DefaultExtraDataDTO from '../DTOs/DefaultExtraDataDTO';
import DefaultWeightDTO from '../DTOs/DefaultWeightDTO';
import EquationConstantDTO from '../DTOs/EquationConstantDTO';
import Auditor from '../Models/Auditor';
import DefaultExtraData from '../Models/DefaultExtraData';
import DefaultWeight from '../Models/DefaultWeight';
import EquationConstant from '../Models/EquationConstant';
......@@ -49,6 +50,7 @@ function initParameterDataBase(): void {
console.log(err);
});
});
Auditor.sync({ force: true });
}
export default { initParameterDataBase };
import express, { Request, Response, NextFunction } from 'express';
import { Response, NextFunction } from 'express';
import { validate } from '../Services/UserAPI';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const authChecker = (req: any, res: Response, next: NextFunction): void => {
const token = req.headers.authorization;
const userId = validate(token);
if (userId === -1) {
res.status(401).send('auth failed');
res.status(401).send({ message: 'auth failed' });
return;
}
req.user_id = userId;
next();
......
import { DataTypes, Model } from 'sequelize';
import sequelize from '../Loaders/ParameterDataBase';
class Auditor extends Model {
id!: number;
user_id!: number;
action!: string;
time!: string;
}
Auditor.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
user_id: DataTypes.INTEGER,
action: DataTypes.STRING,
time: DataTypes.DATE,
},
{
sequelize,
modelName: 'Auditor',
timestamps: true,
});
// BD: ID // USER ID // ACTION // TIME
export default Auditor;
......@@ -20,11 +20,11 @@ class EquationConstant extends Parameter {
EquationConstant.init(
{
ageRange: {
type: DataTypes.STRING,
type: DataTypes.STRING(128),
primaryKey: true,
},
sex: {
type: DataTypes.STRING,
type: DataTypes.STRING(20),
primaryKey: true,
},
order: {
......
import Auditor from '../Models/Auditor';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const audit = (request: any, action: string): void => {
const userId = request.user_id;
Auditor.create({ user_id: userId, action });
};
// BD: UUID // USER ID // ACTION // TIME
......@@ -6,7 +6,6 @@ import express, {
Request,
Response,
} from 'express';
import 'dotenv/config';
import cors from 'cors';
import swaggerUi from 'swagger-ui-express';
import helmet from 'helmet';
......@@ -14,6 +13,9 @@ import YAML from 'yamljs';
import Routes from './routes';
import logger from './Logger/logger';
import ParameterDataBaseLoader from './Loaders/ParameterDataBaseLoader';
import authChecker from './Middlewares/authChecker';
require('dotenv').config();
const app: Application = express();
const PORT = process.env.PORT || 8000;
......@@ -38,11 +40,7 @@ app.use(express.raw({
limit: '50mb',
}));
const auditMiddleware = (req, res, next) => {
next();
};
app.use(authChecker);
app.use(auditMiddleware);
app.use(Routes);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment