mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-18 13:57:32 +00:00
e318139b37
* implement notification providers * email provider * notification handler * notify users * implement code sent on user eventstore * send email implementation * send init code * handle codes * fix project member handler * add some logs for debug * send emails * text changes * send sms * notification process * send password code * format phone number * test format phone * remove fmts * remove unused code * rename files * add mocks * merge master * Update internal/notification/providers/email/message.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/repository/eventsourcing/handler/notification.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/repository/eventsourcing/handler/notification.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/providers/email/provider.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * requested changes of mr * move locker to eventstore pkg * Update internal/notification/providers/chat/message.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * move locker to eventstore pkg * linebreak * Update internal/notification/providers/email/provider.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/repository/eventsourcing/handler/notification.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/repository/eventsourcing/handler/notification.go Co-authored-by: Silvan <silvan.reusser@gmail.com> Co-authored-by: Silvan <silvan.reusser@gmail.com> Co-authored-by: Livio Amstutz <livio.a@gmail.com>
452 lines
17 KiB
Go
452 lines
17 KiB
Go
package eventsourcing
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
|
"github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
|
)
|
|
|
|
func UserByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
|
if id == "" {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d8isw", "id should be filled")
|
|
}
|
|
return UserQuery(latestSequence).
|
|
AggregateIDFilter(id), nil
|
|
}
|
|
|
|
func UserQuery(latestSequence uint64) *es_models.SearchQuery {
|
|
return es_models.NewSearchQuery().
|
|
AggregateTypeFilter(model.UserAggregate).
|
|
LatestSequenceFilter(latestSequence)
|
|
}
|
|
|
|
func UserAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User) (*es_models.Aggregate, error) {
|
|
if user == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dis83", "existing user should not be nil")
|
|
}
|
|
return aggCreator.NewAggregate(ctx, user.AggregateID, model.UserAggregate, model.UserVersion, user.Sequence)
|
|
}
|
|
|
|
func UserAggregateOverwriteContext(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User, resourceOwnerID string, userID string) (*es_models.Aggregate, error) {
|
|
if user == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dis83", "existing user should not be nil")
|
|
}
|
|
|
|
return aggCreator.NewAggregate(ctx, user.AggregateID, model.UserAggregate, model.UserVersion, user.Sequence, es_models.OverwriteResourceOwner(resourceOwnerID), es_models.OverwriteEditorUser(userID))
|
|
}
|
|
|
|
func UserCreateAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User, initCode *model.InitUserCode, phoneCode *model.PhoneCode, resourceOwner string) (agg *es_models.Aggregate, err error) {
|
|
if user == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-duxk2", "user should not be nil")
|
|
}
|
|
|
|
if resourceOwner != "" {
|
|
agg, err = UserAggregateOverwriteContext(ctx, aggCreator, user, user.AggregateID, resourceOwner)
|
|
} else {
|
|
agg, err = UserAggregate(ctx, aggCreator, user)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
agg, err = agg.AppendEvent(model.UserAdded, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if user.Email != nil && user.EmailAddress != "" && user.IsEmailVerified {
|
|
agg, err = agg.AppendEvent(model.UserEmailVerified, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if user.Phone != nil && user.PhoneNumber != "" && user.IsPhoneVerified {
|
|
agg, err = agg.AppendEvent(model.UserPhoneVerified, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if initCode != nil {
|
|
agg, err = agg.AppendEvent(model.InitializedUserCodeAdded, initCode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if phoneCode != nil {
|
|
agg, err = agg.AppendEvent(model.UserPhoneCodeAdded, phoneCode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return agg, err
|
|
}
|
|
|
|
func UserRegisterAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User, resourceOwner string, emailCode *model.EmailCode) (*es_models.Aggregate, error) {
|
|
if user == nil || resourceOwner == "" || emailCode == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-duxk2", "user, resourceowner, emailcode should not be nothing")
|
|
}
|
|
|
|
agg, err := UserAggregateOverwriteContext(ctx, aggCreator, user, resourceOwner, user.AggregateID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
agg, err = agg.AppendEvent(model.UserRegistered, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserEmailCodeAdded, emailCode)
|
|
}
|
|
|
|
func UserDeactivateAggregate(aggCreator *es_models.AggregateCreator, user *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return userStateAggregate(aggCreator, user, model.UserDeactivated)
|
|
}
|
|
|
|
func UserReactivateAggregate(aggCreator *es_models.AggregateCreator, user *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return userStateAggregate(aggCreator, user, model.UserReactivated)
|
|
}
|
|
|
|
func UserLockAggregate(aggCreator *es_models.AggregateCreator, user *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return userStateAggregate(aggCreator, user, model.UserLocked)
|
|
}
|
|
|
|
func UserUnlockAggregate(aggCreator *es_models.AggregateCreator, user *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return userStateAggregate(aggCreator, user, model.UserUnlocked)
|
|
}
|
|
|
|
func userStateAggregate(aggCreator *es_models.AggregateCreator, user *model.User, state es_models.EventType) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(state, nil)
|
|
}
|
|
}
|
|
|
|
func UserInitCodeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, code *model.InitUserCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if code == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d8i23", "code should not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.InitializedUserCodeAdded, code)
|
|
}
|
|
}
|
|
|
|
func UserInitCodeSentAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.InitializedUserCodeSent, nil)
|
|
}
|
|
}
|
|
|
|
func SkipMfaAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.MfaInitSkipped, nil)
|
|
}
|
|
}
|
|
|
|
func PasswordChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, password *model.Password) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if password == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d9832", "password should not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserPasswordChanged, password)
|
|
}
|
|
}
|
|
|
|
func PasswordCheckSucceededAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, check *model.AuthRequest) es_sdk.AggregateFunc {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserPasswordCheckSucceeded, check)
|
|
}
|
|
}
|
|
func PasswordCheckFailedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, check *model.AuthRequest) es_sdk.AggregateFunc {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserPasswordCheckFailed, check)
|
|
}
|
|
}
|
|
|
|
func RequestSetPassword(aggCreator *es_models.AggregateCreator, existing *model.User, request *model.PasswordCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if request == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d8ei2", "password set request should not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserPasswordCodeAdded, request)
|
|
}
|
|
}
|
|
|
|
func PasswordCodeSentAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserPasswordCodeSent, nil)
|
|
}
|
|
}
|
|
|
|
func ProfileChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, profile *model.Profile) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if profile == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dhr74", "profile should not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
changes := existing.Profile.Changes(profile)
|
|
if len(changes) == 0 {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-0spow", "no changes found")
|
|
}
|
|
return agg.AppendEvent(model.UserProfileChanged, changes)
|
|
}
|
|
}
|
|
|
|
func EmailChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, email *model.Email, code *model.EmailCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if email == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dki8s", "email should not be nil")
|
|
}
|
|
if (!email.IsEmailVerified && code == nil) || (email.IsEmailVerified && code != nil) {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-id934", "email has to be verified or code must be sent")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
changes := existing.Email.Changes(email)
|
|
if len(changes) == 0 {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-s90pw", "no changes found")
|
|
}
|
|
agg, err = agg.AppendEvent(model.UserEmailChanged, changes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if existing.Email == nil {
|
|
existing.Email = new(model.Email)
|
|
}
|
|
if email.IsEmailVerified {
|
|
return agg.AppendEvent(model.UserEmailVerified, code)
|
|
}
|
|
if code != nil {
|
|
return agg.AppendEvent(model.UserEmailCodeAdded, code)
|
|
}
|
|
return agg, nil
|
|
}
|
|
}
|
|
func EmailVerifiedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserEmailVerified, nil)
|
|
}
|
|
}
|
|
|
|
func EmailVerificationCodeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, code *model.EmailCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if code == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dki8s", "code should not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserEmailCodeAdded, code)
|
|
}
|
|
}
|
|
|
|
func EmailCodeSentAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserEmailCodeSent, nil)
|
|
}
|
|
}
|
|
|
|
func PhoneChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, phone *model.Phone, code *model.PhoneCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if phone == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dkso3", "phone should not be nil")
|
|
}
|
|
if (!phone.IsPhoneVerified && code == nil) || (phone.IsPhoneVerified && code != nil) {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dksi8", "phone has to be verified or code must be sent")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if existing.Phone == nil {
|
|
existing.Phone = new(model.Phone)
|
|
}
|
|
changes := existing.Phone.Changes(phone)
|
|
if len(changes) == 0 {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-sp0oc", "no changes found")
|
|
}
|
|
agg, err = agg.AppendEvent(model.UserPhoneChanged, changes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if phone.IsPhoneVerified {
|
|
return agg.AppendEvent(model.UserPhoneVerified, code)
|
|
}
|
|
if code != nil {
|
|
return agg.AppendEvent(model.UserPhoneCodeAdded, code)
|
|
}
|
|
return agg, nil
|
|
}
|
|
}
|
|
func PhoneVerifiedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserPhoneVerified, nil)
|
|
}
|
|
}
|
|
|
|
func PhoneVerificationCodeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, code *model.PhoneCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if code == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dsue2", "code should not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserPhoneCodeAdded, code)
|
|
}
|
|
}
|
|
|
|
func PhoneCodeSentAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.UserPhoneCodeSent, nil)
|
|
}
|
|
}
|
|
|
|
func AddressChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, address *model.Address) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if address == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dkx9s", "address should not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if existing.Address == nil {
|
|
existing.Address = new(model.Address)
|
|
}
|
|
changes := existing.Address.Changes(address)
|
|
if len(changes) == 0 {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-2tszw", "no changes found")
|
|
}
|
|
return agg.AppendEvent(model.UserAddressChanged, changes)
|
|
}
|
|
}
|
|
|
|
func MfaOTPAddAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, otp *model.OTP) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if otp == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dkx9s", "otp should not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.MfaOtpAdded, otp)
|
|
}
|
|
}
|
|
|
|
func MfaOTPVerifyAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.MfaOtpVerified, nil)
|
|
}
|
|
}
|
|
|
|
func MfaOTPCheckSucceededAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, authReq *model.AuthRequest) es_sdk.AggregateFunc {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if authReq == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-sd5DA", "authReq must not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.MfaOtpCheckSucceeded, authReq)
|
|
}
|
|
}
|
|
|
|
func MfaOTPCheckFailedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, authReq *model.AuthRequest) es_sdk.AggregateFunc {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if authReq == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-64sd6", "authReq must not be nil")
|
|
}
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.MfaOtpCheckFailed, authReq)
|
|
}
|
|
}
|
|
|
|
func MfaOTPRemoveAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.MfaOtpRemoved, nil)
|
|
}
|
|
}
|
|
|
|
func SignOutAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, agentID string) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return agg.AppendEvent(model.SignedOut, map[string]interface{}{"agentID": agentID})
|
|
}
|
|
}
|