mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
dfcb96d6a3
* fix: user grant command side * fix: user grant command side * fix: user grant command side check permissions * fix: unique constraint on user grants * fix: add usergrant * fix: add usergrant * fix: add usergrant * fix: user grant remove * Update internal/v2/command/auth_checks.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * Update internal/v2/command/auth_checks.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * Update internal/v2/command/project.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * Update internal/v2/command/user_grant.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * fix: project events Co-authored-by: Livio Amstutz <livio.a@gmail.com>
75 lines
2.1 KiB
Go
75 lines
2.1 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 nil
|
|
}
|
|
|
|
func (wm *UserGrantWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, usergrant.AggregateType).
|
|
AggregateIDs(wm.AggregateID).
|
|
ResourceOwner(wm.ResourceOwner)
|
|
}
|
|
|
|
func UserGrantAggregateFromWriteModel(wm *eventstore.WriteModel) *usergrant.Aggregate {
|
|
return &usergrant.Aggregate{
|
|
Aggregate: *eventstore.AggregateFromWriteModel(wm, usergrant.AggregateType, usergrant.AggregateVersion),
|
|
}
|
|
}
|