2021-01-28 05:35:26 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/project"
|
2021-01-28 05:35:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectRoleWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Key string
|
|
|
|
DisplayName string
|
|
|
|
Group string
|
|
|
|
State domain.ProjectRoleState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectRoleWriteModelWithKey(key, projectID, resourceOwner string) *ProjectRoleWriteModel {
|
|
|
|
return &ProjectRoleWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: projectID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
Key: key,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectRoleWriteModel(projectID, resourceOwner string) *ProjectRoleWriteModel {
|
|
|
|
return &ProjectRoleWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: projectID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectRoleWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.RoleAddedEvent:
|
|
|
|
if e.Key == wm.Key {
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
case *project.RoleChangedEvent:
|
|
|
|
if e.Key == wm.Key {
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
case *project.RoleRemovedEvent:
|
|
|
|
if e.Key == wm.Key {
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
case *project.ProjectRemovedEvent:
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectRoleWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.RoleAddedEvent:
|
|
|
|
wm.Key = e.Key
|
|
|
|
wm.DisplayName = e.DisplayName
|
|
|
|
wm.Group = e.Group
|
|
|
|
wm.State = domain.ProjectRoleStateActive
|
|
|
|
case *project.RoleChangedEvent:
|
|
|
|
wm.Key = e.Key
|
|
|
|
if e.DisplayName != nil {
|
|
|
|
wm.DisplayName = *e.DisplayName
|
|
|
|
}
|
|
|
|
if e.Group != nil {
|
|
|
|
wm.Group = *e.Group
|
|
|
|
}
|
|
|
|
case *project.RoleRemovedEvent:
|
|
|
|
wm.State = domain.ProjectRoleStateRemoved
|
|
|
|
case *project.ProjectRemovedEvent:
|
|
|
|
wm.State = domain.ProjectRoleStateRemoved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectRoleWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
|
|
|
project.RoleAddedType,
|
|
|
|
project.RoleChangedType,
|
|
|
|
project.RoleRemovedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
project.ProjectRemovedType).
|
|
|
|
Builder()
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectRoleWriteModel) NewProjectRoleChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
key,
|
|
|
|
displayName,
|
|
|
|
group string,
|
|
|
|
) (*project.RoleChangedEvent, bool, error) {
|
|
|
|
changes := make([]project.RoleChanges, 0)
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if wm.DisplayName != displayName {
|
|
|
|
changes = append(changes, project.ChangeDisplayName(displayName))
|
|
|
|
}
|
|
|
|
if wm.Group != group {
|
|
|
|
changes = append(changes, project.ChangeGroup(group))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2021-03-15 11:51:15 +00:00
|
|
|
changeEvent, err := project.NewRoleChangedEvent(ctx, aggregate, key, changes)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|