2021-01-28 06:35:26 +01:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-01-03 09:19:07 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 14:48:27 +01:00
|
|
|
|
2021-01-28 06:35:26 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2021-01-28 06:35:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueGrantType = "project_grant"
|
2021-01-28 06:35:26 +01:00
|
|
|
grantEventTypePrefix = projectEventTypePrefix + "grant."
|
|
|
|
GrantAddedType = grantEventTypePrefix + "added"
|
|
|
|
GrantChangedType = grantEventTypePrefix + "changed"
|
|
|
|
GrantCascadeChangedType = grantEventTypePrefix + "cascade.changed"
|
|
|
|
GrantDeactivatedType = grantEventTypePrefix + "deactivated"
|
|
|
|
GrantReactivatedType = grantEventTypePrefix + "reactivated"
|
|
|
|
GrantRemovedType = grantEventTypePrefix + "removed"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewAddProjectGrantUniqueConstraint(grantedOrgID, projectID string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-03-30 13:01:14 +02:00
|
|
|
UniqueGrantType,
|
2021-01-28 06:35:26 +01:00
|
|
|
fmt.Sprintf("%s:%s", grantedOrgID, projectID),
|
|
|
|
"Errors.Project.Grant.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoveProjectGrantUniqueConstraint(grantedOrgID, projectID string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
2021-03-30 13:01:14 +02:00
|
|
|
UniqueGrantType,
|
2021-01-28 06:35:26 +01:00
|
|
|
fmt.Sprintf("%s:%s", grantedOrgID, projectID))
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
GrantID string `json:"grantId,omitempty"`
|
|
|
|
GrantedOrgID string `json:"grantedOrgId,omitempty"`
|
|
|
|
RoleKeys []string `json:"roleKeys,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-03-15 12:51:15 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddProjectGrantUniqueConstraint(e.GrantedOrgID, e.Aggregate().ID)}
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewGrantAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
grantID,
|
2021-03-15 12:51:15 +01:00
|
|
|
grantedOrgID string,
|
2021-02-18 14:48:27 +01:00
|
|
|
roleKeys []string,
|
|
|
|
) *GrantAddedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &GrantAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantAddedType,
|
|
|
|
),
|
|
|
|
GrantID: grantID,
|
|
|
|
GrantedOrgID: grantedOrgID,
|
|
|
|
RoleKeys: roleKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func GrantAddedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &GrantAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-mL0vs", "unable to unmarshal project grant")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
GrantID string `json:"grantId,omitempty"`
|
|
|
|
RoleKeys []string `json:"roleKeys,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantChangedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewGrantChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
grantID string,
|
|
|
|
roleKeys []string,
|
|
|
|
) *GrantChangedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &GrantChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantChangedType,
|
|
|
|
),
|
|
|
|
GrantID: grantID,
|
|
|
|
RoleKeys: roleKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func GrantChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &GrantChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-mL0vs", "unable to unmarshal project grant")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantCascadeChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
GrantID string `json:"grantId,omitempty"`
|
|
|
|
RoleKeys []string `json:"roleKeys,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantCascadeChangedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantCascadeChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewGrantCascadeChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
grantID string,
|
|
|
|
roleKeys []string,
|
|
|
|
) *GrantCascadeChangedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &GrantCascadeChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantCascadeChangedType,
|
|
|
|
),
|
|
|
|
GrantID: grantID,
|
|
|
|
RoleKeys: roleKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func GrantCascadeChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &GrantCascadeChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-9o0se", "unable to unmarshal project grant")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantDeactivateEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
GrantID string `json:"grantId,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantDeactivateEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantDeactivateEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewGrantDeactivateEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
grantID string,
|
|
|
|
) *GrantDeactivateEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &GrantDeactivateEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantDeactivatedType,
|
|
|
|
),
|
|
|
|
GrantID: grantID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func GrantDeactivateEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &GrantDeactivateEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-9o0se", "unable to unmarshal project grant")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantReactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
GrantID string `json:"grantId,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantReactivatedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewGrantReactivatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
grantID string,
|
|
|
|
) *GrantReactivatedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &GrantReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantReactivatedType,
|
|
|
|
),
|
|
|
|
GrantID: grantID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func GrantReactivatedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &GrantReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-78f7D", "unable to unmarshal project grant")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
GrantID string `json:"grantId,omitempty"`
|
|
|
|
grantedOrgID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantRemovedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-03-15 12:51:15 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveProjectGrantUniqueConstraint(e.grantedOrgID, e.Aggregate().ID)}
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewGrantRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
grantID,
|
2021-03-15 12:51:15 +01:00
|
|
|
grantedOrgID string,
|
2021-02-18 14:48:27 +01:00
|
|
|
) *GrantRemovedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &GrantRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantRemovedType,
|
|
|
|
),
|
|
|
|
GrantID: grantID,
|
|
|
|
grantedOrgID: grantedOrgID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func GrantRemovedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &GrantRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-28jM8", "unable to unmarshal project grant")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|