-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from Tietokilta/feature/audit-logging
Add audit logging
- Loading branch information
Showing
45 changed files
with
927 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/ilmomasiina-backend/src/cron/deleteOldAuditLogs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import moment from 'moment'; | ||
import { Op, WhereOptions } from 'sequelize'; | ||
|
||
import config from '../config'; | ||
import { AuditLog } from '../models/auditlog'; | ||
|
||
export default async function deleteOldAuditLogs() { | ||
await AuditLog.unscoped().destroy({ | ||
where: { | ||
createdAt: { | ||
[Op.lt]: moment().subtract(config.anonymizeAfterDays, 'days').toDate(), | ||
}, | ||
} as WhereOptions, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { | ||
DataTypes, Model, Optional, Sequelize, | ||
} from 'sequelize'; | ||
|
||
import AuditLogAttributes from '@tietokilta/ilmomasiina-models/src/models/auditlog'; | ||
import { RANDOM_ID_LENGTH } from './randomId'; | ||
|
||
export interface AuditLogCreationAttributes extends Optional<AuditLogAttributes, 'id'> {} | ||
|
||
export class AuditLog extends Model<AuditLogAttributes, AuditLogCreationAttributes> implements AuditLogAttributes { | ||
public id!: number; | ||
public user!: string | null; | ||
public ipAddress!: string; | ||
public action!: string; | ||
public eventId!: string | null; | ||
public eventName!: string | null; | ||
public signupId!: string | null; | ||
public signupName!: string | null; | ||
public extra!: string; | ||
|
||
public readonly createdAt!: Date; | ||
public readonly updatedAt!: Date; | ||
} | ||
|
||
export default function setupAuditLogModel(sequelize: Sequelize) { | ||
AuditLog.init( | ||
{ | ||
id: { | ||
type: DataTypes.INTEGER.UNSIGNED, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
}, | ||
user: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
ipAddress: { | ||
type: DataTypes.STRING(64), | ||
allowNull: false, | ||
}, | ||
action: { | ||
type: DataTypes.STRING(32), | ||
allowNull: false, | ||
}, | ||
eventId: { | ||
type: DataTypes.CHAR(RANDOM_ID_LENGTH), | ||
allowNull: true, | ||
}, | ||
eventName: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
signupId: { | ||
type: DataTypes.CHAR(RANDOM_ID_LENGTH), | ||
allowNull: true, | ||
}, | ||
signupName: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
extra: { | ||
type: DataTypes.TEXT, | ||
allowNull: true, | ||
}, | ||
}, | ||
{ | ||
sequelize, | ||
modelName: 'auditlog', | ||
freezeTableName: true, | ||
}, | ||
); | ||
|
||
return AuditLog; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/ilmomasiina-backend/src/models/migrations/0001-add-audit-logs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { DataTypes, Sequelize } from 'sequelize'; | ||
import { RunnableMigration } from 'umzug'; | ||
|
||
// Constant from ../randomId | ||
const RANDOM_ID_LENGTH = 12; | ||
|
||
const migration: RunnableMigration<Sequelize> = { | ||
name: '0001-add-audit-logs', | ||
async up({ context: sequelize }) { | ||
const query = sequelize.getQueryInterface(); | ||
await query.createTable( | ||
'auditlog', | ||
{ | ||
id: { | ||
type: DataTypes.INTEGER.UNSIGNED, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
}, | ||
user: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
ipAddress: { | ||
type: DataTypes.STRING(64), | ||
allowNull: false, | ||
}, | ||
action: { | ||
type: DataTypes.STRING(32), | ||
allowNull: false, | ||
}, | ||
eventId: { | ||
type: DataTypes.CHAR(RANDOM_ID_LENGTH), | ||
allowNull: true, | ||
}, | ||
eventName: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
signupId: { | ||
type: DataTypes.CHAR(RANDOM_ID_LENGTH), | ||
allowNull: true, | ||
}, | ||
signupName: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
extra: { | ||
type: DataTypes.TEXT, | ||
allowNull: true, | ||
}, | ||
createdAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
}, | ||
updatedAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
}, | ||
}, | ||
); | ||
}, | ||
}; | ||
|
||
export default migration; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.