feat: set up org (#1157)

* add setup steps

* refactoring

* omitempty

* cleanup

* begin org

* create org

* setup org

* setup org

* merge

* fixes

* fixes

* fixes
This commit is contained in:
Livio Amstutz
2021-01-08 11:33:45 +01:00
committed by GitHub
parent 26c8113930
commit ff87264f95
40 changed files with 1160 additions and 162 deletions

View File

@@ -0,0 +1,222 @@
package org
import (
"context"
"encoding/json"
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
"github.com/caos/zitadel/internal/v2/domain"
)
const (
domainEventPrefix = orgEventTypePrefix + "domain."
OrgDomainAdded = domainEventPrefix + "added"
OrgDomainVerificationAdded = domainEventPrefix + "verification.added"
OrgDomainVerificationFailed = domainEventPrefix + "verification.failed"
OrgDomainVerified = domainEventPrefix + "verified"
OrgDomainPrimarySet = domainEventPrefix + "primary.set"
OrgDomainRemoved = domainEventPrefix + "removed"
)
type DomainAddedEvent struct {
eventstore.BaseEvent `json:"-"`
Domain string `json:"domain,omitempty"`
}
func (e *DomainAddedEvent) Data() interface{} {
return e
}
func NewDomainAddedEvent(ctx context.Context, domain string) *DomainAddedEvent {
return &DomainAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgDomainAdded,
),
Domain: domain,
}
}
func DomainAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
orgDomainAdded := &DomainAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, orgDomainAdded)
if err != nil {
return nil, errors.ThrowInternal(err, "ORG-GBr52", "unable to unmarshal org domain added")
}
return orgDomainAdded, nil
}
type DomainVerificationAddedEvent struct {
eventstore.BaseEvent `json:"-"`
Domain string `json:"domain,omitempty"`
ValidationType domain.OrgDomainValidationType `json:"validationType,omitempty"`
ValidationCode *crypto.CryptoValue `json:"validationCode,omitempty"`
}
func (e *DomainVerificationAddedEvent) Data() interface{} {
return e
}
func NewDomainVerificationAddedEvent(
ctx context.Context,
domain string,
validationType domain.OrgDomainValidationType,
validationCode *crypto.CryptoValue) *DomainVerificationAddedEvent {
return &DomainVerificationAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgDomainVerificationAdded,
),
Domain: domain,
ValidationType: validationType,
ValidationCode: validationCode,
}
}
func DomainVerificationAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
orgDomainVerificationAdded := &DomainVerificationAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, orgDomainVerificationAdded)
if err != nil {
return nil, errors.ThrowInternal(err, "ORG-NRN32", "unable to unmarshal org domain verification added")
}
return orgDomainVerificationAdded, nil
}
type DomainVerificationFailedEvent struct {
eventstore.BaseEvent `json:"-"`
Domain string `json:"domain,omitempty"`
}
func (e *DomainVerificationFailedEvent) Data() interface{} {
return e
}
func NewDomainVerificationFailedEvent(ctx context.Context, domain string) *DomainVerificationFailedEvent {
return &DomainVerificationFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgDomainVerificationFailed,
),
Domain: domain,
}
}
func DomainVerificationFailedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
orgDomainVerificationFailed := &DomainVerificationFailedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, orgDomainVerificationFailed)
if err != nil {
return nil, errors.ThrowInternal(err, "ORG-Bhm37", "unable to unmarshal org domain verification failed")
}
return orgDomainVerificationFailed, nil
}
type DomainVerifiedEvent struct {
eventstore.BaseEvent `json:"-"`
Domain string `json:"domain,omitempty"`
}
func (e *DomainVerifiedEvent) Data() interface{} {
return e
}
func NewDomainVerifiedEvent(ctx context.Context, domain string) *DomainVerifiedEvent {
return &DomainVerifiedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgDomainVerified,
),
Domain: domain,
}
}
func DomainVerifiedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
orgDomainVerified := &DomainVerifiedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, orgDomainVerified)
if err != nil {
return nil, errors.ThrowInternal(err, "ORG-BFSwt", "unable to unmarshal org domain verified")
}
return orgDomainVerified, nil
}
type DomainPrimarySetEvent struct {
eventstore.BaseEvent `json:"-"`
Domain string `json:"domain,omitempty"`
}
func (e *DomainPrimarySetEvent) Data() interface{} {
return e
}
func NewDomainPrimarySetEvent(ctx context.Context, domain string) *DomainPrimarySetEvent {
return &DomainPrimarySetEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgDomainPrimarySet,
),
Domain: domain,
}
}
func DomainPrimarySetEventMapper(event *repository.Event) (eventstore.EventReader, error) {
orgDomainPrimarySet := &DomainPrimarySetEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, orgDomainPrimarySet)
if err != nil {
return nil, errors.ThrowInternal(err, "ORG-N5787", "unable to unmarshal org domain primary set")
}
return orgDomainPrimarySet, nil
}
type DomainRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
Domain string `json:"domain,omitempty"`
}
func (e *DomainRemovedEvent) Data() interface{} {
return e
}
func NewDomainRemovedEvent(ctx context.Context, domain string) *DomainRemovedEvent {
return &DomainRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgDomainRemoved,
),
Domain: domain,
}
}
func DomainRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
orgDomainRemoved := &DomainRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, orgDomainRemoved)
if err != nil {
return nil, errors.ThrowInternal(err, "ORG-BngB2", "unable to unmarshal org domain removed")
}
return orgDomainRemoved, nil
}

