zitadel/internal/v2/repository/policy/org_iam.go

78 lines
1.6 KiB
Go
Raw Normal View History

2020-11-06 16:25:07 +00:00
package policy
import (
"context"
"encoding/json"
2020-11-06 16:25:07 +00:00
"github.com/caos/zitadel/internal/errors"
2020-11-06 16:25:07 +00:00
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-11-06 16:25:07 +00:00
)
const (
OrgIAMPolicyAddedEventType = "policy.org.iam.added"
)
2020-11-11 16:51:44 +00:00
type OrgIAMPolicyAggregate struct {
eventstore.Aggregate
UserLoginMustBeDomain bool
}
type OrgIAMPolicyReadModel struct {
eventstore.ReadModel
UserLoginMustBeDomain bool
}
func (rm *OrgIAMPolicyReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *OrgIAMPolicyAddedEvent:
rm.UserLoginMustBeDomain = e.UserLoginMustBeDomain
}
}
return rm.ReadModel.Reduce()
}
2020-11-06 16:25:07 +00:00
type OrgIAMPolicyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
UserLoginMustBeDomain bool `json:"allowUsernamePassword"`
}
func (e *OrgIAMPolicyAddedEvent) CheckPrevious() bool {
return true
}
func (e *OrgIAMPolicyAddedEvent) Data() interface{} {
return e
}
func NewOrgIAMPolicyAddedEvent(
ctx context.Context,
userLoginMustBeDomain bool,
) *OrgIAMPolicyAddedEvent {
return &OrgIAMPolicyAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgIAMPolicyAddedEventType,
),
UserLoginMustBeDomain: userLoginMustBeDomain,
}
}
func OrgIAMPolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &OrgIAMPolicyAddedEvent{
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
}