mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# Which Problems Are Solved This PR implements the endpoints listed in https://github.com/zitadel/zitadel/issues/10444 . # How the Problems Are Solved The implementation follows the same pattern as the Instance one. The `Add` and `Remove` are implementing the `Commander` interface, while `List` Domains endpoints are implementing the `Querier` interace. The PR adds also some forgotten calls to the relational logic for the Instance API. The commits are identifiable by the label `instance:` prepended to them. This is the complete list: bafce01b0 , 6fe7257ab , e27bc9c34 , 8133ede73 , fde667953 , a3e1135ed , ee1a6f9c1 # Additional Context - Closes #10444 --------- Co-authored-by: Elio Bischof <[email protected]> Co-authored-by: Lebogang Phoshoko <[email protected]> Co-authored-by: Wim Van Laer <[email protected]> Co-authored-by: Max Peintner <[email protected]> Co-authored-by: Gayathri Vijayan <[email protected]>
119 lines
3.8 KiB
Go
119 lines
3.8 KiB
Go
package domain
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
type RemoveInstanceDomainCommand struct {
|
|
InstanceID string `json:"instance_id"`
|
|
DomainName string `json:"domain_name"`
|
|
DType DomainType `json:"domain_type"`
|
|
|
|
DeleteTime *time.Time `json:"delete_date"`
|
|
}
|
|
|
|
// RequiresTransaction implements [Transactional].
|
|
func (r *RemoveInstanceDomainCommand) RequiresTransaction() {}
|
|
|
|
func NewRemoveInstanceDomainCommand(instanceID, domainName string, domainType DomainType) *RemoveInstanceDomainCommand {
|
|
return &RemoveInstanceDomainCommand{
|
|
InstanceID: instanceID,
|
|
DomainName: domainName,
|
|
DType: domainType,
|
|
}
|
|
}
|
|
|
|
// Events implements [Commander].
|
|
func (r *RemoveInstanceDomainCommand) Events(ctx context.Context, _ *InvokeOpts) ([]eventstore.Command, error) {
|
|
var toReturn eventstore.Command
|
|
switch r.DType {
|
|
case DomainTypeCustom:
|
|
toReturn = instance.NewDomainRemovedEvent(ctx, &instance.NewAggregate(r.InstanceID).Aggregate, r.DomainName)
|
|
case DomainTypeTrusted:
|
|
toReturn = instance.NewTrustedDomainRemovedEvent(ctx, &instance.NewAggregate(r.InstanceID).Aggregate, r.DomainName)
|
|
default:
|
|
return nil, nil
|
|
}
|
|
return []eventstore.Command{toReturn}, nil
|
|
}
|
|
|
|
// Execute implements [Commander].
|
|
func (r *RemoveInstanceDomainCommand) Execute(ctx context.Context, opts *InvokeOpts) (err error) {
|
|
instanceRepo := opts.instanceDomainRepo
|
|
|
|
deletedRows, err := instanceRepo.Remove(ctx, opts.DB(), database.And(instanceRepo.InstanceIDCondition(r.InstanceID), instanceRepo.PrimaryKeyCondition(r.DomainName)))
|
|
if err != nil {
|
|
return zerrors.ThrowInternal(err, "DOM-KH7AuJ", "Errors.Instance.Domain.Remove")
|
|
}
|
|
|
|
if deletedRows > 1 {
|
|
return zerrors.ThrowInternal(nil, "DOM-XSCnJB", "Errors.Instsance.Domain.DeleteMismatch")
|
|
}
|
|
|
|
if deletedRows < 1 {
|
|
return nil
|
|
}
|
|
|
|
r.DeleteTime = gu.Ptr(time.Now())
|
|
return err
|
|
}
|
|
|
|
// String implements [Commander].
|
|
func (r *RemoveInstanceDomainCommand) String() string {
|
|
return "RemoveInstanceDomainCommand"
|
|
}
|
|
|
|
// Validate implements [Commander].
|
|
func (r *RemoveInstanceDomainCommand) Validate(ctx context.Context, opts *InvokeOpts) (err error) {
|
|
if r.DomainName = strings.TrimSpace(r.DomainName); r.DomainName == "" || len(r.DomainName) > 253 {
|
|
return zerrors.ThrowInvalidArgument(nil, "DOM-PLpYix", "Errors.Invalid.Argument")
|
|
}
|
|
|
|
if r.InstanceID = strings.TrimSpace(r.InstanceID); r.InstanceID == "" {
|
|
return zerrors.ThrowInvalidArgument(nil, "DOM-VSsTTf", "Errors.Invalid.Argument")
|
|
}
|
|
|
|
if !allowedDomainCharacters.MatchString(r.DomainName) {
|
|
return zerrors.ThrowInvalidArgument(nil, "DOM-dYD5I7", "Errors.Instance.Domain.InvalidCharacter")
|
|
}
|
|
|
|
if r.InstanceID != authz.GetInstance(ctx).InstanceID() {
|
|
return zerrors.ThrowInvalidArgument(nil, "DOM-83FUdY", "Errors.Invalid.Argument")
|
|
}
|
|
|
|
if authZErr := opts.Permissions.CheckInstancePermission(ctx, DomainWritePermission); authZErr != nil {
|
|
return zerrors.ThrowPermissionDenied(authZErr, "DOM-eroxID", "Errors.PermissionDenied")
|
|
}
|
|
|
|
domainRepo := opts.instanceDomainRepo
|
|
d, err := domainRepo.Get(ctx, opts.DB(), database.WithCondition(domainRepo.DomainCondition(database.TextOperationEqual, r.DomainName)))
|
|
if err != nil {
|
|
if errors.Is(err, &database.NoRowFoundError{}) {
|
|
return nil
|
|
}
|
|
return zerrors.ThrowInternal(err, "DOM-Zvv1fi", "Errors.Instance.Domain.Get")
|
|
}
|
|
|
|
if d.Type == DomainTypeCustom && d.IsGenerated != nil && *d.IsGenerated {
|
|
return zerrors.ThrowPreconditionFailed(nil, "DOM-cSfCVG", "Errors.Instance.Domain.GeneratedNotRemovable")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
_ Commander = (*RemoveInstanceDomainCommand)(nil)
|
|
_ Transactional = (*RemoveInstanceDomainCommand)(nil)
|
|
)
|