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/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
|
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"
|
2022-01-13 10:02:39 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2022-11-30 16:01:17 +00:00
|
|
|
UserGrantProjectionTable = "projections.user_grants3"
|
|
|
|
|
|
|
|
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"
|
|
|
|
UserGrantUserOwnerRemoved = "user_owner_removed"
|
|
|
|
UserGrantProjectID = "project_id"
|
|
|
|
UserGrantResourceOwnerProject = "resource_owner_project"
|
|
|
|
UserGrantProjectOwnerRemoved = "project_owner_removed"
|
|
|
|
UserGrantGrantID = "grant_id"
|
|
|
|
UserGrantGrantedOrg = "granted_org"
|
|
|
|
UserGrantGrantedOrgRemoved = "granted_org_removed"
|
|
|
|
UserGrantRoles = "roles"
|
|
|
|
UserGrantOwnerRemoved = "owner_removed"
|
2022-03-23 08:02:39 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type userGrantProjection struct {
|
2022-01-13 10:02:39 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newUserGrantProjection(ctx context.Context, config crdb.StatementHandlerConfig) *userGrantProjection {
|
|
|
|
p := new(userGrantProjection)
|
2022-01-13 10:02:39 +00:00
|
|
|
config.ProjectionName = UserGrantProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(UserGrantID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserGrantCreationDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(UserGrantChangeDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(UserGrantSequence, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(UserGrantState, crdb.ColumnTypeEnum),
|
|
|
|
crdb.NewColumn(UserGrantResourceOwner, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserGrantInstanceID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserGrantUserID, crdb.ColumnTypeText),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.NewColumn(UserGrantResourceOwnerUser, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserGrantUserOwnerRemoved, crdb.ColumnTypeBool, crdb.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
crdb.NewColumn(UserGrantProjectID, crdb.ColumnTypeText),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.NewColumn(UserGrantResourceOwnerProject, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserGrantProjectOwnerRemoved, crdb.ColumnTypeBool, crdb.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
crdb.NewColumn(UserGrantGrantID, crdb.ColumnTypeText),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.NewColumn(UserGrantGrantedOrg, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserGrantGrantedOrgRemoved, crdb.ColumnTypeBool, crdb.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
crdb.NewColumn(UserGrantRoles, crdb.ColumnTypeTextArray, crdb.Nullable()),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.NewColumn(UserGrantOwnerRemoved, crdb.ColumnTypeBool, crdb.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(UserGrantInstanceID, UserGrantID),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.WithIndex(crdb.NewIndex("user_id", []string{UserGrantUserID})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("resource_owner", []string{UserGrantResourceOwner})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{UserGrantOwnerRemoved})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("user_owner_removed", []string{UserGrantUserOwnerRemoved})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("project_owner_removed", []string{UserGrantProjectOwnerRemoved})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("granted_org_removed", []string{UserGrantGrantedOrgRemoved})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-01-13 10:02:39 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userGrantProjection) reducers() []handler.AggregateReducer {
|
2022-01-13 10:02:39 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: usergrant.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: p.reduceUserRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: project.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOwnerRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
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 {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.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())
|
|
|
|
userOwner, err := getResourceOwnerOfUser(ctx, p.Eventstore, e.Aggregate().InstanceID, e.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectOwner := ""
|
|
|
|
grantOwner := ""
|
|
|
|
if e.ProjectGrantID != "" {
|
|
|
|
grantOwner, err = getGrantedOrgOfGrantedProject(ctx, p.Eventstore, e.Aggregate().InstanceID, e.ProjectID, e.ProjectGrantID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
projectOwner, err = getResourceOwnerOfProject(ctx, p.Eventstore, e.Aggregate().InstanceID, e.ProjectID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 10:02:39 +00:00
|
|
|
return crdb.NewCreateStatement(
|
|
|
|
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),
|
2022-01-13 10:02:39 +00:00
|
|
|
handler.NewCol(UserGrantCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserGrantChangeDate, e.CreationDate()),
|
|
|
|
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),
|
2022-08-31 07:52:43 +00:00
|
|
|
handler.NewCol(UserGrantRoles, database.StringArray(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) {
|
2022-08-31 07:52:43 +00:00
|
|
|
var roles database.StringArray
|
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:
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-hOr1E", "reduce.wrong.event.type %v", []eventstore.EventType{usergrant.UserGrantChangedType, usergrant.UserGrantCascadeChangedType})
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
event,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserGrantChangeDate, event.CreationDate()),
|
|
|
|
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:
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-7OBEC", "reduce.wrong.event.type %v", []eventstore.EventType{usergrant.UserGrantRemovedType, usergrant.UserGrantCascadeRemovedType})
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
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 {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-oP7Gm", "reduce.wrong.event.type %s", usergrant.UserGrantDeactivatedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
event,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserGrantChangeDate, event.CreationDate()),
|
|
|
|
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) {
|
2022-01-13 10:02:39 +00:00
|
|
|
if _, ok := event.(*usergrant.UserGrantDeactivatedEvent); !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-DGsKh", "reduce.wrong.event.type %s", usergrant.UserGrantReactivatedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
event,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserGrantChangeDate, event.CreationDate()),
|
|
|
|
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 {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-Bner2a", "reduce.wrong.event.type %s", user.UserRemovedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
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 {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-Bne2a", "reduce.wrong.event.type %s", project.ProjectRemovedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
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 {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-dGr2a", "reduce.wrong.event.type %s", project.GrantRemovedType)
|
2022-01-13 10:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
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 {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-dswg2", "reduce.wrong.event.type %s", project.RoleRemovedType)
|
2022-01-20 07:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
event,
|
|
|
|
[]handler.Column{
|
|
|
|
crdb.NewArrayRemoveCol(UserGrantRoles, e.Key),
|
|
|
|
},
|
|
|
|
[]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
|
|
|
|
var keys []string
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.GrantChangedEvent:
|
|
|
|
grantID = e.GrantID
|
|
|
|
keys = e.RoleKeys
|
|
|
|
case *project.GrantCascadeChangedEvent:
|
|
|
|
grantID = e.GrantID
|
|
|
|
keys = e.RoleKeys
|
|
|
|
default:
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-Fh3gw", "reduce.wrong.event.type %v", []eventstore.EventType{project.GrantChangedType, project.GrantCascadeChangedType})
|
2022-01-20 07:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2022-08-31 07:52:43 +00:00
|
|
|
crdb.NewArrayIntersectCol(UserGrantRoles, database.StringArray(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 {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-jpIvp", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewMultiStatement(
|
|
|
|
e,
|
|
|
|
crdb.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserGrantChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserGrantSequence, e.Sequence()),
|
|
|
|
handler.NewCol(UserGrantOwnerRemoved, true),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserGrantResourceOwner, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
crdb.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserGrantChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserGrantSequence, e.Sequence()),
|
|
|
|
handler.NewCol(UserGrantUserOwnerRemoved, true),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserGrantResourceOwnerUser, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
crdb.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserGrantChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserGrantSequence, e.Sequence()),
|
|
|
|
handler.NewCol(UserGrantProjectOwnerRemoved, true),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserGrantResourceOwnerProject, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
crdb.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserGrantChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserGrantSequence, e.Sequence()),
|
|
|
|
handler.NewCol(UserGrantGrantedOrgRemoved, true),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserGrantInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserGrantGrantedOrg, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getResourceOwnerOfUser(ctx context.Context, es *eventstore.Eventstore, instanceID, aggID string) (string, error) {
|
|
|
|
events, err := es.Filter(
|
|
|
|
ctx,
|
|
|
|
eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
InstanceID(instanceID).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(aggID).
|
|
|
|
EventTypes(user.HumanRegisteredType, user.HumanAddedType, user.MachineAddedEventType).
|
|
|
|
Builder(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(events) != 1 {
|
|
|
|
return "", errors.ThrowNotFound(nil, "PROJ-0I92sp", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
return events[0].Aggregate().ResourceOwner, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getResourceOwnerOfProject(ctx context.Context, es *eventstore.Eventstore, instanceID, aggID string) (string, error) {
|
|
|
|
events, err := es.Filter(
|
|
|
|
ctx,
|
|
|
|
eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
InstanceID(instanceID).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(aggID).
|
|
|
|
EventTypes(project.ProjectAddedType).
|
|
|
|
Builder(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(events) != 1 {
|
|
|
|
return "", errors.ThrowNotFound(nil, "PROJ-0I91sp", "Errors.Project.NotFound")
|
|
|
|
}
|
|
|
|
return events[0].Aggregate().ResourceOwner, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGrantedOrgOfGrantedProject(ctx context.Context, es *eventstore.Eventstore, instanceID, projectID, grantID string) (string, error) {
|
|
|
|
events, err := es.Filter(
|
|
|
|
ctx,
|
|
|
|
eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
InstanceID(instanceID).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(projectID).
|
|
|
|
EventTypes(project.GrantAddedType).
|
|
|
|
EventData(map[string]interface{}{
|
|
|
|
"grantId": grantID,
|
|
|
|
}).
|
|
|
|
Builder(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(events) != 1 {
|
|
|
|
return "", errors.ThrowNotFound(nil, "PROJ-MoaSpw", "Errors.Grant.NotFound")
|
|
|
|
}
|
|
|
|
grantAddedEvent, ok := events[0].(*project.GrantAddedEvent)
|
|
|
|
if !ok {
|
|
|
|
return "", errors.ThrowNotFound(nil, "PROJ-P0s2o0", "Errors.Grant.NotFound")
|
|
|
|
}
|
|
|
|
return grantAddedEvent.GrantedOrgID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setUserGrantContext(event eventstore.Aggregate) context.Context {
|
|
|
|
return authz.WithInstanceID(context.Background(), event.InstanceID)
|
|
|
|
}
|