zitadel/internal/v2/command/user_grant_model.go
Fabi 320679467b
feat: User login commands (#1228)
* feat: change login to command side

* feat: change login to command side

* fix: fix push on user

* feat: user command side

* feat: sign out

* feat: command side login

* feat: command side login

* feat: fix register user

* feat: fix register user

* feat: fix web auth n events

* feat: add machine keys

* feat: send codes

* feat: move authrequest to domain

* feat: move authrequest to domain

* feat: webauthn working

* feat: external users

* feat: external users login

* feat: notify users

* fix: tests

* feat: cascade remove user grants on project remove

* fix: webauthn

* fix: pr requests

* fix: register human with member

* fix: fix bugs

* fix: fix bugs
2021-02-08 11:30:30 +01:00

78 lines
2.2 KiB
Go

package command
import (
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/domain"
"github.com/caos/zitadel/internal/v2/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) AppendEvents(events ...eventstore.EventReader) {
wm.WriteModel.AppendEvents(events...)
}
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)
if wm.ResourceOwner != "" {
query.ResourceOwner(wm.ResourceOwner)
}
return query
}
func UserGrantAggregateFromWriteModel(wm *eventstore.WriteModel) *usergrant.Aggregate {
return &usergrant.Aggregate{
Aggregate: *eventstore.AggregateFromWriteModel(wm, usergrant.AggregateType, usergrant.AggregateVersion),
}
}