mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
2bd255106a
* fix: org tests * fix: org tests * fix: user grant test * fix: user grant test * fix: project and project role test * fix: project grant test * fix: project grant test * fix: project member, grant member, app changed tests * fix: application tests * fix: application tests * fix: add oidc app test * fix: add oidc app test * fix: add api keys test * fix: iam policies * fix: iam and org member tests * fix: clock skew validation * revert crypto changes * fix: tests * fix project grant member commands Co-authored-by: Livio Amstutz <livio.a@gmail.com>
120 lines
2.9 KiB
Go
120 lines
2.9 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"github.com/caos/zitadel/internal/domain"
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
"github.com/caos/zitadel/internal/repository/project"
|
|
)
|
|
|
|
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 {
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, project.AggregateType).
|
|
AggregateIDs(wm.AggregateID).
|
|
ResourceOwner(wm.ResourceOwner).
|
|
EventTypes(
|
|
project.RoleAddedType,
|
|
project.RoleChangedType,
|
|
project.RoleRemovedType,
|
|
project.ProjectRemovedType)
|
|
}
|
|
|
|
func (wm *ProjectRoleWriteModel) NewProjectRoleChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
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
|
|
}
|
|
changeEvent, err := project.NewRoleChangedEvent(ctx, aggregate, key, changes)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return changeEvent, true, nil
|
|
}
|