mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:17:32 +00:00
fix: get orgID when missing on trigger logs (#7555)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user