2022-01-13 10:02:39 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-10-20 12:36:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/usergrant"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-01-13 10:02:39 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2024-01-08 15:26:30 +00:00
|
|
|
UserGrantProjectionTable = "projections.user_grants4"
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
UserGrantID = "id"
|
|
|
|
UserGrantCreationDate = "creation_date"
|
|
|
|
UserGrantChangeDate = "change_date"
|
|
|
|
UserGrantSequence = "sequence"
|
|
|
|
UserGrantState = "state"
|
|
|
|
UserGrantResourceOwner = "resource_owner"
|
|
|
|
UserGrantInstanceID = "instance_id"
|
|
|
|
UserGrantUserID = "user_id"
|
|
|
|
UserGrantResourceOwnerUser = "resource_owner_user"
|
|
|
|
UserGrantProjectID = "project_id"
|
|
|
|
UserGrantResourceOwnerProject = "resource_owner_project"
|
|
|
|
UserGrantGrantID = "grant_id"
|
|
|
|
UserGrantGrantedOrg = "granted_org"
|
|
|
|
UserGrantRoles = "roles"
|
2022-03-23 08:02:39 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type userGrantProjection struct {
|
2023-10-19 10:19:10 +00:00
|
|
|
es handler.EventStore
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func newUserGrantProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, &userGrantProjection{es: config.Eventstore})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*userGrantProjection) Name() string {
|
|
|
|
return UserGrantProjectionTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*userGrantProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(UserGrantID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantCreationDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(UserGrantChangeDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(UserGrantSequence, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(UserGrantState, handler.ColumnTypeEnum),
|
|
|
|
handler.NewColumn(UserGrantResourceOwner, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantInstanceID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantUserID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantResourceOwnerUser, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantProjectID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantResourceOwnerProject, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantGrantID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantGrantedOrg, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(UserGrantRoles, handler.ColumnTypeTextArray, handler.Nullable()),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewPrimaryKey(UserGrantInstanceID, UserGrantID),
|
|
|
|
handler.WithIndex(handler.NewIndex("user_id", []string{UserGrantUserID})),
|
|
|
|
handler.WithIndex(handler.NewIndex("resource_owner", []string{UserGrantResourceOwner})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *userGrantProjection) Reducers() []handler.AggregateReducer {
|
2022-01-13 10:02:39 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: usergrant.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-01-13 10:02:39 +00:00
|
|
|
{
|
|
|
|
Event: usergrant.UserGrantAddedType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: usergrant.UserGrantChangedType,
|
|
|
|
Reduce: p.reduceChanged,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: usergrant.UserGrantCascadeChangedType,
|
|
|
|
Reduce: p.reduceChanged,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: usergrant.UserGrantRemovedType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: usergrant.UserGrantCascadeRemovedType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: usergrant.UserGrantDeactivatedType,
|
|
|
|
Reduce: p.reduceDeactivated,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: usergrant.UserGrantReactivatedType,
|
|
|
|
Reduce: p.reduceReactivated,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-01-13 10:02:39 +00:00
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: p.reduceUserRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: project.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-01-13 10:02:39 +00:00
|
|
|
{
|
|
|
|
Event: project.ProjectRemovedType,
|
|
|
|
Reduce: p.reduceProjectRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.GrantRemovedType,
|
|
|
|
Reduce: p.reduceProjectGrantRemoved,
|
|
|
|
},
|
2022-01-20 07:33:51 +00:00
|
|
|
{
|
|
|
|
Event: project.RoleRemovedType,
|
|
|
|
Reduce: p.reduceRoleRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.GrantChangedType,
|
|
|
|
Reduce: p.reduceProjectGrantChanged,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.GrantCascadeChangedType,
|
|
|
|
Reduce: p.reduceProjectGrantChanged,
|
|
|
|
},
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOwnerRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(UserGrantInstanceID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-13 10:02:39 +00:00
|
|
|
e, ok := event.(*usergrant.UserGrantAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-MQHVB", "reduce.wrong.event.type %s", usergrant.UserGrantAddedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
ctx := setUserGrantContext(e.Aggregate())
|
2024-03-08 07:52:59 +00:00
|
|
|
userOwner, projectOwner, grantOwner, err := getUserGrantResourceOwners(ctx, p.es, e.Aggregate().InstanceID, e.UserID, e.ProjectID, e.ProjectGrantID)
|
2022-11-30 16:01:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewCreateStatement(
|
2022-01-13 10:02:39 +00:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserGrantID, e.Aggregate().ID),
|
|
|
|
handler.NewCol(UserGrantResourceOwner, e.Aggregate().ResourceOwner),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(UserGrantInstanceID, e.Aggregate().InstanceID),
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewCol(UserGrantCreationDate, e.CreatedAt()),
|
|
|
|
handler.NewCol(UserGrantChangeDate, e.CreatedAt()),
|
2022-01-13 10:02:39 +00:00
|
|
|
handler.NewCol(UserGrantSequence, e.Sequence()),
|
|
|
|
handler.NewCol(UserGrantUserID, e.UserID),
|
2022-11-30 16:01:17 +00:00
|
|
|
handler.NewCol(UserGrantResourceOwnerUser, userOwner),
|
2022-01-13 10:02:39 +00:00
|
|
|
handler.NewCol(UserGrantProjectID, e.ProjectID),
|
2022-11-30 16:01:17 +00:00
|
|
|
handler.NewCol(UserGrantResourceOwnerProject, projectOwner),
|
2022-01-13 10:02:39 +00:00
|
|
|
handler.NewCol(UserGrantGrantID, e.ProjectGrantID),
|
2022-11-30 16:01:17 +00:00
|
|
|
handler.NewCol(UserGrantGrantedOrg, grantOwner),
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewCol(UserGrantRoles, database.TextArray[string](e.RoleKeys)),
|
2022-01-13 10:02:39 +00:00
|
|
|
handler.NewCol(UserGrantState, domain.UserGrantStateActive),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceChanged(event eventstore.Event) (*handler.Statement, error) {
|
2023-10-19 10:19:10 +00:00
|
|
|
var roles database.TextArray[string]
|
2022-01-13 10:02:39 +00:00
|
|
|
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *usergrant.UserGrantChangedEvent:
|
|
|
|
roles = e.RoleKeys
|
|
|
|
case *usergrant.UserGrantCascadeChangedEvent:
|
|
|
|
roles = e.RoleKeys
|
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-hOr1E", "reduce.wrong.event.type %v", []eventstore.EventType{usergrant.UserGrantChangedType, usergrant.UserGrantCascadeChangedType})
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2022-01-13 10:02:39 +00:00
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewCol(UserGrantChangeDate, event.CreatedAt()),
|
2022-01-13 10:02:39 +00:00
|
|
|
handler.NewCol(UserGrantRoles, roles),
|
|
|
|
handler.NewCol(UserGrantSequence, event.Sequence()),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantID, event.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-13 10:02:39 +00:00
|
|
|
switch event.(type) {
|
|
|
|
case *usergrant.UserGrantRemovedEvent, *usergrant.UserGrantCascadeRemovedEvent:
|
|
|
|
// ok
|
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-7OBEC", "reduce.wrong.event.type %v", []eventstore.EventType{usergrant.UserGrantRemovedType, usergrant.UserGrantCascadeRemovedType})
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2022-01-13 10:02:39 +00:00
|
|
|
event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantID, event.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceDeactivated(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-13 10:02:39 +00:00
|
|
|
if _, ok := event.(*usergrant.UserGrantDeactivatedEvent); !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-oP7Gm", "reduce.wrong.event.type %s", usergrant.UserGrantDeactivatedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2022-01-13 10:02:39 +00:00
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewCol(UserGrantChangeDate, event.CreatedAt()),
|
2022-01-13 10:02:39 +00:00
|
|
|
handler.NewCol(UserGrantState, domain.UserGrantStateInactive),
|
|
|
|
handler.NewCol(UserGrantSequence, event.Sequence()),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantID, event.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceReactivated(event eventstore.Event) (*handler.Statement, error) {
|
2023-12-31 13:03:23 +00:00
|
|
|
if _, ok := event.(*usergrant.UserGrantReactivatedEvent); !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-DGsKh", "reduce.wrong.event.type %s", usergrant.UserGrantReactivatedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2022-01-13 10:02:39 +00:00
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewCol(UserGrantChangeDate, event.CreatedAt()),
|
2022-01-13 10:02:39 +00:00
|
|
|
handler.NewCol(UserGrantState, domain.UserGrantStateActive),
|
|
|
|
handler.NewCol(UserGrantSequence, event.Sequence()),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantID, event.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceUserRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-13 10:02:39 +00:00
|
|
|
if _, ok := event.(*user.UserRemovedEvent); !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-Bner2a", "reduce.wrong.event.type %s", user.UserRemovedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2022-01-13 10:02:39 +00:00
|
|
|
event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantUserID, event.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceProjectRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-13 10:02:39 +00:00
|
|
|
if _, ok := event.(*project.ProjectRemovedEvent); !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-Bne2a", "reduce.wrong.event.type %s", project.ProjectRemovedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2022-01-13 10:02:39 +00:00
|
|
|
event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantProjectID, event.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceProjectGrantRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-13 10:02:39 +00:00
|
|
|
e, ok := event.(*project.GrantRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-dGr2a", "reduce.wrong.event.type %s", project.GrantRemovedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2022-01-13 10:02:39 +00:00
|
|
|
event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantGrantID, e.GrantID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-13 10:02:39 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
2022-01-20 07:33:51 +00:00
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceRoleRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-20 07:33:51 +00:00
|
|
|
e, ok := event.(*project.RoleRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-dswg2", "reduce.wrong.event.type %s", project.RoleRemovedType)
|
2022-01-20 07:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2022-01-20 07:33:51 +00:00
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewArrayRemoveCol(UserGrantRoles, e.Key),
|
2022-01-20 07:33:51 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantProjectID, e.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-20 07:33:51 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reduceProjectGrantChanged(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-20 07:33:51 +00:00
|
|
|
var grantID string
|
2023-10-19 10:19:10 +00:00
|
|
|
var keys database.TextArray[string]
|
2022-01-20 07:33:51 +00:00
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.GrantChangedEvent:
|
|
|
|
grantID = e.GrantID
|
|
|
|
keys = e.RoleKeys
|
|
|
|
case *project.GrantCascadeChangedEvent:
|
|
|
|
grantID = e.GrantID
|
|
|
|
keys = e.RoleKeys
|
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-Fh3gw", "reduce.wrong.event.type %v", []eventstore.EventType{project.GrantChangedType, project.GrantCascadeChangedType})
|
2022-01-20 07:33:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2022-01-20 07:33:51 +00:00
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewArrayIntersectCol(UserGrantRoles, keys),
|
2022-01-20 07:33:51 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantGrantID, grantID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserGrantInstanceID, event.Aggregate().InstanceID),
|
2022-01-20 07:33:51 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
func (p *userGrantProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-jpIvp", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewMultiStatement(
|
2022-11-30 16:01:17 +00:00
|
|
|
e,
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.AddDeleteStatement(
|
2022-11-30 16:01:17 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserGrantResourceOwner, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.AddDeleteStatement(
|
2022-11-30 16:01:17 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserGrantResourceOwnerUser, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.AddDeleteStatement(
|
2022-11-30 16:01:17 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserGrantResourceOwnerProject, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.AddDeleteStatement(
|
2022-11-30 16:01:17 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserGrantGrantedOrg, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2024-01-08 15:26:30 +00:00
|
|
|
func getUserResourceOwner(ctx context.Context, es handler.EventStore, instanceID, userID string) (string, error) {
|
|
|
|
userRO, _, _, err := getResourceOwners(ctx, es, instanceID, userID, "", "")
|
2024-03-08 07:52:59 +00:00
|
|
|
if userRO == "" {
|
|
|
|
return "", zerrors.ThrowNotFound(nil, "PROJ-uahkkord22", "Errors.NotFound")
|
|
|
|
}
|
2024-01-08 15:26:30 +00:00
|
|
|
return userRO, err
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 07:52:59 +00:00
|
|
|
func getUserGrantResourceOwners(ctx context.Context, es handler.EventStore, instanceID, userID, projectID, grantID string) (string, string, string, error) {
|
|
|
|
userRO, projectRO, grantedOrg, err := getResourceOwners(ctx, es, instanceID, userID, projectID, grantID)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
// user grant always has a user defined
|
|
|
|
if userRO == "" {
|
|
|
|
return "", "", "", zerrors.ThrowNotFound(nil, "PROJ-8x5behx5jy", "Errors.NotFound")
|
|
|
|
}
|
|
|
|
// either a projectID
|
|
|
|
if projectID != "" && projectRO == "" {
|
|
|
|
return "", "", "", zerrors.ThrowNotFound(nil, "PROJ-1ldp25o3bx", "Errors.NotFound")
|
|
|
|
}
|
|
|
|
// or a grantID
|
|
|
|
if grantID != "" && grantedOrg == "" {
|
|
|
|
return "", "", "", zerrors.ThrowNotFound(nil, "PROJ-9ngp5dcn76", "Errors.NotFound")
|
|
|
|
}
|
|
|
|
return userRO, projectRO, grantedOrg, nil
|
|
|
|
}
|
|
|
|
|
2024-01-08 15:26:30 +00:00
|
|
|
func getResourceOwners(ctx context.Context, es handler.EventStore, instanceID, userID, projectID, grantID string) (userRO string, projectRO string, grantedOrg string, err error) {
|
|
|
|
builder := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AwaitOpenTransactions().
|
|
|
|
InstanceID(instanceID).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(userID).
|
|
|
|
EventTypes(user.HumanRegisteredType, user.HumanAddedType, user.MachineAddedEventType)
|
|
|
|
|
|
|
|
// if it's a project grant then we only need the resourceowner for the projectgrant, else the project
|
|
|
|
if grantID != "" {
|
|
|
|
builder = builder.Or().
|
2022-11-30 16:01:17 +00:00
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(projectID).
|
|
|
|
EventTypes(project.GrantAddedType).
|
|
|
|
EventData(map[string]interface{}{
|
|
|
|
"grantId": grantID,
|
2024-01-08 15:26:30 +00:00
|
|
|
})
|
|
|
|
} else if projectID != "" {
|
|
|
|
builder = builder.Or().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(projectID).
|
|
|
|
EventTypes(project.ProjectAddedType)
|
|
|
|
}
|
|
|
|
|
|
|
|
events, err := es.Filter(
|
|
|
|
ctx,
|
|
|
|
builder.Builder(),
|
2022-11-30 16:01:17 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-01-08 15:26:30 +00:00
|
|
|
return "", "", "", err
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
2024-03-08 07:52:59 +00:00
|
|
|
|
|
|
|
// sorted ascending
|
2024-01-08 15:26:30 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.GrantAddedEvent:
|
|
|
|
grantedOrg = e.GrantedOrgID
|
|
|
|
case *project.ProjectAddedEvent:
|
|
|
|
projectRO = e.Aggregate().ResourceOwner
|
|
|
|
case *user.HumanRegisteredEvent, *user.HumanAddedEvent, *user.MachineAddedEvent:
|
|
|
|
userRO = e.Aggregate().ResourceOwner
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
2024-01-08 15:26:30 +00:00
|
|
|
return userRO, projectRO, grantedOrg, nil
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func setUserGrantContext(aggregate *eventstore.Aggregate) context.Context {
|
|
|
|
return authz.WithInstanceID(context.Background(), aggregate.InstanceID)
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|