2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-15 08:32:59 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2021-01-08 10:33:45 +00:00
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/user"
|
|
|
|
)
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) getHuman(ctx context.Context, userID, resourceowner string) (*domain.Human, error) {
|
|
|
|
writeModel, err := r.getHumanWriteModelByID(ctx, userID, resourceowner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if writeModel.UserState == domain.UserStateUnspecified || writeModel.UserState == domain.UserStateDeleted {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-M9dsd", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
return writeModelToHuman(writeModel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) AddHuman(ctx context.Context, orgID string, human *domain.Human) (*domain.Human, error) {
|
|
|
|
userAgg, addedHuman, err := r.addHuman(ctx, orgID, human)
|
2021-01-08 10:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = r.eventstore.PushAggregate(ctx, addedHuman, userAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModelToHuman(addedHuman), nil
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) addHuman(ctx context.Context, orgID string, human *domain.Human) (*user.Aggregate, *HumanWriteModel, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
if !human.IsValid() {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-4M90d", "Errors.User.Invalid")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
return r.createHuman(ctx, orgID, human, nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) RegisterHuman(ctx context.Context, orgID string, human *domain.Human, externalIDP *domain.ExternalIDP) (*domain.Human, error) {
|
|
|
|
userAgg, addedHuman, err := r.registerHuman(ctx, orgID, human, externalIDP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = r.eventstore.PushAggregate(ctx, addedHuman, userAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModelToHuman(addedHuman), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) registerHuman(ctx context.Context, orgID string, human *domain.Human, externalIDP *domain.ExternalIDP) (*user.Aggregate, *HumanWriteModel, error) {
|
|
|
|
if !human.IsValid() || externalIDP == nil && (human.Password == nil || human.SecretString == "") {
|
|
|
|
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-9dk45", "Errors.User.Invalid")
|
|
|
|
}
|
|
|
|
return r.createHuman(ctx, orgID, human, externalIDP, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) createHuman(ctx context.Context, orgID string, human *domain.Human, externalIDP *domain.ExternalIDP, selfregister bool) (*user.Aggregate, *HumanWriteModel, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
userID, err := r.idGenerator.Next()
|
|
|
|
if err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
human.AggregateID = userID
|
2021-01-08 10:33:45 +00:00
|
|
|
orgIAMPolicy, err := r.getOrgIAMPolicy(ctx, orgID)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-05 08:33:45 +00:00
|
|
|
pwPolicy, err := r.GetOrgPasswordComplexityPolicy(ctx, orgID)
|
|
|
|
if err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
addedHuman := NewHumanWriteModel(human.AggregateID, orgID)
|
2021-01-15 08:32:59 +00:00
|
|
|
//TODO: Check Unique Username or unique external idp
|
|
|
|
if err := human.CheckOrgIAMPolicy(human.Username, orgIAMPolicy); err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
human.SetNamesAsDisplayname()
|
2021-01-07 15:06:45 +00:00
|
|
|
if err := human.HashPasswordIfExisting(pwPolicy, r.userPasswordAlg, true); err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
|
|
|
|
userAgg := UserAggregateFromWriteModel(&addedHuman.WriteModel)
|
2021-01-15 08:32:59 +00:00
|
|
|
var createEvent eventstore.EventPusher
|
|
|
|
if selfregister {
|
|
|
|
createEvent = createRegisterHumanEvent(ctx, human.Username, human)
|
|
|
|
} else {
|
|
|
|
createEvent = createAddHumanEvent(ctx, human.Username, human)
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
userAgg.PushEvents(createEvent)
|
2021-01-06 10:12:56 +00:00
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
if externalIDP != nil {
|
|
|
|
if !externalIDP.IsValid() {
|
|
|
|
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-4Dj9s", "Errors.User.ExternalIDP.Invalid")
|
|
|
|
}
|
|
|
|
//TODO: check if idpconfig exists
|
|
|
|
userAgg.PushEvents(user.NewHumanExternalIDPAddedEvent(ctx, externalIDP.IDPConfigID, externalIDP.DisplayName))
|
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
if human.IsInitialState() {
|
|
|
|
initCode, err := domain.NewInitUserCode(r.initializeUserCode)
|
|
|
|
if err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
userAgg.PushEvents(user.NewHumanInitialCodeAddedEvent(ctx, initCode.Code, initCode.Expiry))
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
if human.Email != nil && human.EmailAddress != "" && human.IsEmailVerified {
|
|
|
|
userAgg.PushEvents(user.NewHumanEmailVerifiedEvent(ctx))
|
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
if human.Phone != nil && human.PhoneNumber != "" && !human.IsPhoneVerified {
|
|
|
|
phoneCode, err := domain.NewPhoneCode(r.phoneVerificationCode)
|
|
|
|
if err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
user.NewHumanPhoneCodeAddedEvent(ctx, phoneCode.Code, phoneCode.Expiry)
|
|
|
|
} else if human.Phone != nil && human.PhoneNumber != "" && human.IsPhoneVerified {
|
2021-01-04 13:52:13 +00:00
|
|
|
userAgg.PushEvents(user.NewHumanPhoneVerifiedEvent(ctx))
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
return userAgg, addedHuman, nil
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) ResendInitialMail(ctx context.Context, userID, email, resourceowner string) (err error) {
|
|
|
|
if userID == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M9fs", "Errors.User.UserIDMissing")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
|
|
|
|
existingEmail, err := r.emailWriteModel(ctx, userID, resourceowner)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
2021-01-15 08:32:59 +00:00
|
|
|
return err
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
if existingEmail.UserState == domain.UserStateUnspecified || existingEmail.UserState == domain.UserStateDeleted {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-2M9df", "Errors.User.NotFound")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
if existingEmail.UserState != domain.UserStateInitial {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M9sd", "Errors.User.AlreadyInitialised")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
userAgg := UserAggregateFromWriteModel(&existingEmail.WriteModel)
|
|
|
|
if email != "" && existingEmail.Email != email {
|
|
|
|
changedEvent, _ := existingEmail.NewChangedEvent(ctx, email)
|
|
|
|
userAgg.PushEvents(changedEvent)
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
initCode, err := domain.NewInitUserCode(r.initializeUserCode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
userAgg.PushEvents(user.NewHumanInitialCodeAddedEvent(ctx, initCode.Code, initCode.Expiry))
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingEmail, userAgg)
|
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func createAddHumanEvent(ctx context.Context, username string, human *domain.Human) *user.HumanAddedEvent {
|
|
|
|
addEvent := user.NewHumanAddedEvent(
|
2021-01-06 10:12:56 +00:00
|
|
|
ctx,
|
|
|
|
username,
|
|
|
|
human.FirstName,
|
|
|
|
human.LastName,
|
|
|
|
human.NickName,
|
|
|
|
human.DisplayName,
|
|
|
|
human.PreferredLanguage,
|
|
|
|
human.Gender,
|
|
|
|
human.EmailAddress,
|
|
|
|
)
|
|
|
|
if human.Phone != nil {
|
|
|
|
addEvent.AddPhoneData(human.PhoneNumber)
|
|
|
|
}
|
|
|
|
if human.Address != nil {
|
|
|
|
addEvent.AddAddressData(
|
|
|
|
human.Country,
|
|
|
|
human.Locality,
|
|
|
|
human.PostalCode,
|
|
|
|
human.Region,
|
|
|
|
human.StreetAddress)
|
|
|
|
}
|
|
|
|
if human.Password != nil {
|
|
|
|
addEvent.AddPasswordData(human.SecretCrypto, human.ChangeRequired)
|
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
return addEvent
|
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func createRegisterHumanEvent(ctx context.Context, username string, human *domain.Human) *user.HumanRegisteredEvent {
|
|
|
|
addEvent := user.NewHumanRegisteredEvent(
|
|
|
|
ctx,
|
|
|
|
username,
|
|
|
|
human.FirstName,
|
|
|
|
human.LastName,
|
|
|
|
human.NickName,
|
|
|
|
human.DisplayName,
|
|
|
|
human.PreferredLanguage,
|
|
|
|
human.Gender,
|
|
|
|
human.EmailAddress,
|
|
|
|
)
|
|
|
|
if human.Phone != nil {
|
|
|
|
addEvent.AddPhoneData(human.PhoneNumber)
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
if human.Address != nil {
|
|
|
|
addEvent.AddAddressData(
|
|
|
|
human.Country,
|
|
|
|
human.Locality,
|
|
|
|
human.PostalCode,
|
|
|
|
human.Region,
|
|
|
|
human.StreetAddress)
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
if human.Password != nil {
|
|
|
|
addEvent.AddPasswordData(human.SecretCrypto, human.ChangeRequired)
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
return addEvent
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) getHumanWriteModelByID(ctx context.Context, userID, resourceowner string) (*HumanWriteModel, error) {
|
|
|
|
humanWriteModel := NewHumanWriteModel(userID, resourceowner)
|
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, humanWriteModel)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
2021-01-15 08:32:59 +00:00
|
|
|
return nil, err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
return humanWriteModel, nil
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|