2021-01-04 14:52:13 +01:00
|
|
|
package policy
|
2020-12-11 15:49:19 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-01-03 09:19:07 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-18 11:24:15 +01:00
|
|
|
|
2020-12-11 15:49:19 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-11 15:49:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-08 11:33:45 +01:00
|
|
|
//TODO: use for org events as suffix (when possible)
|
2020-12-11 15:49:19 +01:00
|
|
|
OrgIAMPolicyAddedEventType = "policy.org.iam.added"
|
|
|
|
OrgIAMPolicyChangedEventType = "policy.org.iam.changed"
|
|
|
|
)
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type OrgIAMPolicyAddedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 10:47:55 +01:00
|
|
|
UserLoginMustBeDomain bool `json:"userLoginMustBeDomain,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *OrgIAMPolicyAddedEvent) Data() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *OrgIAMPolicyAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewOrgIAMPolicyAddedEvent(
|
2020-12-11 15:49:19 +01:00
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
userLoginMustBeDomain bool,
|
2021-01-04 14:52:13 +01:00
|
|
|
) *OrgIAMPolicyAddedEvent {
|
2020-12-11 15:49:19 +01:00
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
return &OrgIAMPolicyAddedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
UserLoginMustBeDomain: userLoginMustBeDomain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func OrgIAMPolicyAddedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &OrgIAMPolicyAddedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "POLIC-TvSmA", "unable to unmarshal policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type OrgIAMPolicyChangedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 11:12:56 +01:00
|
|
|
UserLoginMustBeDomain *bool `json:"userLoginMustBeDomain,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *OrgIAMPolicyChangedEvent) Data() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *OrgIAMPolicyChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewOrgIAMPolicyChangedEvent(
|
2020-12-11 15:49:19 +01:00
|
|
|
base *eventstore.BaseEvent,
|
2021-01-18 11:24:15 +01:00
|
|
|
changes []OrgIAMPolicyChanges,
|
|
|
|
) (*OrgIAMPolicyChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-DAf3h", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &OrgIAMPolicyChangedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
2021-01-18 11:24:15 +01:00
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrgIAMPolicyChanges func(*OrgIAMPolicyChangedEvent)
|
|
|
|
|
|
|
|
func ChangeUserLoginMustBeDomain(userLoginMustBeDomain bool) func(*OrgIAMPolicyChangedEvent) {
|
|
|
|
return func(e *OrgIAMPolicyChangedEvent) {
|
|
|
|
e.UserLoginMustBeDomain = &userLoginMustBeDomain
|
|
|
|
}
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func OrgIAMPolicyChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &OrgIAMPolicyChangedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "POLIC-0Pl9d", "unable to unmarshal policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2021-01-18 11:24:15 +01:00
|
|
|
|
|
|
|
type OrgIAMPolicyRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *OrgIAMPolicyRemovedEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *OrgIAMPolicyRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func NewOrgIAMPolicyRemovedEvent(base *eventstore.BaseEvent) *OrgIAMPolicyRemovedEvent {
|
|
|
|
return &OrgIAMPolicyRemovedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func OrgIAMPolicyRemovedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-18 11:24:15 +01:00
|
|
|
return &OrgIAMPolicyRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|