|
| 1 | +package eventlog |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | +) |
| 6 | + |
| 7 | +func Meta(timestamp time.Time, userId string) EventMeta { |
| 8 | + return EventMeta{ |
| 9 | + Timestamp: timestamp.UTC(), |
| 10 | + UserId: userId, |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +func MetaSystemUser(timestamp time.Time) EventMeta { |
| 15 | + return EventMeta{ |
| 16 | + Timestamp: timestamp.UTC(), |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func MetaWithImpersonator(timestamp time.Time, userId string, impersonatingUserId string) EventMeta { |
| 21 | + if impersonatingUserId == userId { |
| 22 | + // save some space, since this can be deduced |
| 23 | + impersonatingUserId = "" |
| 24 | + } |
| 25 | + |
| 26 | + return EventMeta{ |
| 27 | + Timestamp: timestamp.UTC(), |
| 28 | + UserId: userId, |
| 29 | + ImpersonatingUserId: impersonatingUserId, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type Allocators map[string]func() Event |
| 34 | + |
| 35 | +// event has type (event's name) and metadata (common attributes shared by all events) |
| 36 | +type Event interface { |
| 37 | + MetaType() string |
| 38 | + Meta() *EventMeta |
| 39 | +} |
| 40 | + |
| 41 | +type EventMeta struct { |
| 42 | + Timestamp time.Time |
| 43 | + TimestampOfRecording time.Time |
| 44 | + // whose resource was acted upon? |
| 45 | + // - user changes her password => target=<self> and actor=<self> (though actor omitted then) |
| 46 | + // - when admin changes another users password => target=<someone> and actor=<admin> |
| 47 | + UserId string |
| 48 | + // "who done this"? most times user acts on her own resources, but sometimes |
| 49 | + // e.g. a super user can do stuff to another user's (the target) resources |
| 50 | + ImpersonatingUserId string |
| 51 | +} |
| 52 | + |
| 53 | +// returns "who acted on someones resource" (most times same as user id, but could be impersonator) |
| 54 | +func (m *EventMeta) ActingUserOrDefaultToTarget() string { |
| 55 | + if m.ImpersonatingUserId != "" { |
| 56 | + return m.ImpersonatingUserId |
| 57 | + } else { |
| 58 | + // no special "actor" recorded => user actioned her own resource |
| 59 | + return m.UserId |
| 60 | + } |
| 61 | +} |
0 commit comments