This commit is contained in:
adlerhurst
2025-05-08 07:42:53 +02:00
parent 6ba86bc67b
commit 8ba497cb87
166 changed files with 700 additions and 10922 deletions

View File

@@ -3,6 +3,8 @@ package domain
import (
"context"
"time"
"github.com/zitadel/zitadel/backend/v3/storage/database"
)
type Instance struct {
@@ -19,16 +21,43 @@ func (i *Instance) Keys(index string) (key []string) {
return []string{}
}
type InstanceRepository interface {
ByID(ctx context.Context, id string) (*Instance, error)
Create(ctx context.Context, instance *Instance) error
On(id string) InstanceOperation
type instanceColumns interface {
// IDColumn returns the column for the id field.
IDColumn() database.Column
// NameColumn returns the column for the name field.
NameColumn() database.Column
// CreatedAtColumn returns the column for the created at field.
CreatedAtColumn() database.Column
// UpdatedAtColumn returns the column for the updated at field.
UpdatedAtColumn() database.Column
// DeletedAtColumn returns the column for the deleted at field.
DeletedAtColumn() database.Column
}
type InstanceOperation interface {
AdminRepository
Update(ctx context.Context, instance *Instance) error
Delete(ctx context.Context) error
type instanceConditions interface {
// IDCondition returns an equal filter on the id field.
IDCondition(instanceID string) database.Condition
// NameCondition returns a filter on the name field.
NameCondition(op database.TextOperation, name string) database.Condition
}
type instanceChanges interface {
// SetName sets the name column.
SetName(name string) database.Change
}
type InstanceRepository interface {
instanceColumns
instanceConditions
instanceChanges
Member() MemberRepository
Get(ctx context.Context, opts ...database.QueryOption) (*Instance, error)
Create(ctx context.Context, instance *Instance) error
Update(ctx context.Context, condition database.Condition, changes ...database.Change) error
Delete(ctx context.Context, condition database.Condition) error
}
type CreateInstance struct {

View File

@@ -3,33 +3,95 @@ package domain
import (
"context"
"time"
"github.com/zitadel/zitadel/backend/v3/storage/database"
)
type OrgState uint8
const (
OrgStateActive OrgState = iota + 1
OrgStateInactive
)
type Org struct {
ID string `json:"id"`
Name string `json:"name"`
State OrgState `json:"state"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type orgColumns interface {
// InstanceIDColumn returns the column for the instance id field.
InstanceIDColumn() database.Column
// IDColumn returns the column for the id field.
IDColumn() database.Column
// NameColumn returns the column for the name field.
NameColumn() database.Column
// StateColumn returns the column for the state field.
StateColumn() database.Column
// CreatedAtColumn returns the column for the created at field.
CreatedAtColumn() database.Column
// UpdatedAtColumn returns the column for the updated at field.
UpdatedAtColumn() database.Column
// DeletedAtColumn returns the column for the deleted at field.
DeletedAtColumn() database.Column
}
type orgConditions interface {
// InstanceIDCondition returns an equal filter on the instance id field.
InstanceIDCondition(instanceID string) database.Condition
// IDCondition returns an equal filter on the id field.
IDCondition(orgID string) database.Condition
// NameCondition returns a filter on the name field.
NameCondition(op database.TextOperation, name string) database.Condition
// StateCondition returns a filter on the state field.
StateCondition(op database.NumberOperation, state OrgState) database.Condition
}
type orgChanges interface {
// SetName sets the name column.
SetName(name string) database.Change
// SetState sets the state column.
SetState(state OrgState) database.Change
}
type OrgRepository interface {
ByID(ctx context.Context, orgID string) (*Org, error)
orgColumns
orgConditions
orgChanges
// Member returns the admin repository.
Member() MemberRepository
// Domain returns the domain repository.
Domain() DomainRepository
// Get returns an org based on the given condition.
Get(ctx context.Context, opts ...database.QueryOption) (*Org, error)
// List returns a list of orgs based on the given condition.
List(ctx context.Context, opts ...database.QueryOption) ([]*Org, error)
// Create creates a new org.
Create(ctx context.Context, org *Org) error
On(id string) OrgOperation
// Delete removes orgs based on the given condition.
Delete(ctx context.Context, condition database.Condition) error
// Update executes the given changes based on the given condition.
Update(ctx context.Context, condition database.Condition, changes ...database.Change) error
}
type OrgOperation interface {
AdminRepository
MemberRepository
DomainRepository
Update(ctx context.Context, org *Org) error
Delete(ctx context.Context) error
}
type AdminRepository interface {
AddAdmin(ctx context.Context, userID string, roles []string) error
SetAdminRoles(ctx context.Context, userID string, roles []string) error
RemoveAdmin(ctx context.Context, userID string) error
type MemberRepository interface {
AddMember(ctx context.Context, orgID, userID string, roles []string) error
SetMemberRoles(ctx context.Context, orgID, userID string, roles []string) error
RemoveMember(ctx context.Context, orgID, userID string) error
}
type DomainRepository interface {

View File

@@ -2,15 +2,17 @@ package domain
import (
"context"
"github.com/zitadel/zitadel/backend/v3/storage/eventstore"
)
type AddOrgCommand struct {
ID string `json:"id"`
Name string `json:"name"`
Admins []AddAdminCommand `json:"admins"`
ID string `json:"id"`
Name string `json:"name"`
Admins []*AddMemberCommand `json:"admins"`
}
func NewAddOrgCommand(name string, admins ...AddAdminCommand) *AddOrgCommand {
func NewAddOrgCommand(name string, admins ...*AddMemberCommand) *AddOrgCommand {
return &AddOrgCommand{
Name: name,
Admins: admins,
@@ -39,11 +41,31 @@ func (cmd *AddOrgCommand) Execute(ctx context.Context, opts *CommandOpts) (err e
return err
}
for _, admin := range cmd.Admins {
admin.orgID = cmd.ID
if err = opts.Invoke(ctx, admin); err != nil {
return err
}
}
return nil
}
// Events implements [eventer].
func (cmd *AddOrgCommand) Events() []*eventstore.Event {
return []*eventstore.Event{
{
AggregateType: "org",
AggregateID: cmd.ID,
Type: "org.added",
Payload: cmd,
},
}
}
var (
_ Commander = (*AddOrgCommand)(nil)
_ eventer = (*AddOrgCommand)(nil)
)
func (cmd *AddOrgCommand) ensureID() (err error) {
@@ -54,21 +76,36 @@ func (cmd *AddOrgCommand) ensureID() (err error) {
return err
}
type AddAdminCommand struct {
type AddMemberCommand struct {
orgID string
UserID string `json:"userId"`
Roles []string `json:"roles"`
}
// Execute implements Commander.
func (a *AddAdminCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) {
func (a *AddMemberCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) {
close, err := opts.EnsureTx(ctx)
if err != nil {
return err
}
defer func() { err = close(ctx, err) }()
return nil
return orgRepo(opts.DB).Member().AddMember(ctx, a.orgID, a.UserID, a.Roles)
}
// Events implements [eventer].
func (a *AddMemberCommand) Events() []*eventstore.Event {
return []*eventstore.Event{
{
AggregateType: "org",
AggregateID: a.UserID,
Type: "member.added",
Payload: a,
},
}
}
var (
_ Commander = (*AddAdminCommand)(nil)
_ Commander = (*AddMemberCommand)(nil)
_ eventer = (*AddMemberCommand)(nil)
)