2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/user"
|
|
|
|
)
|
|
|
|
|
2021-01-07 15:06:45 +00:00
|
|
|
func (r *CommandSide) ChangeUsername(ctx context.Context, orgID, userID, userName string) error {
|
|
|
|
if orgID == "" || userID == "" || userName == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2N9fs", "Errors.IDMissing")
|
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID, orgID)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-5N9ds", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
if existingUser.UserName == userName {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M9fs", "Errors.User.UsernameNotChanged")
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
orgIAMPolicy, err := r.getOrgIAMPolicy(ctx, orgID)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := CheckOrgIAMPolicyForUserName(userName, orgIAMPolicy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
2021-01-21 09:49:38 +00:00
|
|
|
userAgg.PushEvents(user.NewUsernameChangedEvent(ctx, existingUser.UserName, userName, orgIAMPolicy.UserLoginMustBeDomain))
|
2021-01-07 15:06:45 +00:00
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) DeactivateUser(ctx context.Context, userID, resourceOwner string) error {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-m0gDf", "Errors.User.UserIDMissing")
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID, resourceOwner)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
2021-01-15 08:32:59 +00:00
|
|
|
return err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-3M9ds", "Errors.User.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if existingUser.UserState == domain.UserStateInactive {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-5M0sf", "Errors.User.AlreadyInactive")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserDeactivatedEvent(ctx))
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
return r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) ReactivateUser(ctx context.Context, userID, resourceOwner string) error {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-4M9ds", "Errors.User.UserIDMissing")
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID, resourceOwner)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
2021-01-15 08:32:59 +00:00
|
|
|
return err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-4M0sd", "Errors.User.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if existingUser.UserState != domain.UserStateInactive {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-6M0sf", "Errors.User.NotInactive")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserReactivatedEvent(ctx))
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
return r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) LockUser(ctx context.Context, userID, resourceOwner string) error {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M0sd", "Errors.User.UserIDMissing")
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID, resourceOwner)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
2021-01-15 08:32:59 +00:00
|
|
|
return err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-5M9fs", "Errors.User.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if existingUser.UserState != domain.UserStateActive && existingUser.UserState != domain.UserStateInitial {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M9fs", "Errors.User.ShouldBeActiveOrInitial")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserLockedEvent(ctx))
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
return r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) UnlockUser(ctx context.Context, userID, resourceOwner string) error {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-M0dse", "Errors.User.UserIDMissing")
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID, resourceOwner)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
2021-01-15 08:32:59 +00:00
|
|
|
return err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-M0dos", "Errors.User.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if existingUser.UserState != domain.UserStateLocked {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-4M0ds", "Errors.User.NotLocked")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserUnlockedEvent(ctx))
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
return r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func (r *CommandSide) RemoveUser(ctx context.Context, userID, resourceOwner string) error {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M0ds", "Errors.User.UserIDMissing")
|
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID, resourceOwner)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-5M0od", "Errors.User.NotFound")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-01-21 09:49:38 +00:00
|
|
|
orgIAMPolicy, err := r.getOrgIAMPolicy(ctx, existingUser.ResourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
2021-01-21 09:49:38 +00:00
|
|
|
userAgg.PushEvents(user.NewUserRemovedEvent(ctx, existingUser.ResourceOwner, existingUser.UserName, orgIAMPolicy.UserLoginMustBeDomain))
|
2021-01-07 15:06:45 +00:00
|
|
|
//TODO: remove user grants
|
2021-01-06 10:12:56 +00:00
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
func (r *CommandSide) checkUserExists(ctx context.Context, userID, resourceOwner string) error {
|
2021-01-18 10:24:15 +00:00
|
|
|
userWriteModel, err := r.userWriteModelByID(ctx, userID, resourceOwner)
|
|
|
|
if err != nil {
|
2021-01-28 05:35:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if userWriteModel.UserState == domain.UserStateUnspecified || userWriteModel.UserState == domain.UserStateDeleted {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-4M0fs", "Errors.User.NotFound")
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func (r *CommandSide) userWriteModelByID(ctx context.Context, userID, resourceOwner string) (writeModel *UserWriteModel, err error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
writeModel = NewUserWriteModel(userID, resourceOwner)
|
2021-01-04 13:52:13 +00:00
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
|
|
|
|
func (r *CommandSide) userReadModelByID(ctx context.Context, userID, resourceOwner string) (writeModel *UserWriteModel, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
writeModel = NewUserWriteModel(userID, resourceOwner)
|
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|