View File

@@ -0,0 +1,19 @@
package org
import (
"github.com/caos/zitadel/internal/eventstore/v2"
)
func RegisterEventMappers(es *eventstore.Eventstore) {
es.RegisterFilterEventMapper(OrgAdded, OrgAddedEventMapper).
RegisterFilterEventMapper(OrgChanged, OrgChangedEventMapper).
//RegisterFilterEventMapper(OrgDeactivated, OrgChangedEventMapper). TODO: !
//RegisterFilterEventMapper(OrgReactivated, OrgChangedEventMapper).
//RegisterFilterEventMapper(OrgRemoved, OrgChangedEventMapper).
RegisterFilterEventMapper(OrgDomainAdded, DomainAddedEventMapper).
RegisterFilterEventMapper(OrgDomainVerificationAdded, DomainVerificationAddedEventMapper).
RegisterFilterEventMapper(OrgDomainVerificationFailed, DomainVerificationFailedEventMapper).
RegisterFilterEventMapper(OrgDomainVerified, DomainVerifiedEventMapper).
RegisterFilterEventMapper(OrgDomainPrimarySet, DomainPrimarySetEventMapper).
RegisterFilterEventMapper(OrgDomainRemoved, DomainRemovedEventMapper)
}

View File

@@ -0,0 +1,82 @@
package org
import (
"context"
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
const (
OrgAdded = orgEventTypePrefix + "added"
OrgChanged = orgEventTypePrefix + "changed"
OrgDeactivated = orgEventTypePrefix + "deactivated"
OrgReactivated = orgEventTypePrefix + "reactivated"
OrgRemoved = orgEventTypePrefix + "removed"
)
type OrgAddedEvent struct {
eventstore.BaseEvent `json:"-"`
Name string `json:"name,omitempty"`
}
func (e *OrgAddedEvent) Data() interface{} {
return e
}
func NewOrgAddedEvent(ctx context.Context, name string) *OrgAddedEvent {
return &OrgAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgAdded,
),
Name: name,
}
}
func OrgAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
orgAdded := &OrgAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, orgAdded)
if err != nil {
return nil, errors.ThrowInternal(err, "ORG-Bren2", "unable to unmarshal org added")
}
return orgAdded, nil
}
type OrgChangedEvent struct {
eventstore.BaseEvent `json:"-"`
Name string `json:"name,omitempty"`
}
func (e *OrgChangedEvent) Data() interface{} {
return e
}
func NewOrgChangedEvent(ctx context.Context, name string) *OrgChangedEvent {
return &OrgChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
OrgChanged,
),
Name: name,
}
}
func OrgChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
orgChanged := &OrgChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, orgChanged)
if err != nil {
return nil, errors.ThrowInternal(err, "ORG-Bren2", "unable to unmarshal org added")
}
return orgChanged, nil
}

View File

@@ -8,14 +8,38 @@ import (
)
var (
OrgIAMPolicyAddedEventType = orgEventTypePrefix + policy.OrgIAMPolicyAddedEventType
OrgIAMPolicyChangedEventType = orgEventTypePrefix + policy.OrgIAMPolicyChangedEventType
//TODO: enable when possible
//OrgIAMPolicyAddedEventType = orgEventTypePrefix + policy.OrgIAMPolicyAddedEventType
//OrgIAMPolicyChangedEventType = orgEventTypePrefix + policy.OrgIAMPolicyChangedEventType
OrgIAMPolicyAddedEventType = orgEventTypePrefix + "iam.policy.added"
OrgIAMPolicyChangedEventType = orgEventTypePrefix + "iam.policy.changed"
)
type OrgIAMPolicyAddedEvent struct {
policy.OrgIAMPolicyAddedEvent
}
func NewOrgIAMPolicyAddedEvent(
ctx context.Context,
userLoginMustBeDomain bool,
) *OrgIAMPolicyAddedEvent {
return &OrgIAMPolicyAddedEvent{
OrgIAMPolicyAddedEvent: *policy.NewOrgIAMPolicyAddedEvent(
eventstore.NewBaseEventForPush(ctx, OrgIAMPolicyAddedEventType),
userLoginMustBeDomain,
),
}
}
func OrgIAMPolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := policy.OrgIAMPolicyAddedEventMapper(event)
if err != nil {
return nil, err
}
return &OrgIAMPolicyAddedEvent{OrgIAMPolicyAddedEvent: *e.(*policy.OrgIAMPolicyAddedEvent)}, nil
}
type OrgIAMPolicyChangedEvent struct {
policy.OrgIAMPolicyChangedEvent
}