mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 19:14:23 +00:00
5640a0ef33
* refactor(domain): add user type * fix(projections): start with login names * fix(login_policy): correct handling of user domain claimed event * fix(projections): add members * refactor: simplify member projections * add migration for members * add metadata to member projections * refactor: login name projection * fix: set correct suffixes on login name projections * test(projections): login name reduces * fix: correct cols in reduce member * test(projections): org, iam, project members * member additional cols and conds as opt, add project grant members * fix(migration): members * fix(migration): correct database name * migration version * migs * better naming for member cond and col * split project and project grant members * prepare member columns * feat(queries): membership query * test(queries): membership prepare * fix(queries): multiple projections for latest sequence * fix(api): use query for membership queries in auth and management * feat: org member queries * fix(api): use query for iam member calls * fix(queries): org members * fix(queries): project members * fix(queries): project grant members * refactor: remove unsued methods in repo-interfaces * refactor(eventstore): rename EventPusher to Command, EventReader to Event, PushEvents to Push and FilterEvents to Filter
166 lines
5.7 KiB
Go
166 lines
5.7 KiB
Go
package projection
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
"github.com/caos/zitadel/internal/eventstore/handler"
|
|
"github.com/caos/zitadel/internal/eventstore/handler/crdb"
|
|
"github.com/caos/zitadel/internal/repository/member"
|
|
"github.com/caos/zitadel/internal/repository/org"
|
|
"github.com/caos/zitadel/internal/repository/project"
|
|
"github.com/caos/zitadel/internal/repository/user"
|
|
)
|
|
|
|
type ProjectMemberProjection struct {
|
|
crdb.StatementHandler
|
|
}
|
|
|
|
const (
|
|
ProjectMemberProjectionTable = "zitadel.projections.project_members"
|
|
)
|
|
|
|
func NewProjectMemberProjection(ctx context.Context, config crdb.StatementHandlerConfig) *ProjectMemberProjection {
|
|
p := &ProjectMemberProjection{}
|
|
config.ProjectionName = ProjectMemberProjectionTable
|
|
config.Reducers = p.reducers()
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
return p
|
|
}
|
|
|
|
func (p *ProjectMemberProjection) reducers() []handler.AggregateReducer {
|
|
return []handler.AggregateReducer{
|
|
{
|
|
Aggregate: project.AggregateType,
|
|
EventRedusers: []handler.EventReducer{
|
|
{
|
|
Event: project.MemberAddedType,
|
|
Reduce: p.reduceAdded,
|
|
},
|
|
{
|
|
Event: project.MemberChangedType,
|
|
Reduce: p.reduceChanged,
|
|
},
|
|
{
|
|
Event: project.MemberCascadeRemovedType,
|
|
Reduce: p.reduceCascadeRemoved,
|
|
},
|
|
{
|
|
Event: project.MemberRemovedType,
|
|
Reduce: p.reduceRemoved,
|
|
},
|
|
{
|
|
Event: project.ProjectRemovedType,
|
|
Reduce: p.reduceProjectRemoved,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: user.AggregateType,
|
|
EventRedusers: []handler.EventReducer{
|
|
{
|
|
Event: user.UserRemovedType,
|
|
Reduce: p.reduceUserRemoved,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: org.AggregateType,
|
|
EventRedusers: []handler.EventReducer{
|
|
{
|
|
Event: org.OrgRemovedEventType,
|
|
Reduce: p.reduceOrgRemoved,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
type ProjectMemberColumn string
|
|
|
|
const (
|
|
ProjectMemberProjectIDCol = "project_id"
|
|
)
|
|
|
|
func (p *ProjectMemberProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.MemberAddedEvent)
|
|
if !ok {
|
|
logging.LogWithFields("HANDL-3FRys", "seq", event.Sequence(), "expectedType", project.MemberAddedType).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-bgx5Q", "reduce.wrong.event.type")
|
|
}
|
|
return reduceMemberAdded(
|
|
*member.NewMemberAddedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
|
withMemberCol(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
)
|
|
}
|
|
|
|
func (p *ProjectMemberProjection) reduceChanged(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.MemberChangedEvent)
|
|
if !ok {
|
|
logging.LogWithFields("HANDL-9hgMR", "seq", event.Sequence(), "expectedType", project.MemberChangedType).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-90WJ1", "reduce.wrong.event.type")
|
|
}
|
|
return reduceMemberChanged(
|
|
*member.NewMemberChangedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
)
|
|
}
|
|
|
|
func (p *ProjectMemberProjection) reduceCascadeRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.MemberCascadeRemovedEvent)
|
|
if !ok {
|
|
logging.LogWithFields("HANDL-2kyYa", "seq", event.Sequence(), "expectedType", project.MemberCascadeRemovedType).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-aGd43", "reduce.wrong.event.type")
|
|
}
|
|
return reduceMemberCascadeRemoved(
|
|
*member.NewCascadeRemovedEvent(&e.BaseEvent, e.UserID),
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
)
|
|
}
|
|
|
|
func (p *ProjectMemberProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.MemberRemovedEvent)
|
|
if !ok {
|
|
logging.LogWithFields("HANDL-X0yvM", "seq", event.Sequence(), "expectedType", project.MemberRemovedType).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-eJZPh", "reduce.wrong.event.type")
|
|
}
|
|
return reduceMemberRemoved(e,
|
|
withMemberCond(MemberUserIDCol, e.UserID),
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
)
|
|
}
|
|
|
|
func (p *ProjectMemberProjection) reduceUserRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*user.UserRemovedEvent)
|
|
if !ok {
|
|
logging.LogWithFields("HANDL-g8eWd", "seq", event.Sequence(), "expected", user.UserRemovedType).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-aYA60", "reduce.wrong.event.type")
|
|
}
|
|
return reduceMemberRemoved(e, withMemberCond(MemberUserIDCol, e.Aggregate().ID))
|
|
}
|
|
|
|
func (p *ProjectMemberProjection) reduceOrgRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
//TODO: as soon as org deletion is implemented:
|
|
// Case: The user has resource owner A and project has resource owner B
|
|
// if org B deleted it works
|
|
// if org A is deleted, the membership wouldn't be deleted
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
if !ok {
|
|
logging.LogWithFields("HANDL-q7H8D", "seq", event.Sequence(), "expected", org.OrgRemovedEventType).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-NGUEL", "reduce.wrong.event.type")
|
|
}
|
|
return reduceMemberRemoved(e, withMemberCond(MemberResourceOwner, e.Aggregate().ID))
|
|
}
|
|
|
|
func (p *ProjectMemberProjection) reduceProjectRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.ProjectRemovedEvent)
|
|
if !ok {
|
|
logging.LogWithFields("HANDL-q7H8D", "seq", event.Sequence(), "expected", project.ProjectRemovedType).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-NGUEL", "reduce.wrong.event.type")
|
|
}
|
|
return reduceMemberRemoved(e, withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID))
|
|
}
|