This file contains hidden or 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
| Time | UserId | Resource | Action | Status | Data | |
|---|---|---|---|---|---|---|
| 2019-06-01 10:00:00 | 4000 | Users | Read | Success | { json } | |
| 2019-06-01 10:05:00 | 4000 | Users | Add | Success | { json } | |
| 2019-06-01 10:06:00 | 4000 | Todos | Read | Error | { json } | |
| 2019-06-01 10:07:00 | 4000 | Todos | Modify | Success | { json } |
This file contains hidden or 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
| export class UsersController { | |
| // --------- other methods removed for clarity | |
| @ownerOrAdmin() | |
| @route.put(":id") | |
| async modify(id: number, data: User, @bind.user() user:LoginUser) { | |
| const audit = <Audit>{ | |
| userId: user.userId, | |
| action: "Modify", | |
| resource: "Users", |
This file contains hidden or 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
| export class UsersController { | |
| // --------- other methods removed for clarity | |
| @ownerOrAdmin() | |
| @route.put(":id") | |
| async modify(id: number, data: User) { | |
| const password = await bcrypt.hash(data.password, 10) | |
| return db("User").update({ ...data, password }).where({ id }) | |
| } | |
| } |
This file contains hidden or 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
| export class UsersController { | |
| // ----- other methods removed for clarity | |
| @ownerOrAdmin() | |
| @route.delete(":id") | |
| async delete(id: number, @bind.user() user: LoginUser) { | |
| const audit = <Audit>{ | |
| userId: user.userId, | |
| action: "Delete", | |
| resource: "Users", |
This file contains hidden or 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
| export class UserActivityMiddleware implements Middleware { | |
| async execute(next: Readonly<Invocation>): Promise<ActionResult> { | |
| if (next.context.route && next.context.state.user) { | |
| const audit = createAudit(next.context) | |
| try { | |
| const result = await next.proceed() | |
| await db("Audit").insert(<Audit>{...audit, status: "Success"}) | |
| return result | |
| } | |
| catch (e) { |
This file contains hidden or 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
| const AuditActionMap = new Map([ | |
| ["get", "Read"], | |
| ["post", "Add"], | |
| ["put", "Modify"], | |
| ["delete", "Delete"] | |
| ]) | |
| function createAudit(context: Context) { | |
| const { route, state, method } = context | |
| const controller = route!.controller.name |
This file contains hidden or 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
| const CensorshipMap = new Map([ | |
| [User, (x: User) => (<User>{ ...x, password: "*****", email: "*****" })] | |
| ]) | |
| function censor(context: Context) { | |
| const parameters = context.parameters || [] | |
| const types = context.route!.action.parameters.map(x => x.type) | |
| return parameters.map((x, i) => { | |
| const fn = CensorshipMap.get(types[i]) |
This file contains hidden or 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
| interface User extends Document { | |
| email: string, | |
| name: string, | |
| birthDate: Date | |
| } |
This file contains hidden or 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
| const userValidator = joi.object({ | |
| email: joi.string().email(), | |
| name: joi.string(), | |
| birthDate: joi.date() | |
| }) |
This file contains hidden or 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
| const UserModel = mongoose.model<User>("User", new Schema({ | |
| email: String, | |
| name: String, | |
| birthDate: Date | |
| })) |