zitadel/internal/query/projection/idp_user_link.go
Silvan d2ea9a1b8c
feat: member queries (#2796)
* 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

* fix(query): member queries and user avatar column

* member cols

* fix(queries): membership stmt

* fix user test

* fix user test

* fix(membership): correct display name

* fix(projection): additional member manipulation events

* additional member tests

* fix(projections): additional events of idp links

* fix: use query for memberships (#2797)

* fix(api): use query for memberships

* remove comment

* handle err

* refactor(projections): idp user link user aggregate type

* fix(projections): handle old user events

* fix(api): add asset prefix

* no image for iam members
2021-12-16 13:25:38 +00:00

189 lines
6.2 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/iam"
"github.com/caos/zitadel/internal/repository/org"
"github.com/caos/zitadel/internal/repository/user"
)
type IDPUserLinkProjection struct {
crdb.StatementHandler
}
func NewIDPUserLinkProjection(ctx context.Context, config crdb.StatementHandlerConfig) *IDPUserLinkProjection {
p := &IDPUserLinkProjection{}
config.ProjectionName = IDPUserLinkTable
config.Reducers = p.reducers()
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *IDPUserLinkProjection) reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: user.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: user.UserIDPLinkAddedType,
Reduce: p.reduceAdded,
},
{
Event: user.UserIDPLinkCascadeRemovedType,
Reduce: p.reduceCascadeRemoved,
},
{
Event: user.UserIDPLinkRemovedType,
Reduce: p.reduceRemoved,
},
{
Event: user.UserRemovedType,
Reduce: p.reduceUserRemoved,
},
},
},
{
Aggregate: org.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: org.IDPConfigRemovedEventType,
Reduce: p.reduceIDPConfigRemoved,
},
{
Event: org.OrgRemovedEventType,
Reduce: p.reduceOrgRemoved,
},
},
},
{
Aggregate: iam.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: iam.IDPConfigRemovedEventType,
Reduce: p.reduceIDPConfigRemoved,
},
},
},
}
}
const (
IDPUserLinkTable = "zitadel.projections.idp_user_links"
IDPUserLinkIDPIDCol = "idp_id"
IDPUserLinkUserIDCol = "user_id"
IDPUserLinkExternalUserIDCol = "external_user_id"
IDPUserLinkCreationDateCol = "creation_date"
IDPUserLinkChangeDateCol = "change_date"
IDPUserLinkSequenceCol = "sequence"
IDPUserLinkResourceOwnerCol = "resource_owner"
IDPUserLinkDisplayNameCol = "display_name"
)
func (p *IDPUserLinkProjection) reduceAdded(event eventstore.EventReader) (*handler.Statement, error) {
e, ok := event.(*user.UserIDPLinkAddedEvent)
if !ok {
logging.LogWithFields("HANDL-v2qC3", "seq", event.Sequence(), "expectedType", user.UserIDPLinkAddedType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-DpmXq", "reduce.wrong.event.type")
}
return crdb.NewCreateStatement(e,
[]handler.Column{
handler.NewCol(IDPUserLinkIDPIDCol, e.IDPConfigID),
handler.NewCol(IDPUserLinkUserIDCol, e.Aggregate().ID),
handler.NewCol(IDPUserLinkExternalUserIDCol, e.ExternalUserID),
handler.NewCol(IDPUserLinkCreationDateCol, e.CreationDate()),
handler.NewCol(IDPUserLinkChangeDateCol, e.CreationDate()),
handler.NewCol(IDPUserLinkSequenceCol, e.Sequence()),
handler.NewCol(IDPUserLinkResourceOwnerCol, e.Aggregate().ResourceOwner),
handler.NewCol(IDPUserLinkDisplayNameCol, e.DisplayName),
},
), nil
}
func (p *IDPUserLinkProjection) reduceRemoved(event eventstore.EventReader) (*handler.Statement, error) {
e, ok := event.(*user.UserIDPLinkRemovedEvent)
if !ok {
logging.LogWithFields("HANDL-zX5m9", "seq", event.Sequence(), "expectedType", user.UserIDPLinkRemovedType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-AZmfJ", "reduce.wrong.event.type")
}
return crdb.NewDeleteStatement(e,
[]handler.Condition{
handler.NewCond(IDPUserLinkIDPIDCol, e.IDPConfigID),
handler.NewCond(IDPUserLinkUserIDCol, e.Aggregate().ID),
handler.NewCond(IDPUserLinkExternalUserIDCol, e.ExternalUserID),
},
), nil
}
func (p *IDPUserLinkProjection) reduceCascadeRemoved(event eventstore.EventReader) (*handler.Statement, error) {
e, ok := event.(*user.UserIDPLinkCascadeRemovedEvent)
if !ok {
logging.LogWithFields("HANDL-I0s2H", "seq", event.Sequence(), "expectedType", user.UserIDPLinkCascadeRemovedType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-jQpv9", "reduce.wrong.event.type")
}
return crdb.NewDeleteStatement(e,
[]handler.Condition{
handler.NewCond(IDPUserLinkIDPIDCol, e.IDPConfigID),
handler.NewCond(IDPUserLinkUserIDCol, e.Aggregate().ID),
handler.NewCond(IDPUserLinkExternalUserIDCol, e.ExternalUserID),
},
), nil
}
func (p *IDPUserLinkProjection) reduceOrgRemoved(event eventstore.EventReader) (*handler.Statement, error) {
e, ok := event.(*org.OrgRemovedEvent)
if !ok {
logging.LogWithFields("HANDL-zX5m9", "seq", event.Sequence(), "expectedType", org.OrgRemovedEventType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-AZmfJ", "reduce.wrong.event.type")
}
return crdb.NewDeleteStatement(e,
[]handler.Condition{
handler.NewCond(IDPUserLinkResourceOwnerCol, e.Aggregate().ID),
},
), nil
}
func (p *IDPUserLinkProjection) reduceUserRemoved(event eventstore.EventReader) (*handler.Statement, error) {
e, ok := event.(*user.UserRemovedEvent)
if !ok {
logging.LogWithFields("HANDL-yM6u6", "seq", event.Sequence(), "expectedType", user.UserRemovedType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-uwlWE", "reduce.wrong.event.type")
}
return crdb.NewDeleteStatement(e,
[]handler.Condition{
handler.NewCond(IDPUserLinkUserIDCol, e.Aggregate().ID),
},
), nil
}
func (p *IDPUserLinkProjection) reduceIDPConfigRemoved(event eventstore.EventReader) (*handler.Statement, error) {
var idpID string
switch e := event.(type) {
case *org.IDPConfigRemovedEvent:
idpID = e.ConfigID
case *iam.IDPConfigRemovedEvent:
idpID = e.ConfigID
default:
logging.LogWithFields("HANDL-7lZaf", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.IDPConfigRemovedEventType, iam.IDPConfigRemovedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-iCKSj", "reduce.wrong.event.type")
}
return crdb.NewDeleteStatement(event,
[]handler.Condition{
handler.NewCond(IDPUserLinkIDPIDCol, idpID),
handler.NewCond(IDPUserLinkResourceOwnerCol, event.Aggregate().ResourceOwner),
},
), nil
}