2024-09-20 09:14:51 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2024-09-27 17:52:18 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2024-09-20 09:14:51 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AddUsername struct {
|
|
|
|
ResourceOwner string
|
|
|
|
UserID string
|
|
|
|
|
2024-09-27 17:52:18 +02:00
|
|
|
Username *Username
|
|
|
|
}
|
|
|
|
|
|
|
|
type Username struct {
|
2024-09-20 09:14:51 +02:00
|
|
|
Username string
|
|
|
|
IsOrgSpecific bool
|
|
|
|
}
|
|
|
|
|
2024-09-27 17:52:18 +02:00
|
|
|
func (c *Commands) AddUsername(ctx context.Context, add *AddUsername) (*domain.ObjectDetails, error) {
|
|
|
|
if add.UserID == "" {
|
2024-09-25 19:58:26 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-aS3Vz5t6BS", "Errors.IDMissing")
|
|
|
|
}
|
2024-09-27 17:52:18 +02:00
|
|
|
schemauser, err := existingSchemaUser(ctx, c, add.ResourceOwner, add.UserID)
|
2024-09-20 09:14:51 +02:00
|
|
|
if err != nil {
|
2024-09-23 17:32:24 +02:00
|
|
|
return nil, err
|
2024-09-20 09:14:51 +02:00
|
|
|
}
|
2024-09-27 17:52:18 +02:00
|
|
|
add.ResourceOwner = schemauser.ResourceOwner
|
2024-09-25 19:58:26 +02:00
|
|
|
|
|
|
|
_, err = existingSchema(ctx, c, "", schemauser.SchemaID)
|
2024-09-20 09:14:51 +02:00
|
|
|
if err != nil {
|
2024-09-23 17:32:24 +02:00
|
|
|
return nil, err
|
2024-09-20 09:14:51 +02:00
|
|
|
}
|
2024-09-25 19:58:26 +02:00
|
|
|
// TODO check for possible authenticators
|
|
|
|
|
2024-09-27 17:52:18 +02:00
|
|
|
writeModel, events, err := c.addUsername(ctx, add.ResourceOwner, add.UserID, add.Username)
|
2024-09-20 09:14:51 +02:00
|
|
|
if err != nil {
|
2024-09-23 17:32:24 +02:00
|
|
|
return nil, err
|
2024-09-20 09:14:51 +02:00
|
|
|
}
|
2024-09-27 17:52:18 +02:00
|
|
|
return c.pushAppendAndReduceDetails(ctx, writeModel, events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) addUsername(ctx context.Context, resourceOwner, userID string, add *Username) (*UsernameV3WriteModel, []eventstore.Command, error) {
|
|
|
|
if resourceOwner == "" || userID == "" || add == nil {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
id, err := c.idGenerator.Next()
|
2024-09-20 09:14:51 +02:00
|
|
|
if err != nil {
|
2024-09-27 17:52:18 +02:00
|
|
|
return nil, nil, err
|
2024-09-20 09:14:51 +02:00
|
|
|
}
|
2024-09-27 17:52:18 +02:00
|
|
|
writeModel, err := c.getSchemaUsernameWM(ctx, resourceOwner, userID, id)
|
2024-09-20 09:14:51 +02:00
|
|
|
if err != nil {
|
2024-09-27 17:52:18 +02:00
|
|
|
return nil, nil, err
|
2024-09-20 09:14:51 +02:00
|
|
|
}
|
2024-09-27 17:52:18 +02:00
|
|
|
events, err := writeModel.NewCreate(ctx, add.IsOrgSpecific, add.Username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return writeModel, events, nil
|
2024-09-20 09:14:51 +02:00
|
|
|
}
|
|
|
|
|
2024-09-26 19:15:03 +02:00
|
|
|
func (c *Commands) DeleteUsername(ctx context.Context, resourceOwner, userID, id string) (*domain.ObjectDetails, error) {
|
2024-09-24 20:42:20 +02:00
|
|
|
if userID == "" {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-J6ybG5WZiy", "Errors.IDMissing")
|
|
|
|
}
|
2024-09-23 17:32:24 +02:00
|
|
|
if id == "" {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-PoSU5BOZCi", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
|
2024-09-25 19:58:26 +02:00
|
|
|
writeModel, err := c.getSchemaUsernameWM(ctx, resourceOwner, userID, id)
|
2024-09-20 09:14:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-25 19:58:26 +02:00
|
|
|
events, err := writeModel.NewDelete(ctx)
|
|
|
|
if err != nil {
|
2024-09-20 09:14:51 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-25 19:58:26 +02:00
|
|
|
return c.pushAppendAndReduceDetails(ctx, writeModel, events...)
|
|
|
|
}
|
2024-09-23 17:32:24 +02:00
|
|
|
|
2024-09-25 19:58:26 +02:00
|
|
|
func (c *Commands) getSchemaUsernameWM(ctx context.Context, resourceOwner, userID, id string) (*UsernameV3WriteModel, error) {
|
|
|
|
writeModel := NewUsernameV3WriteModel(resourceOwner, userID, id, c.checkPermission)
|
|
|
|
if err := c.eventstore.FilterToQueryReducer(ctx, writeModel); err != nil {
|
2024-09-20 09:14:51 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-25 19:58:26 +02:00
|
|
|
return writeModel, nil
|
2024-09-20 09:14:51 +02:00
|
|
|
}
|