fix: get orgID when missing on trigger logs (#7555)

This commit is contained in:
Livio Spring
2024-03-14 09:49:10 +01:00
committed by GitHub
parent bcff220cb4
commit 252e59d5cd
4 changed files with 50 additions and 10 deletions

View File

@@ -10,6 +10,8 @@ import (
"github.com/zitadel/zitadel/internal/api/authz"
http_utils "github.com/zitadel/zitadel/internal/api/http"
"github.com/zitadel/zitadel/internal/api/info"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/repository/user"
)
const (
@@ -50,7 +52,10 @@ func (t TriggerMethod) String() string {
}
// Trigger is used to log a specific events for a user (e.g. session or oidc token creation)
func Trigger(ctx context.Context, orgID, userID string, trigger TriggerMethod) {
func Trigger(ctx context.Context, orgID, userID string, trigger TriggerMethod, reducer func(ctx context.Context, r eventstore.QueryReducer) error) {
if orgID == "" && userID != "" {
orgID = getOrgOfUser(ctx, userID, reducer)
}
ai := info.ActivityInfoFromContext(ctx)
triggerLog(
authz.GetInstance(ctx).InstanceID(),
@@ -99,3 +104,38 @@ func triggerLog(instanceID, orgID, userID, domain string, trigger TriggerMethod,
"isSystemUser", isSystemUser,
).Info(Activity)
}
func getOrgOfUser(ctx context.Context, userID string, reducer func(ctx context.Context, r eventstore.QueryReducer) error) string {
org := &orgIDOfUser{userID: userID}
err := reducer(ctx, org)
if err != nil {
logging.WithError(err).Error("could not get org id of user for trigger log")
return ""
}
return org.orgID
}
type orgIDOfUser struct {
eventstore.WriteModel
userID string
orgID string
}
func (u *orgIDOfUser) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
OrderDesc().
Limit(1).
AddQuery().
AggregateTypes(user.AggregateType).
AggregateIDs(u.userID).
Builder()
}
func (u *orgIDOfUser) Reduce() error {
if len(u.Events) == 0 {
return nil
}
u.orgID = u.Events[0].Aggregate().ResourceOwner
return nil
}