zitadel/internal/query/projection/project.go
Silvan f3e6f3b23b
feat: remove org (#4148)
* feat(command): remove org

* refactor: imports, unused code, error handling

* reduce org removed in action

* add org deletion to projections

* add org removal to projections

* add org removal to projections

* org removed projection

* lint import

* projections

* fix: table names in tests

* fix: table names in tests

* logging

* add org state

* fix(domain): add Owner removed to object details

* feat(ListQuery): add with owner removed

* fix(org-delete): add bool to functions to select with owner removed

* fix(org-delete): add bools to user grants with events to determine if dependencies lost owner

* fix(org-delete): add unit tests for owner removed and org removed events

* fix(org-delete): add handling of org remove for grants and members

* fix(org-delete): correction of unit tests for owner removed

* fix(org-delete): update projections, unit tests and get functions

* fix(org-delete): add change date to authnkeys and owner removed to org metadata

* fix(org-delete): include owner removed for login names

* fix(org-delete): some column fixes in projections and build for queries with owner removed

* indexes

* fix(org-delete): include review changes

* fix(org-delete): change user projection name after merge

* fix(org-delete): include review changes for project grant where no project owner is necessary

* fix(org-delete): include auth and adminapi tables with owner removed information

* fix(org-delete): cleanup username and orgdomain uniqueconstraints when org is removed

* fix(org-delete): add permissions for org.remove

* remove unnecessary unique constraints

* fix column order in primary keys

* fix(org-delete): include review changes

* fix(org-delete): add owner removed indexes and chang setup step to create tables

* fix(org-delete): move PK order of instance_id and change added user_grant from review

* fix(org-delete): no params for prepareUserQuery

* change to step 6

* merge main

* fix(org-delete): OldUserName rename to private

* fix linting

* cleanup

* fix: remove org test

* create prerelease

* chore: delete org-delete as prerelease

Co-authored-by: Stefan Benz <stefan@caos.ch>
Co-authored-by: Livio Spring <livio.a@gmail.com>
Co-authored-by: Fabi <38692350+hifabienne@users.noreply.github.com>
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2022-11-30 17:01:17 +01:00

247 lines
8.9 KiB
Go

package projection
import (
"context"
"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"
"github.com/zitadel/zitadel/internal/repository/instance"
"github.com/zitadel/zitadel/internal/repository/org"
"github.com/zitadel/zitadel/internal/repository/project"
)
const (
ProjectProjectionTable = "projections.projects3"
ProjectColumnID = "id"
ProjectColumnCreationDate = "creation_date"
ProjectColumnChangeDate = "change_date"
ProjectColumnSequence = "sequence"
ProjectColumnState = "state"
ProjectColumnResourceOwner = "resource_owner"
ProjectColumnInstanceID = "instance_id"
ProjectColumnName = "name"
ProjectColumnProjectRoleAssertion = "project_role_assertion"
ProjectColumnProjectRoleCheck = "project_role_check"
ProjectColumnHasProjectCheck = "has_project_check"
ProjectColumnPrivateLabelingSetting = "private_labeling_setting"
ProjectColumnOwnerRemoved = "owner_removed"
)
type projectProjection struct {
crdb.StatementHandler
}
func newProjectProjection(ctx context.Context, config crdb.StatementHandlerConfig) *projectProjection {
p := new(projectProjection)
config.ProjectionName = ProjectProjectionTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(ProjectColumnID, crdb.ColumnTypeText),
crdb.NewColumn(ProjectColumnCreationDate, crdb.ColumnTypeTimestamp),
crdb.NewColumn(ProjectColumnChangeDate, crdb.ColumnTypeTimestamp),
crdb.NewColumn(ProjectColumnSequence, crdb.ColumnTypeInt64),
crdb.NewColumn(ProjectColumnState, crdb.ColumnTypeEnum),
crdb.NewColumn(ProjectColumnResourceOwner, crdb.ColumnTypeText),
crdb.NewColumn(ProjectColumnInstanceID, crdb.ColumnTypeText),
crdb.NewColumn(ProjectColumnName, crdb.ColumnTypeText),
crdb.NewColumn(ProjectColumnProjectRoleAssertion, crdb.ColumnTypeBool),
crdb.NewColumn(ProjectColumnProjectRoleCheck, crdb.ColumnTypeBool),
crdb.NewColumn(ProjectColumnHasProjectCheck, crdb.ColumnTypeBool),
crdb.NewColumn(ProjectColumnPrivateLabelingSetting, crdb.ColumnTypeEnum),
crdb.NewColumn(ProjectColumnOwnerRemoved, crdb.ColumnTypeBool, crdb.Default(false)),
},
crdb.NewPrimaryKey(ProjectColumnInstanceID, ProjectColumnID),
crdb.WithIndex(crdb.NewIndex("resource_owner", []string{ProjectColumnResourceOwner})),
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{ProjectColumnOwnerRemoved})),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *projectProjection) reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: project.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: project.ProjectAddedType,
Reduce: p.reduceProjectAdded,
},
{
Event: project.ProjectChangedType,
Reduce: p.reduceProjectChanged,
},
{
Event: project.ProjectDeactivatedType,
Reduce: p.reduceProjectDeactivated,
},
{
Event: project.ProjectReactivatedType,
Reduce: p.reduceProjectReactivated,
},
{
Event: project.ProjectRemovedType,
Reduce: p.reduceProjectRemoved,
},
},
},
{
Aggregate: org.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: org.OrgRemovedEventType,
Reduce: p.reduceOwnerRemoved,
},
},
},
{
Aggregate: instance.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: instance.InstanceRemovedEventType,
Reduce: reduceInstanceRemovedHelper(ProjectColumnInstanceID),
},
},
},
}
}
func (p *projectProjection) reduceProjectAdded(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*project.ProjectAddedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-l000S", "reduce.wrong.event.type %s", project.ProjectAddedType)
}
return crdb.NewCreateStatement(
e,
[]handler.Column{
handler.NewCol(ProjectColumnID, e.Aggregate().ID),
handler.NewCol(ProjectColumnCreationDate, e.CreationDate()),
handler.NewCol(ProjectColumnChangeDate, e.CreationDate()),
handler.NewCol(ProjectColumnResourceOwner, e.Aggregate().ResourceOwner),
handler.NewCol(ProjectColumnInstanceID, e.Aggregate().InstanceID),
handler.NewCol(ProjectColumnSequence, e.Sequence()),
handler.NewCol(ProjectColumnName, e.Name),
handler.NewCol(ProjectColumnProjectRoleAssertion, e.ProjectRoleAssertion),
handler.NewCol(ProjectColumnProjectRoleCheck, e.ProjectRoleCheck),
handler.NewCol(ProjectColumnHasProjectCheck, e.HasProjectCheck),
handler.NewCol(ProjectColumnPrivateLabelingSetting, e.PrivateLabelingSetting),
handler.NewCol(ProjectColumnState, domain.ProjectStateActive),
},
), nil
}
func (p *projectProjection) reduceProjectChanged(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*project.ProjectChangeEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-s00Fs", "reduce.wrong.event.type %s", project.ProjectChangedType)
}
if e.Name == nil && e.HasProjectCheck == nil && e.ProjectRoleAssertion == nil && e.ProjectRoleCheck == nil && e.PrivateLabelingSetting == nil {
return crdb.NewNoOpStatement(e), nil
}
columns := make([]handler.Column, 0, 7)
columns = append(columns, handler.NewCol(ProjectColumnChangeDate, e.CreationDate()),
handler.NewCol(ProjectColumnSequence, e.Sequence()))
if e.Name != nil {
columns = append(columns, handler.NewCol(ProjectColumnName, *e.Name))
}
if e.ProjectRoleAssertion != nil {
columns = append(columns, handler.NewCol(ProjectColumnProjectRoleAssertion, *e.ProjectRoleAssertion))
}
if e.ProjectRoleCheck != nil {
columns = append(columns, handler.NewCol(ProjectColumnProjectRoleCheck, *e.ProjectRoleCheck))
}
if e.HasProjectCheck != nil {
columns = append(columns, handler.NewCol(ProjectColumnHasProjectCheck, *e.HasProjectCheck))
}
if e.PrivateLabelingSetting != nil {
columns = append(columns, handler.NewCol(ProjectColumnPrivateLabelingSetting, *e.PrivateLabelingSetting))
}
return crdb.NewUpdateStatement(
e,
columns,
[]handler.Condition{
handler.NewCond(ProjectColumnID, e.Aggregate().ID),
handler.NewCond(ProjectColumnInstanceID, e.Aggregate().InstanceID),
},
), nil
}
func (p *projectProjection) reduceProjectDeactivated(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*project.ProjectDeactivatedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-LLp0f", "reduce.wrong.event.type %s", project.ProjectDeactivatedType)
}
return crdb.NewUpdateStatement(
e,
[]handler.Column{
handler.NewCol(ProjectColumnChangeDate, e.CreationDate()),
handler.NewCol(ProjectColumnSequence, e.Sequence()),
handler.NewCol(ProjectColumnState, domain.ProjectStateInactive),
},
[]handler.Condition{
handler.NewCond(ProjectColumnID, e.Aggregate().ID),
handler.NewCond(ProjectColumnInstanceID, e.Aggregate().InstanceID),
},
), nil
}
func (p *projectProjection) reduceProjectReactivated(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*project.ProjectReactivatedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-9J98f", "reduce.wrong.event.type %s", project.ProjectReactivatedType)
}
return crdb.NewUpdateStatement(
e,
[]handler.Column{
handler.NewCol(ProjectColumnChangeDate, e.CreationDate()),
handler.NewCol(ProjectColumnSequence, e.Sequence()),
handler.NewCol(ProjectColumnState, domain.ProjectStateActive),
},
[]handler.Condition{
handler.NewCond(ProjectColumnID, e.Aggregate().ID),
handler.NewCond(ProjectColumnInstanceID, e.Aggregate().InstanceID),
},
), nil
}
func (p *projectProjection) reduceProjectRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*project.ProjectRemovedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-5N9fs", "reduce.wrong.event.type %s", project.ProjectRemovedType)
}
return crdb.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(ProjectColumnID, e.Aggregate().ID),
handler.NewCond(ProjectColumnInstanceID, e.Aggregate().InstanceID),
},
), nil
}
func (p *projectProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*org.OrgRemovedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-sbgru", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
}
return crdb.NewUpdateStatement(
e,
[]handler.Column{
handler.NewCol(ProjectColumnChangeDate, e.CreationDate()),
handler.NewCol(ProjectColumnSequence, e.Sequence()),
handler.NewCol(ProjectColumnOwnerRemoved, true),
},
[]handler.Condition{
handler.NewCond(ProjectColumnInstanceID, e.Aggregate().InstanceID),
handler.NewCond(ProjectColumnResourceOwner, e.Aggregate().ID),
},
), nil
}