From ef2f3e51ea1cbdea2c67a9f8edabecd59de39751 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Thu, 16 Jan 2025 18:14:43 +0100 Subject: [PATCH] EventManager: add more datetime placeholders Signed-off-by: Nicola Murino --- internal/common/eventmanager.go | 11 +++++++++++ internal/common/eventmanager_test.go | 4 ++++ static/locales/en/translation.json | 5 +++++ static/locales/it/translation.json | 5 +++++ templates/webadmin/eventaction.html | 15 +++++++++++++++ 5 files changed, 40 insertions(+) diff --git a/internal/common/eventmanager.go b/internal/common/eventmanager.go index 761a0a06..b4b566f6 100644 --- a/internal/common/eventmanager.go +++ b/internal/common/eventmanager.go @@ -789,6 +789,12 @@ func (p *EventParams) getStringReplacements(addObjectData, jsonEscaped bool) []s } else { dateTimeString = p.Timestamp.UTC().Format(dateTimeMillisFormat) } + year := dateTimeString[0:4] + month := dateTimeString[5:7] + day := dateTimeString[8:10] + hour := dateTimeString[11:13] + minute := dateTimeString[14:16] + replacements := []string{ "{{Name}}", p.getStringReplacement(p.Name, jsonEscaped), "{{Event}}", p.Event, @@ -809,6 +815,11 @@ func (p *EventParams) getStringReplacements(addObjectData, jsonEscaped bool) []s "{{Email}}", p.getStringReplacement(p.Email, jsonEscaped), "{{Timestamp}}", strconv.FormatInt(p.Timestamp.UnixNano(), 10), "{{DateTime}}", dateTimeString, + "{{Year}}", year, + "{{Month}}", month, + "{{Day}}", day, + "{{Hour}}", hour, + "{{Minute}}", minute, "{{StatusString}}", p.getStatusString(), "{{UID}}", p.getStringReplacement(p.UID, jsonEscaped), "{{Ext}}", p.getStringReplacement(p.Extension, jsonEscaped), diff --git a/internal/common/eventmanager_test.go b/internal/common/eventmanager_test.go index 4f352f66..fc4c9df3 100644 --- a/internal/common/eventmanager_test.go +++ b/internal/common/eventmanager_test.go @@ -812,12 +812,16 @@ func TestDateTimePlaceholder(t *testing.T) { r := strings.NewReplacer(replacements...) res := r.Replace("{{DateTime}}") assert.Equal(t, dateTime.UTC().Format(dateTimeMillisFormat), res) + res = r.Replace("{{Year}}-{{Month}}-{{Day}}T{{Hour}}:{{Minute}}") + assert.Equal(t, dateTime.UTC().Format(dateTimeMillisFormat)[:16], res) Config.TZ = "local" replacements = params.getStringReplacements(false, false) r = strings.NewReplacer(replacements...) res = r.Replace("{{DateTime}}") assert.Equal(t, dateTime.Local().Format(dateTimeMillisFormat), res) + res = r.Replace("{{Year}}-{{Month}}-{{Day}}T{{Hour}}:{{Minute}}") + assert.Equal(t, dateTime.Local().Format(dateTimeMillisFormat)[:16], res) Config.TZ = oldTZ } diff --git a/static/locales/en/translation.json b/static/locales/en/translation.json index 7d548ba1..d45605af 100644 --- a/static/locales/en/translation.json +++ b/static/locales/en/translation.json @@ -1078,6 +1078,11 @@ "role": "User or admin role", "timestamp": "Event timestamp as nanoseconds since epoch", "datetime": "Event timestamp formatted as YYYY-MM-DDTHH:MM:SS.ZZZ", + "year": "Event year formatted as four digits", + "month": "Event month formatted as two digits", + "day": "Event day formatted as two digits", + "hour": "Event hour formatted as two digits", + "minute": "Event minute formatted as two digits", "email": "For filesystem events, this is the email associated with the user performing the action. For the provider events, this is the email associated with the affected user or admin. Blank in all other cases", "object_data": "Provider object data serialized as JSON with sensitive fields removed", "object_data_string": "Provider object data as JSON escaped string with sensitive fields removed", diff --git a/static/locales/it/translation.json b/static/locales/it/translation.json index 7abd7d00..898a0f26 100644 --- a/static/locales/it/translation.json +++ b/static/locales/it/translation.json @@ -1078,6 +1078,11 @@ "role": "Ruolo dell'utente o dell'amministratore", "timestamp": "Timestamp dell'evento in nanosecondi dall'epoch time", "datetime": "Timestamp dell'evento formattato come YYYY-MM-DDTHH:MM:SS.ZZZ", + "year": "Anno dell'evento formattato a quattro cifre", + "month": "Mese dell'evento formattato a due cifre", + "day": "Giorno dell'evento formattato a due cifre", + "hour": "Ora dell'evento formattata a due cifre", + "minute": "Minuto dell'evento formattato a due cifre", "email": "Per gli eventi del file system, questa รจ l'e-mail associata all'utente che esegue l'azione. Per gli eventi del provider, si tratta dell'e-mail associata all'utente o all'amministratore interessato. Vuoto in tutti gli altri casi", "object_data": "Dati dell'oggetto provider serializzati come JSON con campi sensibili rimossi", "object_data_string": "Dati dell'oggetto provider serializzati come stringa JSON escaped con campi sensibili rimossi", diff --git a/templates/webadmin/eventaction.html b/templates/webadmin/eventaction.html index d322049b..01a7b33e 100644 --- a/templates/webadmin/eventaction.html +++ b/templates/webadmin/eventaction.html @@ -960,6 +960,21 @@ explicit grant from the SFTPGo Team (support@sftpgo.com).

{{`{{DateTime}}`}} => Timestamp formatted as YYYY-MM-DDTHH:MM:SS.ZZZ.

+

+ {{`{{Year}}`}} => Event year formatted as four digits. +

+

+ {{`{{Month}}`}} => Event month formatted as two digits. +

+

+ {{`{{Day}}`}} => Event day formatted as two digits. +

+

+ {{`{{Hour}}`}} => Event hour formatted as two digits. +

+

+ {{`{{Minute}}`}} => Event minute formatted as two digits. +

{{`{{Email}}`}} => For filesystem events, this is the email associated with the user performing the action. For the provider events, this is the email associated with the affected user or admin. Blank in all other cases.