2021-01-28 06:35:26 +01:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
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"
|
|
|
|
"github.com/caos/zitadel/internal/repository/member"
|
2021-01-28 06:35:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueProjectGrantMemberType = "project_grant_member"
|
|
|
|
GrantMemberAddedType = grantEventTypePrefix + member.AddedEventType
|
|
|
|
GrantMemberChangedType = grantEventTypePrefix + member.ChangedEventType
|
|
|
|
GrantMemberRemovedType = grantEventTypePrefix + member.RemovedEventType
|
2021-01-28 06:35:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewAddProjectGrantMemberUniqueConstraint(projectID, userID, grantID string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueProjectGrantMemberType,
|
2021-01-28 06:35:26 +01:00
|
|
|
fmt.Sprintf("%s:%s:%s", projectID, userID, grantID),
|
|
|
|
"Errors.Project.Member.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoveProjectGrantMemberUniqueConstraint(projectID, userID, grantID string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueProjectGrantMemberType,
|
2021-01-28 06:35:26 +01:00
|
|
|
fmt.Sprintf("%s:%s:%s", projectID, userID, grantID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantMemberAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-15 12:51:15 +01:00
|
|
|
Roles []string `json:"roles"`
|
|
|
|
UserID string `json:"userId"`
|
|
|
|
GrantID string `json:"grantId"`
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantMemberAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantMemberAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-03-15 12:51:15 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddProjectGrantMemberUniqueConstraint(e.Aggregate().ID, e.UserID, e.GrantID)}
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectGrantMemberAddedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
userID,
|
|
|
|
grantID string,
|
|
|
|
roles ...string,
|
|
|
|
) *GrantMemberAddedEvent {
|
|
|
|
return &GrantMemberAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantMemberAddedType,
|
|
|
|
),
|
2021-03-15 12:51:15 +01:00
|
|
|
UserID: userID,
|
|
|
|
GrantID: grantID,
|
|
|
|
Roles: roles,
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GrantMemberAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &GrantMemberAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-9f0sf", "unable to unmarshal label policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantMemberChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Roles []string `json:"roles"`
|
|
|
|
GrantID string `json:"grantId"`
|
|
|
|
UserID string `json:"userId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantMemberChangedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantMemberChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectGrantMemberChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
userID,
|
|
|
|
grantID string,
|
|
|
|
roles ...string,
|
|
|
|
) *GrantMemberChangedEvent {
|
|
|
|
return &GrantMemberChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantMemberAddedType,
|
|
|
|
),
|
|
|
|
UserID: userID,
|
|
|
|
GrantID: grantID,
|
|
|
|
Roles: roles,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GrantMemberChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &GrantMemberChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-39fi8", "unable to unmarshal label policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrantMemberRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-15 12:51:15 +01:00
|
|
|
UserID string `json:"userId"`
|
|
|
|
GrantID string `json:"grantId"`
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantMemberRemovedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GrantMemberRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-03-15 12:51:15 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveProjectGrantMemberUniqueConstraint(e.Aggregate().ID, e.UserID, e.GrantID)}
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectGrantMemberRemovedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
userID,
|
|
|
|
grantID string,
|
|
|
|
) *GrantMemberRemovedEvent {
|
|
|
|
return &GrantMemberRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
GrantMemberRemovedType,
|
|
|
|
),
|
2021-03-15 12:51:15 +01:00
|
|
|
UserID: userID,
|
|
|
|
GrantID: grantID,
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GrantMemberRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &GrantMemberRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-173fM", "unable to unmarshal label policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|