#12 audit and history

This commit is contained in:
2026-02-05 17:02:27 +02:00
parent 6e95ac7999
commit 758fc6be42
7 changed files with 34 additions and 10 deletions
+1
View File
@@ -34,6 +34,7 @@ class AccessManager {
async addToHistory(id, collection, action){ async addToHistory(id, collection, action){
let o; let o;
if (typeof id == 'string') o = await this.app.db.get(collection, {'_id': this.app.db.ObjectId(id)}); if (typeof id == 'string') o = await this.app.db.get(collection, {'_id': this.app.db.ObjectId(id)});
else if (typeof id == 'number') o = await this.app.db.get(collection, {id});
else o = id; else o = id;
o._oid = this.app.db.ObjectId(o._id); o._oid = this.app.db.ObjectId(o._id);
o._from = collection; o._from = collection;
+3 -1
View File
@@ -22,7 +22,7 @@ class GameObjectsManager{
* @param {App} app The Application, обект приложение * @param {App} app The Application, обект приложение
*/ */
init(app){ init(app){
const {db, config} = app; const {db, config, am} = app;
/** /**
* Creates a game object, създаване на игрови обект * Creates a game object, създаване на игрови обект
@@ -58,6 +58,7 @@ class GameObjectsManager{
this.update = async function(ctx, data){ this.update = async function(ctx, data){
data.id = parseInt(data.id); data.id = parseInt(data.id);
let object = await this.read(data.id); let object = await this.read(data.id);
await am.addToHistory(object, collection, 'update');
data = Object.assign(object, data); data = Object.assign(object, data);
if (ctx.files?.file){ if (ctx.files?.file){
await this.addFile(data, ctx.files.file) await this.addFile(data, ctx.files.file)
@@ -76,6 +77,7 @@ class GameObjectsManager{
*/ */
this.remove = async function(id){ this.remove = async function(id){
id = parseInt(id); id = parseInt(id);
await am.addToHistory(id, collection, 'delete');
await db.remove(collection, {id}); await db.remove(collection, {id});
} }
+3 -2
View File
@@ -11,7 +11,7 @@ class GamesManager{
* @param {App} app Class initializer, основна апликация * @param {App} app Class initializer, основна апликация
*/ */
init(app){ init(app){
const {db} = app; const {db, am} = app;
/** /**
* Creates a new game definition, създаване на нова игрова дефиниция * Creates a new game definition, създаване на нова игрова дефиниция
@@ -42,6 +42,7 @@ class GamesManager{
this.update = async function(ctx, data){ this.update = async function(ctx, data){
data.id = parseInt(data.id); data.id = parseInt(data.id);
let object = await this.read(data.id); let object = await this.read(data.id);
await am.addToHistory(object, collection, 'update');
data = Object.assign(object, data); data = Object.assign(object, data);
await db.update(collection, {id: data.id}, data); await db.update(collection, {id: data.id}, data);
return data; return data;
@@ -53,6 +54,7 @@ class GamesManager{
*/ */
this.remove = async function(id){ this.remove = async function(id){
id = parseInt(id); id = parseInt(id);
await am.addToHistory(id, collection, 'delete');
await db.remove(collection, {id}); await db.remove(collection, {id});
} }
@@ -67,7 +69,6 @@ class GamesManager{
project: { name:1, id:1, thumb: 1} project: { name:1, id:1, thumb: 1}
}); });
} }
} }
/** /**
+3 -1
View File
@@ -12,7 +12,7 @@ class ScenariosManager{
* @param {App} app Class initializer, основна апликация * @param {App} app Class initializer, основна апликация
*/ */
init(app){ init(app){
const {db} = app; const {db, am} = app;
/** /**
* Creates a new scenario, създаване на нов сценарий * Creates a new scenario, създаване на нов сценарий
@@ -43,6 +43,7 @@ class ScenariosManager{
this.update = async function(ctx, data){ this.update = async function(ctx, data){
data.id = parseInt(data.id); data.id = parseInt(data.id);
let object = await this.read(data.id); let object = await this.read(data.id);
await am.addToHistory(object, collection, 'update');
data = Object.assign(object, data); data = Object.assign(object, data);
await db.update(collection, {id: data.id}, data); await db.update(collection, {id: data.id}, data);
return data; return data;
@@ -54,6 +55,7 @@ class ScenariosManager{
*/ */
this.remove = async function(id){ this.remove = async function(id){
id = parseInt(id); id = parseInt(id);
await am.addToHistory(id, collection, 'delete');
await db.remove(collection, {id}); await db.remove(collection, {id});
} }
@@ -16,7 +16,7 @@ class GameObjectsController{
* @param {App} app The application instance, апликация * @param {App} app The application instance, апликация
*/ */
init(app){ init(app){
const { gameObject } = app; const { gameObject, am } = app;
const router = express.Router(); const router = express.Router();
/** /**
@@ -27,11 +27,14 @@ class GameObjectsController{
router.put('/', multipartMiddleware, async (req, res)=>{ router.put('/', multipartMiddleware, async (req, res)=>{
try{ try{
let data = req.body; let data = req.body;
let object = await gameObject[data.id? 'update' : 'create'](req, data) let action = data.id ? 'update' : 'create';
let object = await gameObject[action](req, data)
res.json({status: 'OK', object}); res.json({status: 'OK', object});
am.audit(req, `game-object-${action}`, object.id);
}catch(err){ }catch(err){
console.error(err); console.error(err);
res.status(500).json({status: 'ERR', err}); res.status(500).json({status: 'ERR', err});
am.audit(req, `game-object-alter-error`, req.body?.id, {q: req.body, e: err});
} }
}); });
@@ -44,6 +47,7 @@ class GameObjectsController{
router.post('/', async (req, res)=>{ router.post('/', async (req, res)=>{
let result = await gameObject.list(req.body); let result = await gameObject.list(req.body);
res.json(result); res.json(result);
am.audit(req, `game-object-list`, null, {q: req.body});
}) })
/** /**
@@ -56,6 +60,7 @@ class GameObjectsController{
router.get('/:id', async (req, res)=>{ router.get('/:id', async (req, res)=>{
let object = await gameObject.read(parseInt(req.params.id)); let object = await gameObject.read(parseInt(req.params.id));
res.json(object); res.json(object);
am.audit(req, `game-object-read`, object.id);
}) })
/** /**
@@ -67,6 +72,7 @@ class GameObjectsController{
router.delete('/:id', async (req, res)=>{ router.delete('/:id', async (req, res)=>{
await gameObject.remove(req.params.id); await gameObject.remove(req.params.id);
res.json({status: 'OK'}); res.json({status: 'OK'});
am.audit(req, `game-object-delete`, req.params.id);
}) })
app.webServer.xapp.use(this.route, router); app.webServer.xapp.use(this.route, router);
+8 -2
View File
@@ -14,7 +14,7 @@ class GamesController{
*/ */
init(app){ init(app){
const router = express.Router(); const router = express.Router();
const { game } = app; const { game, am } = app;
/** /**
* API: PUT /api/game/ Create or update game, създаване/обновяване на игрова дефиниция * API: PUT /api/game/ Create or update game, създаване/обновяване на игрова дефиниция
@@ -24,11 +24,14 @@ class GamesController{
router.put('/', async (req, res)=>{ router.put('/', async (req, res)=>{
try{ try{
let data = req.body; let data = req.body;
let object = await game[data.id? 'update' : 'create'](req, data) let action = data.id ? 'update' : 'create';
let object = await game[action](req, data)
res.json({status: 'OK', object}); res.json({status: 'OK', object});
am.audit(req, `game-${action}`, object.id);
}catch(err){ }catch(err){
console.error(err); console.error(err);
res.status(500).json({status: 'ERR', err}); res.status(500).json({status: 'ERR', err});
am.audit(req, `game-alter-error`, req.body?.id, {q: req.body, e: err});
} }
}); });
@@ -41,6 +44,7 @@ class GamesController{
router.post('/', async (req, res)=>{ router.post('/', async (req, res)=>{
let result = await game.list(req.body); let result = await game.list(req.body);
res.json(result); res.json(result);
am.audit(req, `game-list`, null, {q: req.body});
}) })
/** /**
@@ -53,6 +57,7 @@ class GamesController{
router.get('/:id', async (req, res)=>{ router.get('/:id', async (req, res)=>{
let object = await game.read(parseInt(req.params.id)); let object = await game.read(parseInt(req.params.id));
res.json(object); res.json(object);
am.audit(req, `game-read`, object.id);
}) })
/** /**
@@ -64,6 +69,7 @@ class GamesController{
router.delete('/:id', async (req, res)=>{ router.delete('/:id', async (req, res)=>{
await game.remove(req.params.id); await game.remove(req.params.id);
res.json({status: 'OK'}); res.json({status: 'OK'});
am.audit(req, `game-delete`, req.params.id);
}) })
app.webServer.xapp.use(this.route, router); app.webServer.xapp.use(this.route, router);
@@ -14,7 +14,7 @@ class ScenariosController{
*/ */
init(app){ init(app){
const router = express.Router(); const router = express.Router();
const { scenario } = app; const { scenario, am } = app;
/** /**
* API: PUT /api/scenario/ Create or update scenario, създаване/обновяване на игрови сценарий * API: PUT /api/scenario/ Create or update scenario, създаване/обновяване на игрови сценарий
@@ -24,11 +24,14 @@ class ScenariosController{
router.put('/', async (req, res)=>{ router.put('/', async (req, res)=>{
try{ try{
let data = req.body; let data = req.body;
let object = await scenario[data.id? 'update' : 'create'](req, data) let action = data.id ? 'update' : 'create';
let object = await scenario[action](req, data)
res.json({status: 'OK', object}); res.json({status: 'OK', object});
am.audit(req, `scenario-${action}`, object.id);
}catch(err){ }catch(err){
console.error(err); console.error(err);
res.status(500).json({status: 'ERR', err}); res.status(500).json({status: 'ERR', err});
am.audit(req, `scenario-alter-error`, req.body?.id, {q: req.body, e: err});
} }
}); });
@@ -41,6 +44,7 @@ class ScenariosController{
router.post('/', async (req, res)=>{ router.post('/', async (req, res)=>{
let result = await scenario.list(req.body); let result = await scenario.list(req.body);
res.json(result); res.json(result);
am.audit(req, `scenario-list`, null, {q: req.body});
}) })
/** /**
@@ -53,6 +57,7 @@ class ScenariosController{
router.get('/:id', async (req, res)=>{ router.get('/:id', async (req, res)=>{
let object = await scenario.read(parseInt(req.params.id)); let object = await scenario.read(parseInt(req.params.id));
res.json(object); res.json(object);
am.audit(req, `scenario-read`, object.id);
}) })
/** /**
@@ -64,6 +69,7 @@ class ScenariosController{
router.delete('/:id', async (req, res)=>{ router.delete('/:id', async (req, res)=>{
await scenario.remove(req.params.id); await scenario.remove(req.params.id);
res.json({status: 'OK'}); res.json({status: 'OK'});
am.audit(req, `scenario-delete`, req.params.id);
}) })
app.webServer.xapp.use(this.route, router); app.webServer.xapp.use(this.route, router);