From 9d1c7bba6f7efcd6ffd0b868657a2b95e5599f4a Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Thu, 19 Oct 2023 21:13:00 +0100 Subject: [PATCH] activity: log account link/unlinks --- api-messages.go | 42 +++++++++++++++++++++++++++++ api-userpage.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ api-users.go | 11 ++++++++ storage.go | 4 ++- 4 files changed, 126 insertions(+), 1 deletion(-) diff --git a/api-messages.go b/api-messages.go index 15e7f45..7520c78 100644 --- a/api-messages.go +++ b/api-messages.go @@ -5,6 +5,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/lithammer/shortuuid/v3" "gopkg.in/ini.v1" ) @@ -677,7 +678,18 @@ func (app *appContext) DiscordConnect(gc *gin.Context) { respondBool(500, false, gc) return } + app.storage.SetDiscordKey(req.JellyfinID, user) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactLinked, + UserID: req.JellyfinID, + SourceType: ActivityAdmin, + Source: gc.GetString("jfId"), + Value: "discord", + Time: time.Now(), + }) + linkExistingOmbiDiscordTelegram(app) respondBool(200, true, gc) } @@ -697,6 +709,16 @@ func (app *appContext) UnlinkDiscord(gc *gin.Context) { return } */ app.storage.DeleteDiscordKey(req.ID) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactUnlinked, + UserID: req.ID, + SourceType: ActivityAdmin, + Source: gc.GetString("jfId"), + Value: "discord", + Time: time.Now(), + }) + respondBool(200, true, gc) } @@ -715,6 +737,16 @@ func (app *appContext) UnlinkTelegram(gc *gin.Context) { return } */ app.storage.DeleteTelegramKey(req.ID) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactUnlinked, + UserID: req.ID, + SourceType: ActivityAdmin, + Source: gc.GetString("jfId"), + Value: "telegram", + Time: time.Now(), + }) + respondBool(200, true, gc) } @@ -733,5 +765,15 @@ func (app *appContext) UnlinkMatrix(gc *gin.Context) { return } */ app.storage.DeleteMatrixKey(req.ID) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactUnlinked, + UserID: req.ID, + SourceType: ActivityAdmin, + Source: gc.GetString("jfId"), + Value: "matrix", + Time: time.Now(), + }) + respondBool(200, true, gc) } diff --git a/api-userpage.go b/api-userpage.go index 5ccb46d..97684ec 100644 --- a/api-userpage.go +++ b/api-userpage.go @@ -208,6 +208,16 @@ func (app *appContext) confirmMyAction(gc *gin.Context, key string) { } emailStore.Addr = claims["email"].(string) app.storage.SetEmailsKey(id, emailStore) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactLinked, + UserID: gc.GetString("jfId"), + SourceType: ActivityUser, + Source: gc.GetString("jfId"), + Value: "email", + Time: time.Now(), + }) + if app.config.Section("ombi").Key("enabled").MustBool(false) { ombiUser, code, err := app.getOmbiUser(id) if code == 200 && err == nil { @@ -360,6 +370,16 @@ func (app *appContext) MyDiscordVerifiedInvite(gc *gin.Context) { dcUser.Contact = existingUser.Contact } app.storage.SetDiscordKey(gc.GetString("jfId"), dcUser) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactLinked, + UserID: gc.GetString("jfId"), + SourceType: ActivityUser, + Source: gc.GetString("jfId"), + Value: "discord", + Time: time.Now(), + }) + respondBool(200, true, gc) } @@ -398,6 +418,16 @@ func (app *appContext) MyTelegramVerifiedInvite(gc *gin.Context) { tgUser.Contact = existingUser.Contact } app.storage.SetTelegramKey(gc.GetString("jfId"), tgUser) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactLinked, + UserID: gc.GetString("jfId"), + SourceType: ActivityUser, + Source: gc.GetString("jfId"), + Value: "telegram", + Time: time.Now(), + }) + respondBool(200, true, gc) } @@ -469,6 +499,16 @@ func (app *appContext) MatrixCheckMyPIN(gc *gin.Context) { } app.storage.SetMatrixKey(gc.GetString("jfId"), mxUser) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactLinked, + UserID: gc.GetString("jfId"), + SourceType: ActivityUser, + Source: gc.GetString("jfId"), + Value: "matrix", + Time: time.Now(), + }) + delete(app.matrix.tokens, pin) respondBool(200, true, gc) } @@ -481,6 +521,16 @@ func (app *appContext) MatrixCheckMyPIN(gc *gin.Context) { // @Tags User Page func (app *appContext) UnlinkMyDiscord(gc *gin.Context) { app.storage.DeleteDiscordKey(gc.GetString("jfId")) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactUnlinked, + UserID: gc.GetString("jfId"), + SourceType: ActivityUser, + Source: gc.GetString("jfId"), + Value: "discord", + Time: time.Now(), + }) + respondBool(200, true, gc) } @@ -492,6 +542,16 @@ func (app *appContext) UnlinkMyDiscord(gc *gin.Context) { // @Tags User Page func (app *appContext) UnlinkMyTelegram(gc *gin.Context) { app.storage.DeleteTelegramKey(gc.GetString("jfId")) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactUnlinked, + UserID: gc.GetString("jfId"), + SourceType: ActivityUser, + Source: gc.GetString("jfId"), + Value: "telegram", + Time: time.Now(), + }) + respondBool(200, true, gc) } @@ -503,6 +563,16 @@ func (app *appContext) UnlinkMyTelegram(gc *gin.Context) { // @Tags User Page func (app *appContext) UnlinkMyMatrix(gc *gin.Context) { app.storage.DeleteMatrixKey(gc.GetString("jfId")) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactUnlinked, + UserID: gc.GetString("jfId"), + SourceType: ActivityUser, + Source: gc.GetString("jfId"), + Value: "matrix", + Time: time.Now(), + }) + respondBool(200, true, gc) } diff --git a/api-users.go b/api-users.go index 0d2eb06..fd0b5a7 100644 --- a/api-users.go +++ b/api-users.go @@ -381,6 +381,7 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc if app.storage.deprecatedDiscord == nil { app.storage.deprecatedDiscord = discordStore{} } + // Note we don't log an activity here, since it's part of creating a user. app.storage.SetDiscordKey(user.ID, discordUser) delete(app.discord.verifiedTokens, req.DiscordPIN) } @@ -1149,6 +1150,16 @@ func (app *appContext) ModifyEmails(gc *gin.Context) { emailStore.Addr = address app.storage.SetEmailsKey(id, emailStore) + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityContactLinked, + UserID: id, + SourceType: ActivityAdmin, + Source: gc.GetString("jfId"), + Value: "email", + Time: time.Now(), + }) + if ombiEnabled { ombiUser, code, err := app.getOmbiUser(id) if code == 200 && err == nil { diff --git a/storage.go b/storage.go index 0f00db0..39f31c6 100644 --- a/storage.go +++ b/storage.go @@ -28,7 +28,8 @@ const ( ActivityDeletion ActivityDisabled ActivityEnabled - ActivityLinked + ActivityContactLinked + ActivityContactUnlinked ActivityChangePassword ActivityResetPassword ActivityCreateInvite @@ -50,6 +51,7 @@ type Activity struct { SourceType ActivitySource Source string InviteCode string // Only set for ActivityCreation + Value string // Used for ActivityContactLinked, "email/discord/telegram/matrix" Time time.Time }