feat(group): group service to create, update, and delete groups (#10455)

# 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 <[email protected]>
This commit is contained in:
Gayathri Vijayan
2025-10-06 11:23:15 +02:00
committed by GitHub
co-authored by Livio Spring
parent ee3654fe6f
commit b81dedcaea
18 changed files with 2349 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
package group
import (
"github.com/zitadel/zitadel/internal/eventstore"
)
const (
groupEventTypePrefix = eventstore.EventType("group.")
AggregateType = "group"
AggregateVersion = "v1"
)
type Aggregate struct {
eventstore.Aggregate
}
func NewAggregate(id, resourceOwner string) *Aggregate {
return &Aggregate{
Aggregate: eventstore.Aggregate{
Type: AggregateType,
Version: AggregateVersion,
ID: id,
ResourceOwner: resourceOwner,
},
}
}
+9
View File
@@ -0,0 +1,9 @@
package group
import "github.com/zitadel/zitadel/internal/eventstore"
func init() {
eventstore.RegisterFilterEventMapper(AggregateType, GroupAddedEventType, eventstore.GenericEventMapper[GroupAddedEvent])
eventstore.RegisterFilterEventMapper(AggregateType, GroupChangedEventType, eventstore.GenericEventMapper[GroupChangedEvent])
eventstore.RegisterFilterEventMapper(AggregateType, GroupRemovedEventType, eventstore.GenericEventMapper[GroupRemovedEvent])
}
+155
View File
@@ -0,0 +1,155 @@
package group
import (
"context"
"github.com/zitadel/zitadel/internal/eventstore"
)
const (
uniqueGroupname = "group_name"
GroupAddedEventType = groupEventTypePrefix + "added"
GroupChangedEventType = groupEventTypePrefix + "changed"
GroupRemovedEventType = groupEventTypePrefix + "removed"
)
type GroupAddedEvent struct {
eventstore.BaseEvent `json:"-"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
func NewGroupAddedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
name,
description string,
) *GroupAddedEvent {
return &GroupAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
GroupAddedEventType),
Name: name,
Description: description,
}
}
func (g *GroupAddedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
g.BaseEvent = *event
}
func (g *GroupAddedEvent) Payload() any {
return g
}
func (g *GroupAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return []*eventstore.UniqueConstraint{NewAddGroupNameUniqueConstraint(g.Name, g.Aggregate().ResourceOwner)}
}
func NewAddGroupNameUniqueConstraint(groupName, organizationID string) *eventstore.UniqueConstraint {
return eventstore.NewAddEventUniqueConstraint(
uniqueGroupname,
groupName+":"+organizationID,
"Errors.Group.AlreadyExists")
}
func NewRemoveGroupNameUniqueConstraint(groupName, organizationID string) *eventstore.UniqueConstraint {
return eventstore.NewRemoveUniqueConstraint(
uniqueGroupname,
groupName+":"+organizationID,
)
}
type GroupChangedEvent struct {
eventstore.BaseEvent `json:"-"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
oldName string
}
func NewGroupChangedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
oldName string,
changes []GroupChanges,
) *GroupChangedEvent {
changeEvent := &GroupChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
GroupChangedEventType,
),
oldName: oldName,
}
for _, change := range changes {
change(changeEvent)
}
return changeEvent
}
type GroupChanges func(event *GroupChangedEvent)
func ChangeName(name *string) func(event *GroupChangedEvent) {
return func(event *GroupChangedEvent) {
event.Name = name
}
}
func ChangeDescription(description *string) func(event *GroupChangedEvent) {
return func(event *GroupChangedEvent) {
event.Description = description
}
}
func (g *GroupChangedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
g.BaseEvent = *event
}
func (g *GroupChangedEvent) Payload() any {
return g
}
func (g *GroupChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
if g.Name == nil {
return nil
}
return []*eventstore.UniqueConstraint{
NewRemoveGroupNameUniqueConstraint(g.oldName, g.Aggregate().ResourceOwner),
NewAddGroupNameUniqueConstraint(*g.Name, g.Aggregate().ResourceOwner)}
}
type GroupRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
name string
}
func NewGroupRemovedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
name string,
) *GroupRemovedEvent {
return &GroupRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(ctx, aggregate, GroupRemovedEventType),
ID: aggregate.ID,
name: name,
}
}
func (g *GroupRemovedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
g.BaseEvent = *event
}
func (g *GroupRemovedEvent) Payload() any {
return g
}
func (g *GroupRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return []*eventstore.UniqueConstraint{NewRemoveGroupNameUniqueConstraint(g.name, g.Aggregate().ResourceOwner)}
}