From 0c0c1c8ab2dae63d3e17b608e52f20512b152d13 Mon Sep 17 00:00:00 2001 From: fatemeghasemi Date: Mon, 9 May 2022 11:04:30 +0430 Subject: [PATCH] Add log to admin panel Relate to #5 --- src/entities/log.ts | 25 ++-- src/routes/v1/adminBroRouter.ts | 252 +++++++++++++++++++++++++++++--- src/server.ts | 8 +- 3 files changed, 249 insertions(+), 36 deletions(-) diff --git a/src/entities/log.ts b/src/entities/log.ts index 95d648e..52cb211 100644 --- a/src/entities/log.ts +++ b/src/entities/log.ts @@ -20,33 +20,32 @@ export const LogStatus = { export class Log extends BaseEntity { @PrimaryGeneratedColumn() readonly id: number; + @Column() + trackId: string; + @Column() + status: string; + @CreateDateColumn() + createdAt: Date; @Column() url: string; - @Column() - method: string; + @Column('text', { nullable: true }) scope?: string; + + @UpdateDateColumn() + updatedAt: Date; + @Column() - status: string; + method: string; @Column('text', { nullable: true }) ip?: string; @Column('integer', { nullable: true }) statusCode?: number; @Column('text', { nullable: true }) error?: string; - @Column() - trackId: string; @Column('text', { nullable: true }) result?: string; - // @CreateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP(1)" }) - // createdAt: Date; - // @UpdateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP(1)", onUpdate: "CURRENT_TIMESTAMP(1)" }) - // updatedAt: Date; - @CreateDateColumn() - createdAt: Date; - @UpdateDateColumn() - updatedAt: Date; @Index() @ManyToOne(_type => Application) application?: Application; diff --git a/src/routes/v1/adminBroRouter.ts b/src/routes/v1/adminBroRouter.ts index 33931a9..c10304c 100644 --- a/src/routes/v1/adminBroRouter.ts +++ b/src/routes/v1/adminBroRouter.ts @@ -1,22 +1,25 @@ import { redis } from '../../services/redis'; import { Admin, AdminRole } from '../../entities/admin'; // eslint:disable-next-line:no-var-requires -const AdminJS =require('adminjs'); +const AdminJS = require('adminjs'); // eslint:disable-next-line:no-var-requires -const bcrypt = require('bcrypt'); +const bcrypt = require('bcrypt'); // eslint:disable-next-line:no-var-requires const session = require('express-session'); -const secret = process.env.ADMIN_BRO_COOKIE_SECRET || "test_secret"; +const secret = process.env.ADMIN_BRO_COOKIE_SECRET || 'test_secret'; // eslint:disable-next-line:no-var-requires -const AdminJSExpress = require( '@adminjs/express'); +const AdminJSExpress = require('@adminjs/express'); import { Database, Resource } from '@adminjs/typeorm'; import { findAdminByEmail } from '../../repositories/adminRepository'; import { logger } from '../../utils/logger'; import { Application } from '../../entities/application'; import { Organization } from '../../entities/organization'; +import { Log } from '../../entities/log'; +import { AccessToken } from '../../entities/accessToken'; // eslint:disable-next-line:no-var-requires const RedisStore = require('connect-redis')(session); + interface AdminBroContextInterface { h: any; resource: any; @@ -73,9 +76,6 @@ export const getAdminBroRouter = async () => { }; - - - const getAdminBroInstance = async () => { return new AdminJS({ branding: { @@ -116,7 +116,7 @@ const getAdminBroInstance = async () => { isVisible: true, }, secret: { - isVisible: true + isVisible: true, }, scopes: { isVisible: true, @@ -135,7 +135,7 @@ const getAdminBroInstance = async () => { edit: false, new: false, }, - } + }, }, actions: { bulkDelete: { @@ -146,7 +146,7 @@ const getAdminBroInstance = async () => { }, delete: { isVisible: false, - } + }, }, }, }, @@ -187,7 +187,7 @@ const getAdminBroInstance = async () => { edit: true, new: true, }, - } + }, }, actions: { bulkDelete: { @@ -198,7 +198,157 @@ const getAdminBroInstance = async () => { }, delete: { isVisible: false, + }, + }, + }, + }, + { + resource: Log, + options: { + properties: { + id: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + + + updatedAt: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + createdAt: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + url: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + method: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + status: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + ip: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + scope: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + statusCode: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + error: { + isVisible: { + list: true, + filter: false, + show: true, + edit: false, + new: false, + }, + }, + trackId: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + result: { + isVisible: { + list: false, + filter: false, + show: true, + edit: false, + new: false, + }, + }, + accessTokenId: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + applicationId: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, } + + }, + actions: { + bulkDelete: { + isVisible: false, + }, + edit: { + isVisible: false, + }, + delete: { + isVisible: false, + }, + new: { + isVisible: false, + }, }, }, }, @@ -216,7 +366,7 @@ const getAdminBroInstance = async () => { }, }, encryptedPassword: { - isVisible: false + isVisible: false, }, password: { type: 'string', @@ -225,7 +375,7 @@ const getAdminBroInstance = async () => { edit: true, filter: false, show: false, - } + }, }, firstName: { isVisible: { @@ -262,7 +412,7 @@ const getAdminBroInstance = async () => { edit: true, new: true, }, - } + }, }, actions: { delete: { @@ -272,7 +422,7 @@ const getAdminBroInstance = async () => { isVisible: false, }, new: { - isAccessible: (params:{ currentAdmin:Admin }) => + isAccessible: (params: { currentAdmin: Admin }) => params.currentAdmin && params.currentAdmin.role === AdminRole.SUPER_ADMIN, before: async ( request: AdminBroRequestInterface, @@ -293,10 +443,10 @@ const getAdminBroInstance = async () => { }, }, edit: { - isAccessible: (params:{ currentAdmin:Admin }) => + isAccessible: (params: { currentAdmin: Admin }) => params.currentAdmin && params.currentAdmin.role === AdminRole.SUPER_ADMIN, - before: async ( request: AdminBroRequestInterface, - context: AdminBroContextInterface,) => { + before: async (request: AdminBroRequestInterface, + context: AdminBroContextInterface) => { logger.debug({ request: request.payload }); if (request.payload.password) { const bc = await bcrypt.hash( @@ -316,12 +466,74 @@ const getAdminBroInstance = async () => { }, }, + { + resource: AccessToken, + options: { + actions: { + bulkDelete: { + isVisible: false, + }, + edit: { + isVisible: false, + }, + delete: { + isVisible: false, + }, + new: { + isVisible: false, + }, + }, + properties: { + id: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + }, + expirationDate: { + isVisible:false + }, + jwt: { + isVisible:false + }, + jti: { + isVisible:false + }, + scopes: { + isVisible: { + list: false, + filter: false, + show: true, + edit: false, + new: false, + }, + }, + isActive: { + isVisible: true + }, + applicationId: { + isVisible: { + list: true, + filter: true, + show: true, + edit: false, + new: false, + }, + } + + }, + + + }, + + } ], rootPath: adminJsRootPath, }); }; - - export const adminJsRootPath = '/admin'; diff --git a/src/server.ts b/src/server.ts index 6adfa97..cea985a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -23,10 +23,7 @@ export const initServer = async () => { app.use(express.static('public')); app.use(bodyParser.json()); - app.use(addLog); app.use(adminJsRootPath, await getAdminBroRouter()); - - app.use(v1Router); app.use( '/docs', swaggerUi.serve, @@ -36,6 +33,11 @@ export const initServer = async () => { }, }), ); + app.use(addLog); + + + app.use(v1Router); + app.use(errorHandler); const port = process.env.PORT || 3040; app.listen(port, () => {