zitadel/internal/v2/repository/policy/policy_org_iam.go
Livio Amstutz 3eb909c4b4
feat: org and policies commands (#1167)
* add setup steps

* refactoring

* omitempty

* cleanup

* begin org

* create org

* setup org

* setup org

* merge

* fixes

* fixes

* fixes

* add project

* add oidc application

* fix app creation

* add resourceOwner to writemodels

* resource owner

* cleanup

* global org, iam project and iam member in setup

* logs

* logs

* logs

* cleanup

* Update internal/v2/command/project.go

Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>

* check project state

* add org domain commands

* add org status changes and member commands

* fixes

* policies

* login policy

* fix iam project event

* mapper

* label policy

* change to command

* fix

* fix

* handle change event differently and lot of fixes

* fixes

* changedEvent handling

Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>
2021-01-18 11:24:15 +01:00

117 lines
2.8 KiB
Go

package policy
import (
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
const (
//TODO: use for org events as suffix (when possible)
OrgIAMPolicyAddedEventType = "policy.org.iam.added"
OrgIAMPolicyChangedEventType = "policy.org.iam.changed"
)
type OrgIAMPolicyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
UserLoginMustBeDomain bool `json:"userLoginMustBeDomain,omitempty"`
}
func (e *OrgIAMPolicyAddedEvent) Data() interface{} {
return e
}
func NewOrgIAMPolicyAddedEvent(
base *eventstore.BaseEvent,
userLoginMustBeDomain bool,
) *OrgIAMPolicyAddedEvent {
return &OrgIAMPolicyAddedEvent{
BaseEvent: *base,
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
}
type OrgIAMPolicyChangedEvent struct {
eventstore.BaseEvent `json:"-"`
UserLoginMustBeDomain *bool `json:"userLoginMustBeDomain,omitempty"`
}
func (e *OrgIAMPolicyChangedEvent) Data() interface{} {
return e
}
func NewOrgIAMPolicyChangedEvent(
base *eventstore.BaseEvent,
changes []OrgIAMPolicyChanges,
) (*OrgIAMPolicyChangedEvent, error) {
if len(changes) == 0 {
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-DAf3h", "Errors.NoChangesFound")
}
changeEvent := &OrgIAMPolicyChangedEvent{
BaseEvent: *base,
}
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
}
}
func OrgIAMPolicyChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &OrgIAMPolicyChangedEvent{
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
}
type OrgIAMPolicyRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *OrgIAMPolicyRemovedEvent) Data() interface{} {
return nil
}
func NewOrgIAMPolicyRemovedEvent(base *eventstore.BaseEvent) *OrgIAMPolicyRemovedEvent {
return &OrgIAMPolicyRemovedEvent{
BaseEvent: *base,
}
}
func OrgIAMPolicyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &OrgIAMPolicyRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}