mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-25 16:59:58 +00:00
# Which Problems Are Solved This PR adds API definition and backend implementation for GroupService to manage user groups. # How the Problems Are Solved * API definition to create, update, retrieve, and delete groups is added * Command-side implementation to create, update, and delete user groups as part of the GroupV2 API is added # Additional Changes N/A # Additional Context - Related to #10089, #9702 (parent ticket) - User contribution: https://github.com/zitadel/zitadel/pull/9428/files - Additional functionalities to list/search user groups, add permissions, manage users in groups, group scopes will be added in subsequent PRs. - Also needs documentation, which will be added once the entire feature is available --------- Co-authored-by: Livio Spring <livio.a@gmail.com>
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/repository/group"
|
|
)
|
|
|
|
// GroupWriteModel represents the write-model for a group.
|
|
type GroupWriteModel struct {
|
|
eventstore.WriteModel
|
|
|
|
Name string
|
|
Description string
|
|
|
|
State domain.GroupState
|
|
}
|
|
|
|
// NewGroupWriteModel initializes a new instance of GroupWriteModel from the given Group.
|
|
func NewGroupWriteModel(id, orgID string) *GroupWriteModel {
|
|
return &GroupWriteModel{
|
|
WriteModel: eventstore.WriteModel{
|
|
AggregateID: id,
|
|
ResourceOwner: orgID,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Query constructs a search query for retrieving group-related events based on the GroupWriteModel attributes.
|
|
func (g *GroupWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
ResourceOwner(g.ResourceOwner).
|
|
AddQuery().
|
|
AggregateTypes(group.AggregateType).
|
|
AggregateIDs(g.AggregateID).
|
|
EventTypes(
|
|
group.GroupAddedEventType,
|
|
group.GroupChangedEventType,
|
|
group.GroupRemovedEventType).Builder()
|
|
}
|
|
|
|
func (g *GroupWriteModel) Reduce() error {
|
|
for _, event := range g.Events {
|
|
switch e := event.(type) {
|
|
case *group.GroupAddedEvent:
|
|
g.AggregateID = e.ID
|
|
g.Name = e.Name
|
|
g.Description = e.Description
|
|
g.State = domain.GroupStateActive
|
|
case *group.GroupChangedEvent:
|
|
if e.Name != nil {
|
|
g.Name = *e.Name
|
|
}
|
|
if e.Description != nil {
|
|
g.Description = *e.Description
|
|
}
|
|
case *group.GroupRemovedEvent:
|
|
g.State = domain.GroupStateRemoved
|
|
}
|
|
}
|
|
return g.WriteModel.Reduce()
|
|
}
|
|
|
|
func (g *GroupWriteModel) NewChangedEvent(ctx context.Context, agg *eventstore.Aggregate, name, description *string) *group.GroupChangedEvent {
|
|
changes := make([]group.GroupChanges, 0)
|
|
oldName := ""
|
|
|
|
if name != nil && g.Name != *name {
|
|
oldName = g.Name
|
|
changes = append(changes, group.ChangeName(name))
|
|
}
|
|
if description != nil && g.Description != *description {
|
|
changes = append(changes, group.ChangeDescription(description))
|
|
}
|
|
if len(changes) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return group.NewGroupChangedEvent(ctx, agg, oldName, changes)
|
|
}
|
|
|
|
// GroupAggregateFromWriteModel maps a WriteModel to a group-specific Aggregate using its type and version.
|
|
func GroupAggregateFromWriteModel(ctx context.Context, wm *eventstore.WriteModel) *eventstore.Aggregate {
|
|
return eventstore.AggregateFromWriteModelCtx(ctx, wm, group.AggregateType, group.AggregateVersion)
|
|
}
|