2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
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) AddUser(ctx context.Context, orgID string, user *domain.User) (*domain.User, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
if !user.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2N9fs", "Errors.User.Invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Human != nil {
|
2021-01-07 15:06:45 +00:00
|
|
|
human, err := r.AddHuman(ctx, orgID, user.UserName, user.Human)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-05 08:33:45 +00:00
|
|
|
return &domain.User{UserName: user.UserName, Human: human}, nil
|
2021-01-04 13:52:13 +00:00
|
|
|
} else if user.Machine != nil {
|
2021-01-07 15:06:45 +00:00
|
|
|
machine, err := r.AddMachine(ctx, orgID, user.UserName, user.Machine)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &domain.User{UserName: user.UserName, Machine: machine}, nil
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-8K0df", "Errors.User.TypeUndefined")
|
|
|
|
}
|
|
|
|
|
2021-01-07 15:06:45 +00:00
|
|
|
func (r *CommandSide) RegisterUser(ctx context.Context, orgID string, user *domain.User) (*domain.User, error) {
|
2021-01-05 08:33:45 +00:00
|
|
|
if !user.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2N9fs", "Errors.User.Invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Human != nil {
|
2021-01-07 15:06:45 +00:00
|
|
|
human, err := r.RegisterHuman(ctx, orgID, user.UserName, user.Human, nil)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &domain.User{UserName: user.UserName, Human: human}, nil
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-8K0df", "Errors.User.TypeUndefined")
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
orgIAMPolicy, err := r.GetOrgIAMPolicy(ctx, orgID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := CheckOrgIAMPolicyForUserName(userName, orgIAMPolicy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUsernameChangedEvent(ctx, userName))
|
|
|
|
//TODO: Check Uniqueness
|
|
|
|
//TODO: release old username, set new unique username
|
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) DeactivateUser(ctx context.Context, userID string) (*domain.User, error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-m0gDf", "Errors.User.UserIDMissing")
|
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-3M9ds", "Errors.User.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if existingUser.UserState == domain.UserStateInactive {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-5M0sf", "Errors.User.AlreadyInactive")
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserDeactivatedEvent(ctx))
|
|
|
|
|
|
|
|
err = r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToUser(existingUser), nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) ReactivateUser(ctx context.Context, userID string) (*domain.User, error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-4M9ds", "Errors.User.UserIDMissing")
|
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-4M0sd", "Errors.User.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if existingUser.UserState != domain.UserStateInactive {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-6M0sf", "Errors.User.NotInactive")
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserReactivatedEvent(ctx))
|
|
|
|
|
|
|
|
err = r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToUser(existingUser), nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) LockUser(ctx context.Context, userID string) (*domain.User, error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M0sd", "Errors.User.UserIDMissing")
|
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
|
|
|
return nil, 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 {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M9fs", "Errors.User.ShouldBeActiveOrInitial")
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserLockedEvent(ctx))
|
|
|
|
|
|
|
|
err = r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToUser(existingUser), nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) UnlockUser(ctx context.Context, userID string) (*domain.User, error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-M0dse", "Errors.User.UserIDMissing")
|
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingUser.UserState == domain.UserStateUnspecified || existingUser.UserState == domain.UserStateDeleted {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-M0dos", "Errors.User.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if existingUser.UserState != domain.UserStateLocked {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-4M0ds", "Errors.User.NotLocked")
|
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserUnlockedEvent(ctx))
|
|
|
|
|
|
|
|
err = r.eventstore.PushAggregate(ctx, existingUser, userAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToUser(existingUser), nil
|
|
|
|
}
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
func (r *CommandSide) RemoveUser(ctx context.Context, userID string) error {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2M0ds", "Errors.User.UserIDMissing")
|
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
existingUser, err := r.userWriteModelByID(ctx, userID)
|
|
|
|
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
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&existingUser.WriteModel)
|
|
|
|
userAgg.PushEvents(user.NewUserRemovedEvent(ctx))
|
|
|
|
//TODO: release unqie username
|
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-04 13:52:13 +00:00
|
|
|
func (r *CommandSide) userWriteModelByID(ctx context.Context, userID string) (writeModel *UserWriteModel, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
writeModel = NewUserWriteModel(userID)
|
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|