2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-25 09:12:24 +00:00
|
|
|
"strings"
|
2021-08-18 15:04:56 +00:00
|
|
|
|
2022-04-20 14:59:37 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) getHuman(ctx context.Context, userID, resourceowner string) (*domain.Human, error) {
|
|
|
|
human, err := c.getHumanWriteModelByID(ctx, userID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
if !isUserStateExists(human.UserState) {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "COMMAND-M9dsd", "Errors.User.NotFound")
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
return writeModelToHuman(human), nil
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
type AddHuman struct {
|
2023-04-26 14:19:32 +00:00
|
|
|
// ID is optional, if empty it will be generated
|
2023-04-26 05:47:57 +00:00
|
|
|
ID string
|
2022-04-12 14:20:17 +00:00
|
|
|
// Username is required
|
|
|
|
Username string
|
|
|
|
// FirstName is required
|
|
|
|
FirstName string
|
|
|
|
// LastName is required
|
|
|
|
LastName string
|
|
|
|
// NickName is required
|
|
|
|
NickName string
|
|
|
|
// DisplayName is required
|
|
|
|
DisplayName string
|
|
|
|
// Email is required
|
|
|
|
Email Email
|
2022-04-28 08:30:41 +00:00
|
|
|
// PreferredLanguage is required
|
|
|
|
PreferredLanguage language.Tag
|
2022-04-12 14:20:17 +00:00
|
|
|
// Gender is required
|
|
|
|
Gender domain.Gender
|
2023-04-26 05:47:57 +00:00
|
|
|
// Phone represents an international phone number
|
2022-04-12 14:20:17 +00:00
|
|
|
Phone Phone
|
2023-04-26 05:47:57 +00:00
|
|
|
// Password is optional
|
2022-04-12 14:20:17 +00:00
|
|
|
Password string
|
2023-07-14 06:49:57 +00:00
|
|
|
// EncodedPasswordHash is optional
|
|
|
|
EncodedPasswordHash string
|
2023-04-26 05:47:57 +00:00
|
|
|
// PasswordChangeRequired is used if the `Password`-field is set
|
2022-04-12 14:20:17 +00:00
|
|
|
PasswordChangeRequired bool
|
|
|
|
Passwordless bool
|
|
|
|
ExternalIDP bool
|
|
|
|
Register bool
|
2023-04-26 05:47:57 +00:00
|
|
|
Metadata []*AddMetadataEntry
|
|
|
|
|
2023-05-24 18:29:58 +00:00
|
|
|
// Links are optional
|
|
|
|
Links []*AddLink
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
// Details are set after a successful execution of the command
|
|
|
|
Details *domain.ObjectDetails
|
|
|
|
|
|
|
|
// EmailCode is set by the command
|
|
|
|
EmailCode *string
|
2023-08-03 04:42:59 +00:00
|
|
|
|
|
|
|
// PhoneCode is set by the command
|
|
|
|
PhoneCode *string
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 18:29:58 +00:00
|
|
|
type AddLink struct {
|
|
|
|
IDPID string
|
|
|
|
DisplayName string
|
|
|
|
IDPExternalID string
|
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
func (h *AddHuman) Validate(hasher *crypto.PasswordHasher) (err error) {
|
2023-04-26 05:47:57 +00:00
|
|
|
if err := h.Email.Validate(); err != nil {
|
|
|
|
return err
|
2021-03-19 17:46:26 +00:00
|
|
|
}
|
2023-04-26 05:47:57 +00:00
|
|
|
if h.Username = strings.TrimSpace(h.Username); h.Username == "" {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "V2-zzad3", "Errors.Invalid.Argument")
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
if h.FirstName = strings.TrimSpace(h.FirstName); h.FirstName == "" {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "USER-UCej2", "Errors.User.Profile.FirstNameEmpty")
|
|
|
|
}
|
|
|
|
if h.LastName = strings.TrimSpace(h.LastName); h.LastName == "" {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "USER-4hB7d", "Errors.User.Profile.LastNameEmpty")
|
|
|
|
}
|
|
|
|
h.ensureDisplayName()
|
2022-07-28 13:42:35 +00:00
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
if h.Phone.Number != "" {
|
|
|
|
if h.Phone.Number, err = h.Phone.Number.Normalize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-19 17:46:26 +00:00
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
for _, metadataEntry := range h.Metadata {
|
|
|
|
if err := metadataEntry.Valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
}
|
2023-07-14 06:49:57 +00:00
|
|
|
if h.EncodedPasswordHash != "" {
|
|
|
|
if !hasher.EncodingSupported(h.EncodedPasswordHash) {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "USER-JDk4t", "Errors.User.Password.NotSupported")
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 05:47:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
type AddMetadataEntry struct {
|
|
|
|
Key string
|
|
|
|
Value []byte
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
func (m *AddMetadataEntry) Valid() error {
|
|
|
|
if m.Key = strings.TrimSpace(m.Key); m.Key == "" {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "USER-Drght", "Errors.User.Metadata.KeyEmpty")
|
|
|
|
}
|
|
|
|
if len(m.Value) == 0 {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "USER-Dbgth", "Errors.User.Metadata.ValueEmpty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) AddHuman(ctx context.Context, resourceOwner string, human *AddHuman, allowInitMail bool) (err error) {
|
2022-07-28 13:42:35 +00:00
|
|
|
if resourceOwner == "" {
|
2023-04-26 05:47:57 +00:00
|
|
|
return errors.ThrowInvalidArgument(nil, "COMMA-5Ky74", "Errors.Internal")
|
|
|
|
}
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter,
|
|
|
|
c.AddHumanCommand(
|
|
|
|
human,
|
2023-04-26 14:19:32 +00:00
|
|
|
resourceOwner,
|
2023-07-14 06:49:57 +00:00
|
|
|
c.userPasswordHasher,
|
2023-04-26 05:47:57 +00:00
|
|
|
c.userEncryption,
|
|
|
|
allowInitMail,
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
2023-04-26 05:47:57 +00:00
|
|
|
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
2022-07-28 13:42:35 +00:00
|
|
|
if err != nil {
|
2023-04-26 05:47:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
human.Details = &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
|
|
|
EventDate: events[len(events)-1].CreationDate(),
|
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().ResourceOwner,
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
return nil
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
type humanCreationCommand interface {
|
|
|
|
eventstore.Command
|
2023-03-14 19:20:38 +00:00
|
|
|
AddPhoneData(phoneNumber domain.PhoneNumber)
|
2023-07-14 06:49:57 +00:00
|
|
|
AddPasswordData(encoded string, changeRequired bool)
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
//nolint:gocognit
|
|
|
|
func (c *Commands) AddHumanCommand(human *AddHuman, orgID string, hasher *crypto.PasswordHasher, codeAlg crypto.EncryptionAlgorithm, allowInitMail bool) preparation.Validation {
|
2022-04-12 14:20:17 +00:00
|
|
|
return func() (_ preparation.CreateCommands, err error) {
|
2023-07-14 06:49:57 +00:00
|
|
|
if err := human.Validate(hasher); err != nil {
|
2023-03-14 19:20:38 +00:00
|
|
|
return nil, err
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
2023-04-26 14:19:32 +00:00
|
|
|
if err := c.addHumanCommandCheckID(ctx, filter, human, orgID); err != nil {
|
2023-03-14 19:20:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-26 14:19:32 +00:00
|
|
|
a := user.NewAggregate(human.ID, orgID)
|
2022-04-12 14:20:17 +00:00
|
|
|
|
2022-12-13 06:37:50 +00:00
|
|
|
domainPolicy, err := domainPolicyWriteModel(ctx, filter, a.ResourceOwner)
|
2022-04-12 14:20:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = userValidateDomain(ctx, a, human.Username, domainPolicy.UserLoginMustBeDomain, filter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var createCmd humanCreationCommand
|
|
|
|
if human.Register {
|
|
|
|
createCmd = user.NewHumanRegisteredEvent(
|
|
|
|
ctx,
|
|
|
|
&a.Aggregate,
|
|
|
|
human.Username,
|
|
|
|
human.FirstName,
|
|
|
|
human.LastName,
|
|
|
|
human.NickName,
|
|
|
|
human.DisplayName,
|
2022-04-28 08:30:41 +00:00
|
|
|
human.PreferredLanguage,
|
2022-04-12 14:20:17 +00:00
|
|
|
human.Gender,
|
|
|
|
human.Email.Address,
|
|
|
|
domainPolicy.UserLoginMustBeDomain,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
createCmd = user.NewHumanAddedEvent(
|
|
|
|
ctx,
|
|
|
|
&a.Aggregate,
|
|
|
|
human.Username,
|
|
|
|
human.FirstName,
|
|
|
|
human.LastName,
|
|
|
|
human.NickName,
|
|
|
|
human.DisplayName,
|
2022-04-28 08:30:41 +00:00
|
|
|
human.PreferredLanguage,
|
2022-04-12 14:20:17 +00:00
|
|
|
human.Gender,
|
|
|
|
human.Email.Address,
|
|
|
|
domainPolicy.UserLoginMustBeDomain,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if human.Phone.Number != "" {
|
|
|
|
createCmd.AddPhoneData(human.Phone.Number)
|
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
if err := addHumanCommandPassword(ctx, filter, createCmd, human, hasher); err != nil {
|
2023-04-26 05:47:57 +00:00
|
|
|
return nil, err
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
cmds := make([]eventstore.Command, 0, 3)
|
|
|
|
cmds = append(cmds, createCmd)
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
cmds, err = c.addHumanCommandEmail(ctx, filter, cmds, a, human, codeAlg, allowInitMail)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-12-07 11:16:01 +00:00
|
|
|
}
|
2023-04-26 05:47:57 +00:00
|
|
|
|
|
|
|
cmds, err = c.addHumanCommandPhone(ctx, filter, cmds, a, human, codeAlg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
for _, metadataEntry := range human.Metadata {
|
|
|
|
cmds = append(cmds, user.NewMetadataSetEvent(
|
|
|
|
ctx,
|
|
|
|
&a.Aggregate,
|
|
|
|
metadataEntry.Key,
|
|
|
|
metadataEntry.Value,
|
|
|
|
))
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
2023-05-24 18:29:58 +00:00
|
|
|
for _, link := range human.Links {
|
|
|
|
cmd, err := addLink(ctx, filter, a, link)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
|
|
|
|
return cmds, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
func (c *Commands) addHumanCommandEmail(ctx context.Context, filter preparation.FilterToQueryReducer, cmds []eventstore.Command, a *user.Aggregate, human *AddHuman, codeAlg crypto.EncryptionAlgorithm, allowInitMail bool) ([]eventstore.Command, error) {
|
|
|
|
if human.Email.Verified {
|
|
|
|
cmds = append(cmds, user.NewHumanEmailVerifiedEvent(ctx, &a.Aggregate))
|
|
|
|
}
|
|
|
|
// if allowInitMail, used for v1 api (system, admin, mgmt, auth):
|
|
|
|
// add init code if
|
|
|
|
// email not verified or
|
|
|
|
// user not registered and password set
|
|
|
|
if allowInitMail && human.shouldAddInitCode() {
|
2023-07-10 08:07:10 +00:00
|
|
|
initCode, err := c.newUserInitCode(ctx, filter, codeAlg)
|
2023-04-26 05:47:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return append(cmds, user.NewHumanInitialCodeAddedEvent(ctx, &a.Aggregate, initCode.Crypted, initCode.Expiry)), nil
|
|
|
|
}
|
|
|
|
if !human.Email.Verified {
|
|
|
|
emailCode, err := c.newEmailCode(ctx, filter, codeAlg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if human.Email.ReturnCode {
|
|
|
|
human.EmailCode = &emailCode.Plain
|
|
|
|
}
|
|
|
|
return append(cmds, user.NewHumanEmailCodeAddedEventV2(ctx, &a.Aggregate, emailCode.Crypted, emailCode.Expiry, human.Email.URLTemplate, human.Email.ReturnCode)), nil
|
|
|
|
}
|
|
|
|
return cmds, nil
|
|
|
|
}
|
2023-05-24 18:29:58 +00:00
|
|
|
|
|
|
|
func addLink(ctx context.Context, filter preparation.FilterToQueryReducer, a *user.Aggregate, link *AddLink) (eventstore.Command, error) {
|
|
|
|
exists, err := ExistsIDP(ctx, filter, link.IDPID, a.ResourceOwner)
|
|
|
|
if !exists || err != nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(err, "COMMAND-39nf2", "Errors.IDPConfig.NotExisting")
|
|
|
|
}
|
|
|
|
return user.NewUserIDPLinkAddedEvent(ctx, &a.Aggregate, link.IDPID, link.DisplayName, link.IDPExternalID), nil
|
|
|
|
}
|
|
|
|
|
2023-04-26 05:47:57 +00:00
|
|
|
func (c *Commands) addHumanCommandPhone(ctx context.Context, filter preparation.FilterToQueryReducer, cmds []eventstore.Command, a *user.Aggregate, human *AddHuman, codeAlg crypto.EncryptionAlgorithm) ([]eventstore.Command, error) {
|
|
|
|
if human.Phone.Number == "" {
|
|
|
|
return cmds, nil
|
|
|
|
}
|
|
|
|
if human.Phone.Verified {
|
|
|
|
return append(cmds, user.NewHumanPhoneVerifiedEvent(ctx, &a.Aggregate)), nil
|
|
|
|
}
|
2023-07-10 08:07:10 +00:00
|
|
|
phoneCode, err := c.newPhoneCode(ctx, filter, codeAlg)
|
2023-04-26 05:47:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 04:42:59 +00:00
|
|
|
if human.Phone.ReturnCode {
|
|
|
|
human.PhoneCode = &phoneCode.Plain
|
|
|
|
}
|
|
|
|
return append(cmds, user.NewHumanPhoneCodeAddedEventV2(ctx, &a.Aggregate, phoneCode.Crypted, phoneCode.Expiry, human.Phone.ReturnCode)), nil
|
2023-04-26 05:47:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 14:19:32 +00:00
|
|
|
func (c *Commands) addHumanCommandCheckID(ctx context.Context, filter preparation.FilterToQueryReducer, human *AddHuman, orgID string) (err error) {
|
|
|
|
if human.ID == "" {
|
|
|
|
human.ID, err = c.idGenerator.Next()
|
2023-04-26 05:47:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 14:19:32 +00:00
|
|
|
existingHuman, err := humanWriteModelByID(ctx, filter, human.ID, orgID)
|
2023-04-26 05:47:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-26 14:19:32 +00:00
|
|
|
if isUserStateExists(existingHuman.UserState) {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "COMMAND-k2unb", "Errors.User.AlreadyExisting")
|
|
|
|
}
|
2023-04-26 05:47:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
func addHumanCommandPassword(ctx context.Context, filter preparation.FilterToQueryReducer, createCmd humanCreationCommand, human *AddHuman, hasher *crypto.PasswordHasher) (err error) {
|
2023-04-26 05:47:57 +00:00
|
|
|
if human.Password != "" {
|
|
|
|
if err = humanValidatePassword(ctx, filter, human.Password); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
secret, err := hasher.Hash(human.Password)
|
2023-04-26 05:47:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
createCmd.AddPasswordData(secret, human.PasswordChangeRequired)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
if human.EncodedPasswordHash != "" {
|
|
|
|
createCmd.AddPasswordData(human.EncodedPasswordHash, human.PasswordChangeRequired)
|
2023-04-26 05:47:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func userValidateDomain(ctx context.Context, a *user.Aggregate, username string, mustBeDomain bool, filter preparation.FilterToQueryReducer) error {
|
|
|
|
if mustBeDomain {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 11:16:17 +00:00
|
|
|
index := strings.LastIndex(username, "@")
|
|
|
|
if index < 0 {
|
|
|
|
return nil
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 11:16:17 +00:00
|
|
|
domainCheck := NewOrgDomainVerifiedWriteModel(username[index+1:])
|
2022-04-12 14:20:17 +00:00
|
|
|
events, err := filter(ctx, domainCheck.Query())
|
2021-01-08 10:33:45 +00:00
|
|
|
if err != nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
domainCheck.AppendEvents(events...)
|
|
|
|
if err = domainCheck.Reduce(); err != nil {
|
|
|
|
return err
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
if domainCheck.Verified && domainCheck.ResourceOwner != a.ResourceOwner {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "COMMAND-SFd21", "Errors.User.DomainNotAllowedAsUsername")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func humanValidatePassword(ctx context.Context, filter preparation.FilterToQueryReducer, password string) error {
|
|
|
|
passwordComplexity, err := passwordComplexityPolicyWriteModel(ctx, filter)
|
2021-01-08 10:33:45 +00:00
|
|
|
if err != nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return err
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
return passwordComplexity.Validate(password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AddHuman) ensureDisplayName() {
|
|
|
|
if strings.TrimSpace(h.DisplayName) != "" {
|
|
|
|
return
|
|
|
|
}
|
2023-04-11 15:56:51 +00:00
|
|
|
if strings.TrimSpace(h.FirstName) != "" && strings.TrimSpace(h.LastName) != "" {
|
|
|
|
h.DisplayName = h.FirstName + " " + h.LastName
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(string(h.Email.Address)) != "" {
|
|
|
|
h.DisplayName = string(h.Email.Address)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.DisplayName = h.Username
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 14:48:26 +00:00
|
|
|
// shouldAddInitCode returns true for all added Humans which:
|
2022-05-18 12:10:49 +00:00
|
|
|
// - were not added from an external IDP
|
|
|
|
// - and either:
|
2022-10-18 14:48:26 +00:00
|
|
|
// - have no verified email
|
|
|
|
// and / or
|
|
|
|
// - have no authentication method (password / passwordless)
|
2022-04-12 14:20:17 +00:00
|
|
|
func (h *AddHuman) shouldAddInitCode() bool {
|
2022-05-18 12:10:49 +00:00
|
|
|
return !h.ExternalIDP &&
|
|
|
|
!h.Email.Verified ||
|
|
|
|
!h.Passwordless &&
|
|
|
|
h.Password == ""
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 08:33:50 +00:00
|
|
|
func (c *Commands) ImportHuman(ctx context.Context, orgID string, human *domain.Human, passwordless bool, links []*domain.UserIDPLink, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator, passwordlessCodeGenerator crypto.Generator) (_ *domain.Human, passwordlessCode *domain.PasswordlessInitCode, err error) {
|
2021-03-25 13:41:07 +00:00
|
|
|
if orgID == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "COMMAND-5N8fs", "Errors.ResourceOwnerMissing")
|
2021-03-25 13:41:07 +00:00
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
domainPolicy, err := c.getOrgDomainPolicy(ctx, orgID)
|
2021-03-25 13:41:07 +00:00
|
|
|
if err != nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, nil, errors.ThrowPreconditionFailed(err, "COMMAND-2N9fs", "Errors.Org.DomainPolicy.NotFound")
|
2021-03-25 13:41:07 +00:00
|
|
|
}
|
|
|
|
pwPolicy, err := c.getOrgPasswordComplexityPolicy(ctx, orgID)
|
|
|
|
if err != nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, nil, errors.ThrowPreconditionFailed(err, "COMMAND-4N8gs", "Errors.Org.PasswordComplexityPolicy.NotFound")
|
2021-03-25 13:41:07 +00:00
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
|
|
|
|
if human.AggregateID != "" {
|
|
|
|
existing, err := c.getHumanWriteModelByID(ctx, human.AggregateID, human.ResourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing.UserState != domain.UserStateUnspecified {
|
|
|
|
return nil, nil, errors.ThrowPreconditionFailed(nil, "COMMAND-ziuna", "Errors.User.AlreadyExisting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 08:33:50 +00:00
|
|
|
events, addedHuman, addedCode, code, err := c.importHuman(ctx, orgID, human, passwordless, links, domainPolicy, pwPolicy, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator, passwordlessCodeGenerator)
|
2021-03-25 13:41:07 +00:00
|
|
|
if err != nil {
|
2021-08-02 13:24:58 +00:00
|
|
|
return nil, nil, err
|
2021-03-25 13:41:07 +00:00
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events...)
|
2021-03-25 13:41:07 +00:00
|
|
|
if err != nil {
|
2021-08-02 13:24:58 +00:00
|
|
|
return nil, nil, err
|
2021-03-25 13:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = AppendAndReduce(addedHuman, pushedEvents...)
|
|
|
|
if err != nil {
|
2021-08-02 13:24:58 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if addedCode != nil {
|
|
|
|
err = AppendAndReduce(addedCode, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
passwordlessCode = writeModelToPasswordlessInitCode(addedCode, code)
|
2021-03-25 13:41:07 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 13:24:58 +00:00
|
|
|
return writeModelToHuman(addedHuman), passwordlessCode, nil
|
2021-03-25 13:41:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 14:48:26 +00:00
|
|
|
func (c *Commands) RegisterHuman(ctx context.Context, orgID string, human *domain.Human, link *domain.UserIDPLink, orgMemberRoles []string, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator crypto.Generator) (*domain.Human, error) {
|
2021-08-25 09:12:24 +00:00
|
|
|
if orgID == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "COMMAND-GEdf2", "Errors.ResourceOwnerMissing")
|
2021-08-18 15:04:56 +00:00
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
domainPolicy, err := c.getOrgDomainPolicy(ctx, orgID)
|
2021-08-18 15:04:56 +00:00
|
|
|
if err != nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(err, "COMMAND-33M9f", "Errors.Org.DomainPolicy.NotFound")
|
2021-08-18 15:04:56 +00:00
|
|
|
}
|
|
|
|
pwPolicy, err := c.getOrgPasswordComplexityPolicy(ctx, orgID)
|
|
|
|
if err != nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(err, "COMMAND-M5Fsd", "Errors.Org.PasswordComplexityPolicy.NotFound")
|
2021-08-18 15:04:56 +00:00
|
|
|
}
|
2021-11-08 07:42:07 +00:00
|
|
|
loginPolicy, err := c.getOrgLoginPolicy(ctx, orgID)
|
|
|
|
if err != nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(err, "COMMAND-Dfg3g", "Errors.Org.LoginPolicy.NotFound")
|
2021-11-08 07:42:07 +00:00
|
|
|
}
|
2023-08-23 11:55:52 +00:00
|
|
|
// check only if local registration is allowed, the idp will be checked separately
|
|
|
|
if !loginPolicy.AllowRegister && link == nil {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(err, "COMMAND-SAbr3", "Errors.Org.LoginPolicy.RegistrationNotAllowed")
|
2021-11-08 07:42:07 +00:00
|
|
|
}
|
2022-10-18 14:48:26 +00:00
|
|
|
userEvents, registeredHuman, err := c.registerHuman(ctx, orgID, human, link, domainPolicy, pwPolicy, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
orgMemberWriteModel := NewOrgMemberWriteModel(orgID, registeredHuman.AggregateID)
|
2021-02-08 10:30:30 +00:00
|
|
|
orgAgg := OrgAggregateFromWriteModel(&orgMemberWriteModel.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
if len(orgMemberRoles) > 0 {
|
2021-02-08 10:30:30 +00:00
|
|
|
orgMember := &domain.Member{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: orgID,
|
|
|
|
},
|
2021-02-18 13:48:27 +00:00
|
|
|
UserID: human.AggregateID,
|
2021-02-08 10:30:30 +00:00
|
|
|
Roles: orgMemberRoles,
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
memberEvent, err := c.addOrgMember(ctx, orgAgg, orgMemberWriteModel, orgMember)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userEvents = append(userEvents, memberEvent)
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, userEvents...)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
err = AppendAndReduce(registeredHuman, pushedEvents...)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
return writeModelToHuman(registeredHuman), nil
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 08:33:50 +00:00
|
|
|
func (c *Commands) importHuman(ctx context.Context, orgID string, human *domain.Human, passwordless bool, links []*domain.UserIDPLink, domainPolicy *domain.DomainPolicy, pwPolicy *domain.PasswordComplexityPolicy, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator, passwordlessCodeGenerator crypto.Generator) (events []eventstore.Command, humanWriteModel *HumanWriteModel, passwordlessCodeWriteModel *HumanPasswordlessInitCodeWriteModel, code string, err error) {
|
2023-03-14 19:20:38 +00:00
|
|
|
if orgID == "" {
|
|
|
|
return nil, nil, nil, "", errors.ThrowInvalidArgument(nil, "COMMAND-00p2b", "Errors.Org.Empty")
|
|
|
|
}
|
|
|
|
if err := human.Normalize(); err != nil {
|
|
|
|
return nil, nil, nil, "", err
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
2022-11-09 08:33:50 +00:00
|
|
|
events, humanWriteModel, err = c.createHuman(ctx, orgID, human, links, false, passwordless, domainPolicy, pwPolicy, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator)
|
2022-04-12 14:20:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, "", err
|
|
|
|
}
|
|
|
|
if passwordless {
|
|
|
|
var codeEvent eventstore.Command
|
|
|
|
codeEvent, passwordlessCodeWriteModel, code, err = c.humanAddPasswordlessInitCode(ctx, human.AggregateID, orgID, true, passwordlessCodeGenerator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, "", err
|
|
|
|
}
|
|
|
|
events = append(events, codeEvent)
|
|
|
|
}
|
|
|
|
return events, humanWriteModel, passwordlessCodeWriteModel, code, nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 14:48:26 +00:00
|
|
|
func (c *Commands) registerHuman(ctx context.Context, orgID string, human *domain.Human, link *domain.UserIDPLink, domainPolicy *domain.DomainPolicy, pwPolicy *domain.PasswordComplexityPolicy, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator crypto.Generator) ([]eventstore.Command, *HumanWriteModel, error) {
|
2022-08-19 13:00:14 +00:00
|
|
|
if human == nil {
|
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "COMMAND-JKefw", "Errors.User.Invalid")
|
|
|
|
}
|
|
|
|
if human.Username = strings.TrimSpace(human.Username); human.Username == "" {
|
2023-03-14 19:20:38 +00:00
|
|
|
human.Username = string(human.EmailAddress)
|
|
|
|
}
|
|
|
|
if orgID == "" {
|
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "COMMAND-hYsVH", "Errors.Org.Empty")
|
|
|
|
}
|
|
|
|
if err := human.Normalize(); err != nil {
|
|
|
|
return nil, nil, err
|
2021-05-17 11:39:51 +00:00
|
|
|
}
|
2023-03-14 19:20:38 +00:00
|
|
|
if link == nil && (human.Password == nil || human.Password.SecretString == "") {
|
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "COMMAND-X23na", "Errors.User.Password.Empty")
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
if human.Password != nil && human.Password.SecretString != "" {
|
|
|
|
human.Password.ChangeRequired = false
|
2021-03-25 13:41:07 +00:00
|
|
|
}
|
2022-11-09 08:33:50 +00:00
|
|
|
var links []*domain.UserIDPLink
|
|
|
|
if link != nil {
|
|
|
|
links = append(links, link)
|
|
|
|
}
|
|
|
|
return c.createHuman(ctx, orgID, human, links, true, false, domainPolicy, pwPolicy, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator)
|
2021-03-19 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 08:33:50 +00:00
|
|
|
func (c *Commands) createHuman(ctx context.Context, orgID string, human *domain.Human, links []*domain.UserIDPLink, selfregister, passwordless bool, domainPolicy *domain.DomainPolicy, pwPolicy *domain.PasswordComplexityPolicy, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator crypto.Generator) (events []eventstore.Command, addedHuman *HumanWriteModel, err error) {
|
2022-03-28 08:05:09 +00:00
|
|
|
if err := human.CheckDomainPolicy(domainPolicy); err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2022-08-19 13:00:14 +00:00
|
|
|
human.Username = strings.TrimSpace(human.Username)
|
2023-03-14 19:20:38 +00:00
|
|
|
human.EmailAddress = human.EmailAddress.Normalize()
|
2022-03-28 08:05:09 +00:00
|
|
|
if !domainPolicy.UserLoginMustBeDomain {
|
2022-12-22 11:16:17 +00:00
|
|
|
index := strings.LastIndex(human.Username, "@")
|
|
|
|
if index > 1 {
|
|
|
|
domainCheck := NewOrgDomainVerifiedWriteModel(human.Username[index+1:])
|
|
|
|
if err := c.eventstore.FilterToQueryReducer(ctx, domainCheck); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if domainCheck.Verified && domainCheck.ResourceOwner != orgID {
|
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "COMMAND-SFd21", "Errors.User.DomainNotAllowedAsUsername")
|
|
|
|
}
|
2021-08-25 09:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
|
|
|
|
if human.AggregateID == "" {
|
|
|
|
userID, err := c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
human.AggregateID = userID
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
|
2023-04-11 15:56:51 +00:00
|
|
|
human.EnsureDisplayName()
|
2021-03-25 13:41:07 +00:00
|
|
|
if human.Password != nil {
|
2023-07-14 06:49:57 +00:00
|
|
|
if err := human.HashPasswordIfExisting(pwPolicy, c.userPasswordHasher, human.Password.ChangeRequired); err != nil {
|
2021-03-25 13:41:07 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-03-25 13:41:07 +00:00
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
addedHuman = NewHumanWriteModel(human.AggregateID, orgID)
|
2021-02-18 13:48:27 +00:00
|
|
|
//TODO: adlerhurst maybe we could simplify the code below
|
2021-01-04 13:52:13 +00:00
|
|
|
userAgg := UserAggregateFromWriteModel(&addedHuman.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
if selfregister {
|
2022-03-28 08:05:09 +00:00
|
|
|
events = append(events, createRegisterHumanEvent(ctx, userAgg, human, domainPolicy.UserLoginMustBeDomain))
|
2021-01-15 08:32:59 +00:00
|
|
|
} else {
|
2022-03-28 08:05:09 +00:00
|
|
|
events = append(events, createAddHumanEvent(ctx, userAgg, human, domainPolicy.UserLoginMustBeDomain))
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 08:33:50 +00:00
|
|
|
for _, link := range links {
|
2023-08-23 11:55:52 +00:00
|
|
|
event, err := c.addUserIDPLink(ctx, userAgg, link, false)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
events = append(events, event)
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2022-11-09 08:33:50 +00:00
|
|
|
if human.IsInitialState(passwordless, len(links) > 0) {
|
2022-02-16 15:49:17 +00:00
|
|
|
initCode, err := domain.NewInitUserCode(initCodeGenerator)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
events = append(events, user.NewHumanInitialCodeAddedEvent(ctx, userAgg, initCode.Code, initCode.Expiry))
|
2022-10-18 14:48:26 +00:00
|
|
|
} else {
|
|
|
|
if human.Email != nil && human.EmailAddress != "" && human.IsEmailVerified {
|
|
|
|
events = append(events, user.NewHumanEmailVerifiedEvent(ctx, userAgg))
|
|
|
|
} else {
|
2023-04-25 07:02:29 +00:00
|
|
|
emailCode, _, err := domain.NewEmailCode(emailCodeGenerator)
|
2022-10-18 14:48:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
events = append(events, user.NewHumanEmailCodeAddedEvent(ctx, userAgg, emailCode.Code, emailCode.Expiry))
|
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
if human.Phone != nil && human.PhoneNumber != "" && !human.IsPhoneVerified {
|
2022-02-16 15:49:17 +00:00
|
|
|
phoneCode, err := domain.NewPhoneCode(phoneCodeGenerator)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
2021-01-08 10:33:45 +00:00
|
|
|
return nil, nil, err
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
events = append(events, user.NewHumanPhoneCodeAddedEvent(ctx, userAgg, phoneCode.Code, phoneCode.Expiry))
|
2021-01-06 10:12:56 +00:00
|
|
|
} else if human.Phone != nil && human.PhoneNumber != "" && human.IsPhoneVerified {
|
2021-02-18 13:48:27 +00:00
|
|
|
events = append(events, user.NewHumanPhoneVerifiedEvent(ctx, userAgg))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
return events, addedHuman, nil
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanSkipMFAInit(ctx context.Context, userID, resourceowner string) (err error) {
|
2021-01-15 08:32:59 +00:00
|
|
|
if userID == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return errors.ThrowInvalidArgument(nil, "COMMAND-2xpX9", "Errors.User.UserIDMissing")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingHuman, err := c.getHumanWriteModelByID(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-02-18 13:48:27 +00:00
|
|
|
if !isUserStateExists(existingHuman.UserState) {
|
2022-04-12 14:20:17 +00:00
|
|
|
return errors.ThrowNotFound(nil, "COMMAND-m9cV8", "Errors.User.NotFound")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
_, err = c.eventstore.Push(ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.NewHumanMFAInitSkippedEvent(ctx, UserAggregateFromWriteModel(&existingHuman.WriteModel)))
|
|
|
|
return err
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
|
2022-10-18 14:48:26 +00:00
|
|
|
// TODO: adlerhurst maybe we can simplify createAddHumanEvent and createRegisterHumanEvent
|
2021-02-18 13:48:27 +00:00
|
|
|
func createAddHumanEvent(ctx context.Context, aggregate *eventstore.Aggregate, human *domain.Human, userLoginMustBeDomain bool) *user.HumanAddedEvent {
|
2021-01-15 08:32:59 +00:00
|
|
|
addEvent := user.NewHumanAddedEvent(
|
2021-01-06 10:12:56 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-21 09:49:38 +00:00
|
|
|
human.Username,
|
2021-01-06 10:12:56 +00:00
|
|
|
human.FirstName,
|
|
|
|
human.LastName,
|
|
|
|
human.NickName,
|
|
|
|
human.DisplayName,
|
|
|
|
human.PreferredLanguage,
|
|
|
|
human.Gender,
|
|
|
|
human.EmailAddress,
|
2021-01-21 09:49:38 +00:00
|
|
|
userLoginMustBeDomain,
|
2021-01-06 10:12:56 +00:00
|
|
|
)
|
|
|
|
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 {
|
2023-07-14 06:49:57 +00:00
|
|
|
addEvent.AddPasswordData(human.Password.EncodedSecret, human.Password.ChangeRequired)
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
2023-07-14 06:49:57 +00:00
|
|
|
if human.HashedPassword != "" {
|
|
|
|
addEvent.AddPasswordData(human.HashedPassword, false)
|
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-02-18 13:48:27 +00:00
|
|
|
func createRegisterHumanEvent(ctx context.Context, aggregate *eventstore.Aggregate, human *domain.Human, userLoginMustBeDomain bool) *user.HumanRegisteredEvent {
|
2021-01-15 08:32:59 +00:00
|
|
|
addEvent := user.NewHumanRegisteredEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-21 09:49:38 +00:00
|
|
|
human.Username,
|
2021-01-15 08:32:59 +00:00
|
|
|
human.FirstName,
|
|
|
|
human.LastName,
|
|
|
|
human.NickName,
|
|
|
|
human.DisplayName,
|
|
|
|
human.PreferredLanguage,
|
|
|
|
human.Gender,
|
|
|
|
human.EmailAddress,
|
2021-01-21 09:49:38 +00:00
|
|
|
userLoginMustBeDomain,
|
2021-01-15 08:32:59 +00:00
|
|
|
)
|
|
|
|
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 {
|
2023-07-14 06:49:57 +00:00
|
|
|
addEvent.AddPasswordData(human.Password.EncodedSecret, human.Password.ChangeRequired)
|
2022-07-28 13:42:35 +00:00
|
|
|
}
|
2023-07-14 06:49:57 +00:00
|
|
|
if human.HashedPassword != "" {
|
|
|
|
addEvent.AddPasswordData(human.HashedPassword, false)
|
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-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumansSignOut(ctx context.Context, agentID string, userIDs []string) error {
|
2021-02-08 10:30:30 +00:00
|
|
|
if agentID == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return errors.ThrowInvalidArgument(nil, "COMMAND-2M0ds", "Errors.User.UserIDMissing")
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
if len(userIDs) == 0 {
|
2022-04-12 14:20:17 +00:00
|
|
|
return errors.ThrowInvalidArgument(nil, "COMMAND-M0od3", "Errors.User.UserIDMissing")
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
events := make([]eventstore.Command, 0)
|
2021-03-19 10:12:56 +00:00
|
|
|
for _, userID := range userIDs {
|
2021-02-24 10:17:39 +00:00
|
|
|
existingUser, err := c.getHumanWriteModelByID(ctx, userID, "")
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
if !isUserStateExists(existingUser.UserState) {
|
2021-02-08 10:30:30 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
events = append(events, user.NewHumanSignedOutEvent(
|
2021-02-18 13:48:27 +00:00
|
|
|
ctx,
|
|
|
|
UserAggregateFromWriteModel(&existingUser.WriteModel),
|
2021-03-19 10:12:56 +00:00
|
|
|
agentID))
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return nil
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
_, err := c.eventstore.Push(ctx, events...)
|
2021-02-08 10:30:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) getHumanWriteModelByID(ctx context.Context, userID, resourceowner string) (*HumanWriteModel, error) {
|
2021-01-15 08:32:59 +00:00
|
|
|
humanWriteModel := NewHumanWriteModel(userID, resourceowner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.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
|
|
|
}
|
2023-04-26 05:47:57 +00:00
|
|
|
|
|
|
|
func humanWriteModelByID(ctx context.Context, filter preparation.FilterToQueryReducer, userID, resourceowner string) (*HumanWriteModel, error) {
|
|
|
|
humanWriteModel := NewHumanWriteModel(userID, resourceowner)
|
|
|
|
events, err := filter(ctx, humanWriteModel.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return humanWriteModel, nil
|
|
|
|
}
|
|
|
|
humanWriteModel.AppendEvents(events...)
|
|
|
|
err = humanWriteModel.Reduce()
|
|
|
|
return humanWriteModel, err
|
|
|
|
}
|