mirror of
https://github.com/zitadel/zitadel.git
synced 2025-04-17 02:31:29 +00:00
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
![]() |
package command
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
||
|
"github.com/caos/zitadel/internal/v2/domain"
|
||
|
"github.com/caos/zitadel/internal/v2/repository/org"
|
||
|
"github.com/caos/zitadel/internal/v2/repository/policy"
|
||
|
)
|
||
|
|
||
|
type OrgLoginPolicyWriteModel struct {
|
||
|
LoginPolicyWriteModel
|
||
|
}
|
||
|
|
||
|
func NewOrgLoginPolicyWriteModel(orgID string) *OrgLoginPolicyWriteModel {
|
||
|
return &OrgLoginPolicyWriteModel{
|
||
|
LoginPolicyWriteModel{
|
||
|
WriteModel: eventstore.WriteModel{
|
||
|
AggregateID: orgID,
|
||
|
ResourceOwner: orgID,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (wm *OrgLoginPolicyWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
||
|
for _, event := range events {
|
||
|
switch e := event.(type) {
|
||
|
case *org.LoginPolicyAddedEvent:
|
||
|
wm.LoginPolicyWriteModel.AppendEvents(&e.LoginPolicyAddedEvent)
|
||
|
case *org.LoginPolicyChangedEvent:
|
||
|
wm.LoginPolicyWriteModel.AppendEvents(&e.LoginPolicyChangedEvent)
|
||
|
case *org.LoginPolicyRemovedEvent:
|
||
|
wm.LoginPolicyWriteModel.AppendEvents(&e.LoginPolicyRemovedEvent)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (wm *OrgLoginPolicyWriteModel) IsValid() bool {
|
||
|
return wm.AggregateID != ""
|
||
|
}
|
||
|
|
||
|
func (wm *OrgLoginPolicyWriteModel) Reduce() error {
|
||
|
return wm.LoginPolicyWriteModel.Reduce()
|
||
|
}
|
||
|
|
||
|
func (wm *OrgLoginPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
||
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, org.AggregateType).
|
||
|
AggregateIDs(wm.LoginPolicyWriteModel.AggregateID).
|
||
|
ResourceOwner(wm.ResourceOwner)
|
||
|
}
|
||
|
|
||
|
func (wm *OrgLoginPolicyWriteModel) NewChangedEvent(
|
||
|
ctx context.Context,
|
||
|
allowUsernamePassword,
|
||
|
allowRegister,
|
||
|
allowExternalIDP,
|
||
|
forceMFA bool,
|
||
|
passwordlessType domain.PasswordlessType,
|
||
|
) (*org.LoginPolicyChangedEvent, bool) {
|
||
|
|
||
|
changes := make([]policy.LoginPolicyChanges, 0)
|
||
|
if wm.AllowUserNamePassword != allowUsernamePassword {
|
||
|
changes = append(changes, policy.ChangeAllowUserNamePassword(allowUsernamePassword))
|
||
|
}
|
||
|
if wm.AllowRegister != allowRegister {
|
||
|
changes = append(changes, policy.ChangeAllowRegister(allowRegister))
|
||
|
}
|
||
|
if wm.AllowExternalIDP != allowExternalIDP {
|
||
|
changes = append(changes, policy.ChangeAllowExternalIDP(allowExternalIDP))
|
||
|
}
|
||
|
if wm.ForceMFA != forceMFA {
|
||
|
changes = append(changes, policy.ChangeForceMFA(forceMFA))
|
||
|
}
|
||
|
if passwordlessType.Valid() && wm.PasswordlessType != passwordlessType {
|
||
|
changes = append(changes, policy.ChangePasswordlessType(passwordlessType))
|
||
|
}
|
||
|
if len(changes) == 0 {
|
||
|
return nil, false
|
||
|
}
|
||
|
changedEvent, err := org.NewLoginPolicyChangedEvent(ctx, changes)
|
||
|
if err != nil {
|
||
|
return nil, false
|
||
|
}
|
||
|
return changedEvent, true
|
||
|
}
|