2021-01-22 12:31:52 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/usergrant"
|
2021-01-22 12:31:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
return wm.WriteModel.Reduce()
|
2021-01-22 12:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *UserGrantWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(usergrant.AggregateType).
|
2021-02-18 13:48:27 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(usergrant.UserGrantAddedType,
|
|
|
|
usergrant.UserGrantChangedType,
|
|
|
|
usergrant.UserGrantCascadeChangedType,
|
|
|
|
usergrant.UserGrantDeactivatedType,
|
|
|
|
usergrant.UserGrantReactivatedType,
|
|
|
|
usergrant.UserGrantRemovedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
usergrant.UserGrantCascadeRemovedType).
|
|
|
|
Builder()
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
if wm.ResourceOwner != "" {
|
|
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
|
|
}
|
|
|
|
return query
|
2021-01-22 12:31:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func UserGrantAggregateFromWriteModel(wm *eventstore.WriteModel) *eventstore.Aggregate {
|
|
|
|
return eventstore.AggregateFromWriteModel(wm, usergrant.AggregateType, usergrant.AggregateVersion)
|
2021-01-22 12:31:52 +00:00
|
|
|
}
|
2021-03-15 11:51:15 +00:00
|
|
|
|
|
|
|
type UserGrantPreConditionReadModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
UserID string
|
|
|
|
ProjectID string
|
|
|
|
ProjectGrantID string
|
2022-08-24 09:38:59 +00:00
|
|
|
ResourceOwner string
|
2021-03-15 11:51:15 +00:00
|
|
|
UserExists bool
|
|
|
|
ProjectExists bool
|
|
|
|
ProjectGrantExists bool
|
|
|
|
ExistingRoleKeys []string
|
|
|
|
}
|
|
|
|
|
2022-08-24 09:38:59 +00:00
|
|
|
func NewUserGrantPreConditionReadModel(userID, projectID, projectGrantID, resourceOwner string) *UserGrantPreConditionReadModel {
|
2021-03-15 11:51:15 +00:00
|
|
|
return &UserGrantPreConditionReadModel{
|
|
|
|
UserID: userID,
|
|
|
|
ProjectID: projectID,
|
|
|
|
ProjectGrantID: projectGrantID,
|
2022-08-24 09:38:59 +00:00
|
|
|
ResourceOwner: resourceOwner,
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *UserGrantPreConditionReadModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanAddedEvent:
|
|
|
|
wm.UserExists = true
|
2021-03-25 17:12:24 +00:00
|
|
|
case *user.HumanRegisteredEvent:
|
|
|
|
wm.UserExists = true
|
2021-03-15 11:51:15 +00:00
|
|
|
case *user.MachineAddedEvent:
|
|
|
|
wm.UserExists = true
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserExists = false
|
|
|
|
case *project.ProjectAddedEvent:
|
2022-08-24 09:38:59 +00:00
|
|
|
if wm.ProjectGrantID == "" && wm.ResourceOwner == e.Aggregate().ResourceOwner {
|
|
|
|
wm.ProjectExists = true
|
|
|
|
}
|
2021-03-15 11:51:15 +00:00
|
|
|
case *project.ProjectRemovedEvent:
|
|
|
|
wm.ProjectExists = false
|
|
|
|
case *project.GrantAddedEvent:
|
2022-08-24 09:38:59 +00:00
|
|
|
if wm.ProjectGrantID == e.GrantID && wm.ResourceOwner == e.GrantedOrgID {
|
2021-03-15 11:51:15 +00:00
|
|
|
wm.ProjectGrantExists = true
|
2021-07-01 13:53:52 +00:00
|
|
|
wm.ExistingRoleKeys = e.RoleKeys
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
case *project.GrantChangedEvent:
|
2021-07-01 13:53:52 +00:00
|
|
|
if wm.ProjectGrantID == e.GrantID {
|
|
|
|
wm.ExistingRoleKeys = e.RoleKeys
|
|
|
|
}
|
2021-03-15 11:51:15 +00:00
|
|
|
case *project.GrantRemovedEvent:
|
|
|
|
if wm.ProjectGrantID == e.GrantID {
|
|
|
|
wm.ProjectGrantExists = false
|
2021-07-01 13:53:52 +00:00
|
|
|
wm.ExistingRoleKeys = []string{}
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
case *project.RoleAddedEvent:
|
|
|
|
if wm.ProjectGrantID != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.ExistingRoleKeys = append(wm.ExistingRoleKeys, e.Key)
|
|
|
|
case *project.RoleRemovedEvent:
|
|
|
|
if wm.ProjectGrantID != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i, key := range wm.ExistingRoleKeys {
|
|
|
|
if key == e.Key {
|
|
|
|
copy(wm.ExistingRoleKeys[i:], wm.ExistingRoleKeys[i+1:])
|
|
|
|
wm.ExistingRoleKeys[len(wm.ExistingRoleKeys)-1] = ""
|
|
|
|
wm.ExistingRoleKeys = wm.ExistingRoleKeys[:len(wm.ExistingRoleKeys)-1]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *UserGrantPreConditionReadModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(wm.UserID).
|
|
|
|
EventTypes(
|
|
|
|
user.UserV1AddedType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.HumanAddedType,
|
|
|
|
user.UserV1RegisteredType,
|
2021-03-25 17:12:24 +00:00
|
|
|
user.HumanRegisteredType,
|
2021-03-15 11:51:15 +00:00
|
|
|
user.MachineAddedEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
user.UserRemovedType).
|
|
|
|
Or().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(wm.ProjectID).
|
|
|
|
EventTypes(
|
2021-03-15 11:51:15 +00:00
|
|
|
project.ProjectAddedType,
|
|
|
|
project.ProjectRemovedType,
|
|
|
|
project.GrantAddedType,
|
2021-04-22 10:03:24 +00:00
|
|
|
project.GrantChangedType,
|
2021-03-15 11:51:15 +00:00
|
|
|
project.GrantRemovedType,
|
|
|
|
project.RoleAddedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
project.RoleRemovedType).
|
|
|
|
Builder()
|
2021-03-15 11:51:15 +00:00
|
|
|
return query
|
|
|
|
}
|