zitadel/internal/command/user_grant_model.go
Fabi d8e42744b4
fix: move v2 pkgs (#1331)
* fix: move eventstore pkgs

* fix: move eventstore pkgs

* fix: remove v2 view

* fix: remove v2 view
2021-02-23 15:13:04 +01:00

79 lines
2.3 KiB
Go

package command
import (
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/repository/usergrant"
)
type UserGrantWriteModel struct {
eventstore.WriteModel
UserID string
ProjectID string
ProjectGrantID string
RoleKeys []string
State domain.UserGrantState
}
func NewUserGrantWriteModel(userGrantID string, resourceOwner string) *UserGrantWriteModel {
return &UserGrantWriteModel{
WriteModel: eventstore.WriteModel{
AggregateID: userGrantID,
ResourceOwner: resourceOwner,
},
}
}
func (wm *UserGrantWriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *usergrant.UserGrantAddedEvent:
wm.UserID = e.UserID
wm.ProjectID = e.ProjectID
wm.ProjectGrantID = e.ProjectGrantID
wm.RoleKeys = e.RoleKeys
wm.State = domain.UserGrantStateActive
case *usergrant.UserGrantChangedEvent:
wm.RoleKeys = e.RoleKeys
case *usergrant.UserGrantCascadeChangedEvent:
wm.RoleKeys = e.RoleKeys
case *usergrant.UserGrantDeactivatedEvent:
if wm.State == domain.UserGrantStateRemoved {
continue
}
wm.State = domain.UserGrantStateInactive
case *usergrant.UserGrantReactivatedEvent:
if wm.State == domain.UserGrantStateRemoved {
continue
}
wm.State = domain.UserGrantStateActive
case *usergrant.UserGrantRemovedEvent:
wm.State = domain.UserGrantStateRemoved
case *usergrant.UserGrantCascadeRemovedEvent:
wm.State = domain.UserGrantStateRemoved
}
}
return wm.WriteModel.Reduce()
}
func (wm *UserGrantWriteModel) Query() *eventstore.SearchQueryBuilder {
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, usergrant.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(usergrant.UserGrantAddedType,
usergrant.UserGrantChangedType,
usergrant.UserGrantCascadeChangedType,
usergrant.UserGrantDeactivatedType,
usergrant.UserGrantReactivatedType,
usergrant.UserGrantRemovedType,
usergrant.UserGrantCascadeRemovedType)
if wm.ResourceOwner != "" {
query.ResourceOwner(wm.ResourceOwner)
}
return query
}
func UserGrantAggregateFromWriteModel(wm *eventstore.WriteModel) *eventstore.Aggregate {
return eventstore.AggregateFromWriteModel(wm, usergrant.AggregateType, usergrant.AggregateVersion)
}