2021-01-28 05:35:26 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-01-03 08:19:07 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-01-28 05:35:26 +00:00
|
|
|
)
|
|
|
|
|
2024-07-04 16:18:43 +00:00
|
|
|
const (
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueRoleType = "project_role"
|
2021-01-28 05:35:26 +00:00
|
|
|
roleEventTypePrefix = projectEventTypePrefix + "role."
|
|
|
|
RoleAddedType = roleEventTypePrefix + "added"
|
|
|
|
RoleChangedType = roleEventTypePrefix + "changed"
|
|
|
|
RoleRemovedType = roleEventTypePrefix + "removed"
|
2024-07-03 15:00:56 +00:00
|
|
|
|
|
|
|
ProjectRoleSearchType = "project_role"
|
|
|
|
ProjectRoleRevision = uint8(1)
|
|
|
|
ProjectRoleKeySearchField = "key"
|
|
|
|
ProjectRoleDisplayNameSearchField = "display_name"
|
|
|
|
ProjectRoleGroupSearchField = "group"
|
2021-01-28 05:35:26 +00:00
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func NewAddProjectRoleUniqueConstraint(roleKey, projectID string) *eventstore.UniqueConstraint {
|
2021-01-28 05:35:26 +00:00
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueRoleType,
|
|
|
|
fmt.Sprintf("%s:%s", roleKey, projectID),
|
2021-01-28 05:35:26 +00:00
|
|
|
"Errors.Project.Role.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func NewRemoveProjectRoleUniqueConstraint(roleKey, projectID string) *eventstore.UniqueConstraint {
|
|
|
|
return eventstore.NewRemoveUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueRoleType,
|
|
|
|
fmt.Sprintf("%s:%s", roleKey, projectID))
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RoleAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
DisplayName string `json:"displayName,omitempty"`
|
|
|
|
Group string `json:"group,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *RoleAddedEvent) Payload() interface{} {
|
2021-01-28 05:35:26 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *RoleAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewAddProjectRoleUniqueConstraint(e.Key, e.Aggregate().ID)}
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:00:56 +00:00
|
|
|
func (e *RoleAddedEvent) Fields() []*eventstore.FieldOperation {
|
|
|
|
return []*eventstore.FieldOperation{
|
|
|
|
eventstore.SetField(
|
|
|
|
e.Aggregate(),
|
|
|
|
projectRoleSearchObject(e.Key),
|
|
|
|
ProjectRoleKeySearchField,
|
|
|
|
&eventstore.Value{
|
|
|
|
Value: e.Key,
|
|
|
|
ShouldIndex: true,
|
|
|
|
},
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
|
|
eventstore.FieldTypeResourceOwner,
|
|
|
|
eventstore.FieldTypeAggregateType,
|
|
|
|
eventstore.FieldTypeAggregateID,
|
|
|
|
eventstore.FieldTypeObjectType,
|
|
|
|
eventstore.FieldTypeObjectID,
|
|
|
|
eventstore.FieldTypeFieldName,
|
|
|
|
),
|
|
|
|
eventstore.SetField(
|
|
|
|
e.Aggregate(),
|
|
|
|
projectRoleSearchObject(e.Key),
|
|
|
|
ProjectRoleDisplayNameSearchField,
|
|
|
|
&eventstore.Value{
|
|
|
|
Value: e.DisplayName,
|
|
|
|
ShouldIndex: true,
|
|
|
|
},
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
|
|
eventstore.FieldTypeResourceOwner,
|
|
|
|
eventstore.FieldTypeAggregateType,
|
|
|
|
eventstore.FieldTypeAggregateID,
|
|
|
|
eventstore.FieldTypeObjectType,
|
|
|
|
eventstore.FieldTypeObjectID,
|
|
|
|
eventstore.FieldTypeFieldName,
|
|
|
|
),
|
|
|
|
eventstore.SetField(
|
|
|
|
e.Aggregate(),
|
|
|
|
projectRoleSearchObject(e.Key),
|
|
|
|
ProjectRoleGroupSearchField,
|
|
|
|
&eventstore.Value{
|
|
|
|
Value: e.Group,
|
|
|
|
ShouldIndex: true,
|
|
|
|
},
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
|
|
eventstore.FieldTypeResourceOwner,
|
|
|
|
eventstore.FieldTypeAggregateType,
|
|
|
|
eventstore.FieldTypeAggregateID,
|
|
|
|
eventstore.FieldTypeObjectType,
|
|
|
|
eventstore.FieldTypeObjectID,
|
|
|
|
eventstore.FieldTypeFieldName,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewRoleAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
key,
|
|
|
|
displayName,
|
2021-03-15 11:51:15 +00:00
|
|
|
group string,
|
2021-02-18 13:48:27 +00:00
|
|
|
) *RoleAddedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &RoleAddedEvent{
|
2021-02-15 12:31:24 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
2021-01-28 05:35:26 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
RoleAddedType,
|
|
|
|
),
|
|
|
|
Key: key,
|
|
|
|
DisplayName: displayName,
|
|
|
|
Group: group,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func RoleAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
e := &RoleAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "PROJECT-2M0xy", "unable to unmarshal project role")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RoleChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
DisplayName *string `json:"displayName,omitempty"`
|
|
|
|
Group *string `json:"group,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *RoleChangedEvent) Payload() interface{} {
|
2021-01-28 05:35:26 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *RoleChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:00:56 +00:00
|
|
|
func (e *RoleChangedEvent) Fields() []*eventstore.FieldOperation {
|
|
|
|
operations := make([]*eventstore.FieldOperation, 0, 2)
|
|
|
|
if e.DisplayName != nil {
|
|
|
|
operations = append(operations, eventstore.SetField(
|
|
|
|
e.Aggregate(),
|
|
|
|
projectRoleSearchObject(e.Key),
|
|
|
|
ProjectRoleDisplayNameSearchField,
|
|
|
|
&eventstore.Value{
|
|
|
|
Value: *e.DisplayName,
|
|
|
|
ShouldIndex: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
|
|
eventstore.FieldTypeResourceOwner,
|
|
|
|
eventstore.FieldTypeAggregateType,
|
|
|
|
eventstore.FieldTypeAggregateID,
|
|
|
|
eventstore.FieldTypeObjectType,
|
|
|
|
eventstore.FieldTypeObjectID,
|
|
|
|
eventstore.FieldTypeFieldName,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
if e.Group != nil {
|
|
|
|
operations = append(operations, eventstore.SetField(
|
|
|
|
e.Aggregate(),
|
|
|
|
projectRoleSearchObject(e.Key),
|
|
|
|
ProjectRoleGroupSearchField,
|
|
|
|
&eventstore.Value{
|
|
|
|
Value: *e.Group,
|
|
|
|
ShouldIndex: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
|
|
eventstore.FieldTypeResourceOwner,
|
|
|
|
eventstore.FieldTypeAggregateType,
|
|
|
|
eventstore.FieldTypeAggregateID,
|
|
|
|
eventstore.FieldTypeObjectType,
|
|
|
|
eventstore.FieldTypeObjectID,
|
|
|
|
eventstore.FieldTypeFieldName,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
return operations
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
func NewRoleChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
key string,
|
2021-02-18 13:48:27 +00:00
|
|
|
changes []RoleChanges,
|
|
|
|
) (*RoleChangedEvent, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
if len(changes) == 0 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "PROJECT-eR9vx", "Errors.NoChangesFound")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
changeEvent := &RoleChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
RoleChangedType,
|
|
|
|
),
|
2021-03-15 11:51:15 +00:00
|
|
|
Key: key,
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RoleChanges func(event *RoleChangedEvent)
|
|
|
|
|
|
|
|
func ChangeKey(key string) func(event *RoleChangedEvent) {
|
|
|
|
return func(e *RoleChangedEvent) {
|
|
|
|
e.Key = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeDisplayName(displayName string) func(event *RoleChangedEvent) {
|
|
|
|
return func(e *RoleChangedEvent) {
|
|
|
|
e.DisplayName = &displayName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeGroup(group string) func(event *RoleChangedEvent) {
|
|
|
|
return func(e *RoleChangedEvent) {
|
|
|
|
e.Group = &group
|
|
|
|
}
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
func RoleChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
e := &RoleChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "PROJECT-3M0vx", "unable to unmarshal project role")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RoleRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-15 11:51:15 +00:00
|
|
|
Key string `json:"key,omitempty"`
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *RoleRemovedEvent) Payload() interface{} {
|
2021-01-28 05:35:26 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *RoleRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewRemoveProjectRoleUniqueConstraint(e.Key, e.Aggregate().ID)}
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:00:56 +00:00
|
|
|
func (e *RoleRemovedEvent) Fields() []*eventstore.FieldOperation {
|
|
|
|
return []*eventstore.FieldOperation{
|
|
|
|
eventstore.RemoveSearchFieldsByAggregateAndObject(
|
|
|
|
e.Aggregate(),
|
|
|
|
projectRoleSearchObject(e.Key),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewRoleRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
2021-03-15 11:51:15 +00:00
|
|
|
key string) *RoleRemovedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &RoleRemovedEvent{
|
2021-02-15 12:31:24 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
2021-01-28 05:35:26 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
RoleRemovedType,
|
|
|
|
),
|
2021-03-15 11:51:15 +00:00
|
|
|
Key: key,
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func RoleRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
e := &RoleRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "PROJECT-1M0xs", "unable to unmarshal project role")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2024-07-03 15:00:56 +00:00
|
|
|
|
|
|
|
func projectRoleSearchObject(id string) eventstore.Object {
|
|
|
|
return eventstore.Object{
|
|
|
|
Type: ProjectRoleSearchType,
|
|
|
|
Revision: ProjectRoleRevision,
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
}
|