2023-01-02 15:11:32 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// EventsHandler HTTP handler
|
|
|
|
type EventsHandler struct {
|
2023-02-03 21:47:20 +01:00
|
|
|
accountManager server.AccountManager
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// NewEventsHandler creates a new EventsHandler HTTP handler
|
|
|
|
func NewEventsHandler(accountManager server.AccountManager, authCfg AuthCfg) *EventsHandler {
|
|
|
|
return &EventsHandler{
|
2023-01-02 15:11:32 +01:00
|
|
|
accountManager: accountManager,
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GetAllEvents list of the given account
|
|
|
|
func (h *EventsHandler) GetAllEvents(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2023-01-02 15:11:32 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accountEvents, err := h.accountManager.GetEvents(account.Id, user.Id)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
|
|
|
return
|
|
|
|
}
|
2023-09-19 18:08:40 +02:00
|
|
|
events := make([]*api.Event, len(accountEvents))
|
|
|
|
for i, e := range accountEvents {
|
|
|
|
events[i] = toEventResponse(e)
|
|
|
|
}
|
|
|
|
|
2023-09-23 10:47:49 +02:00
|
|
|
err = h.fillEventsWithUserInfo(events, account.Id, user.Id)
|
2023-09-19 18:08:40 +02:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
|
|
|
return
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
util.WriteJSONObject(w, events)
|
|
|
|
}
|
|
|
|
|
2023-09-23 10:47:49 +02:00
|
|
|
func (h *EventsHandler) fillEventsWithUserInfo(events []*api.Event, accountId, userId string) error {
|
|
|
|
// build email, name maps based on users
|
2023-09-19 18:08:40 +02:00
|
|
|
userInfos, err := h.accountManager.GetUsersFromAccount(accountId, userId)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get users from account: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
emails := make(map[string]string)
|
2023-09-23 10:47:49 +02:00
|
|
|
names := make(map[string]string)
|
2023-09-19 18:08:40 +02:00
|
|
|
for _, ui := range userInfos {
|
|
|
|
emails[ui.ID] = ui.Email
|
2023-09-23 10:47:49 +02:00
|
|
|
names[ui.ID] = ui.Name
|
2023-09-19 18:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
for _, event := range events {
|
2023-09-23 10:47:49 +02:00
|
|
|
// fill initiator
|
2023-09-19 18:08:40 +02:00
|
|
|
if event.InitiatorEmail == "" {
|
|
|
|
event.InitiatorEmail, ok = emails[event.InitiatorId]
|
|
|
|
if !ok {
|
|
|
|
log.Warnf("failed to resolve email for initiator: %s", event.InitiatorId)
|
|
|
|
}
|
|
|
|
}
|
2023-09-23 10:47:49 +02:00
|
|
|
|
|
|
|
if event.InitiatorName == "" {
|
|
|
|
// here to allowed to be empty because in the first release we did not store the name
|
|
|
|
event.InitiatorName = names[event.InitiatorId]
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill target meta
|
|
|
|
email, ok := emails[event.TargetId]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
event.Meta["email"] = email
|
|
|
|
|
|
|
|
username, ok := names[event.TargetId]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
event.Meta["username"] = username
|
2023-09-19 18:08:40 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
func toEventResponse(event *activity.Event) *api.Event {
|
|
|
|
meta := make(map[string]string)
|
|
|
|
if event.Meta != nil {
|
|
|
|
for s, a := range event.Meta {
|
|
|
|
meta[s] = fmt.Sprintf("%v", a)
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 18:08:40 +02:00
|
|
|
e := &api.Event{
|
|
|
|
Id: fmt.Sprint(event.ID),
|
|
|
|
InitiatorId: event.InitiatorID,
|
2023-09-23 10:47:49 +02:00
|
|
|
InitiatorName: event.InitiatorName,
|
2023-09-19 18:08:40 +02:00
|
|
|
InitiatorEmail: event.InitiatorEmail,
|
|
|
|
Activity: event.Activity.Message(),
|
|
|
|
ActivityCode: api.EventActivityCode(event.Activity.StringCode()),
|
|
|
|
TargetId: event.TargetID,
|
|
|
|
Timestamp: event.Timestamp,
|
|
|
|
Meta: meta,
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
2023-09-19 18:08:40 +02:00
|
|
|
return e
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|