2020-05-11 08:16:27 +00:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-01 05:18:05 +00:00
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2020-05-11 08:16:27 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-06-05 05:50:04 +00:00
|
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
2020-05-28 04:49:22 +00:00
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
|
|
proj_es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
|
|
usr_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
2020-05-11 08:16:27 +00:00
|
|
|
"github.com/caos/zitadel/internal/usergrant/repository/eventsourcing/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func UserGrantByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
|
|
|
if id == "" {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-ols34", "id should be filled")
|
|
|
|
}
|
|
|
|
return UserGrantQuery(latestSequence).
|
|
|
|
AggregateIDFilter(id), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantQuery(latestSequence uint64) *es_models.SearchQuery {
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.UserGrantAggregate).
|
|
|
|
LatestSequenceFilter(latestSequence)
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:49:22 +00:00
|
|
|
func UserGrantUniqueQuery(resourceOwner, projectID, userID string) *es_models.SearchQuery {
|
|
|
|
grantID := resourceOwner + projectID + userID
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.UserGrantUniqueAggregate).
|
|
|
|
AggregateIDFilter(grantID).
|
|
|
|
OrderDesc().
|
|
|
|
SetLimit(1)
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:16:27 +00:00
|
|
|
func UserGrantAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, grant *model.UserGrant) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dis83", "existing grant should not be nil")
|
|
|
|
}
|
|
|
|
return aggCreator.NewAggregate(ctx, grant.AggregateID, model.UserGrantAggregate, model.UserGrantVersion, grant.Sequence)
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:49:22 +00:00
|
|
|
func UserGrantAddedAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, grant *model.UserGrant) ([]*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserGrantAggregate(ctx, aggCreator, grant)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
2020-05-28 04:49:22 +00:00
|
|
|
validationQuery := es_models.NewSearchQuery().
|
2020-06-05 05:50:04 +00:00
|
|
|
AggregateTypeFilter(usr_model.UserAggregate, org_es_model.OrgAggregate, proj_es_model.ProjectAggregate).
|
2020-07-08 11:56:37 +00:00
|
|
|
AggregateIDsFilter(grant.UserID, authz.GetCtxData(ctx).OrgID, grant.ProjectID)
|
2020-05-28 04:49:22 +00:00
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
validation := addUserGrantValidation(authz.GetCtxData(ctx).OrgID, grant)
|
2020-05-28 04:49:22 +00:00
|
|
|
agg, err = agg.SetPrecondition(validationQuery, validation).AppendEvent(model.UserGrantAdded, grant)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueAggregate, err := reservedUniqueUserGrantAggregate(ctx, aggCreator, grant)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*es_models.Aggregate{
|
|
|
|
agg,
|
|
|
|
uniqueAggregate,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func reservedUniqueUserGrantAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, grant *model.UserGrant) (*es_models.Aggregate, error) {
|
2020-07-08 11:56:37 +00:00
|
|
|
grantID := authz.GetCtxData(ctx).OrgID + grant.ProjectID + grant.UserID
|
2020-05-28 04:49:22 +00:00
|
|
|
aggregate, err := aggCreator.NewAggregate(ctx, grantID, model.UserGrantUniqueAggregate, model.UserGrantVersion, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregate, err = aggregate.AppendEvent(model.UserGrantReserved, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return aggregate.SetPrecondition(UserGrantUniqueQuery(authz.GetCtxData(ctx).OrgID, grant.ProjectID, grant.UserID), isEventValidation(aggregate, model.UserGrantReserved)), nil
|
2020-05-28 04:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func releasedUniqueUserGrantAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, grant *model.UserGrant) (aggregate *es_models.Aggregate, err error) {
|
|
|
|
grantID := grant.ResourceOwner + grant.ProjectID + grant.UserID
|
|
|
|
aggregate, err = aggCreator.NewAggregate(ctx, grantID, model.UserGrantUniqueAggregate, model.UserGrantVersion, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregate, err = aggregate.AppendEvent(model.UserGrantReleased, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return aggregate.SetPrecondition(UserGrantUniqueQuery(grant.ResourceOwner, grant.ProjectID, grant.UserID), isEventValidation(aggregate, model.UserGrantReleased)), nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 13:32:03 +00:00
|
|
|
func UserGrantChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.UserGrant, grant *model.UserGrant, cascade bool) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-05-11 08:16:27 +00:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-osl8x", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserGrantAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
changes := existing.Changes(grant)
|
2020-06-19 13:32:03 +00:00
|
|
|
if !cascade {
|
|
|
|
return agg.AppendEvent(model.UserGrantChanged, changes)
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserGrantCascadeChanged, changes)
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantDeactivatedAggregate(aggCreator *es_models.AggregateCreator, existing *model.UserGrant, grant *model.UserGrant) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-lo21s", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserGrantAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserGrantDeactivated, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantReactivatedAggregate(aggCreator *es_models.AggregateCreator, existing *model.UserGrant, grant *model.UserGrant) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-mks34", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserGrantAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserGrantReactivated, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 13:32:03 +00:00
|
|
|
func UserGrantRemovedAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, existing *model.UserGrant, grant *model.UserGrant, cascade bool) ([]*es_models.Aggregate, error) {
|
2020-05-28 04:49:22 +00:00
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-lo21s", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserGrantAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-19 13:32:03 +00:00
|
|
|
eventType := model.UserGrantRemoved
|
|
|
|
if cascade {
|
|
|
|
eventType = model.UserGrantCascadeRemoved
|
|
|
|
}
|
|
|
|
agg, err = agg.AppendEvent(eventType, nil)
|
2020-05-28 04:49:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uniqueAggregate, err := releasedUniqueUserGrantAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*es_models.Aggregate{
|
|
|
|
agg,
|
|
|
|
uniqueAggregate,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEventValidation(aggregate *es_models.Aggregate, eventType es_models.EventType) func(...*es_models.Event) error {
|
|
|
|
return func(events ...*es_models.Event) error {
|
|
|
|
if len(events) == 0 {
|
|
|
|
aggregate.PreviousSequence = 0
|
|
|
|
return nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
2020-05-28 04:49:22 +00:00
|
|
|
if events[0].Type == eventType {
|
|
|
|
return errors.ThrowPreconditionFailedf(nil, "EVENT-eJQqe", "user_grant is already %v", eventType)
|
|
|
|
}
|
|
|
|
aggregate.PreviousSequence = events[0].Sequence
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addUserGrantValidation(resourceOwner string, grant *model.UserGrant) func(...*es_models.Event) error {
|
|
|
|
return func(events ...*es_models.Event) error {
|
|
|
|
existsOrg := false
|
|
|
|
existsUser := false
|
|
|
|
project := new(proj_es_model.Project)
|
|
|
|
for _, event := range events {
|
|
|
|
switch event.AggregateType {
|
|
|
|
case usr_model.UserAggregate:
|
|
|
|
switch event.Type {
|
|
|
|
case usr_model.UserAdded, usr_model.UserRegistered:
|
|
|
|
existsUser = true
|
|
|
|
case usr_model.UserRemoved:
|
|
|
|
existsUser = false
|
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
case org_es_model.OrgAggregate:
|
2020-05-28 04:49:22 +00:00
|
|
|
switch event.Type {
|
2020-06-05 05:50:04 +00:00
|
|
|
case org_es_model.OrgAdded:
|
2020-05-28 04:49:22 +00:00
|
|
|
existsOrg = true
|
2020-06-05 05:50:04 +00:00
|
|
|
case org_es_model.OrgRemoved:
|
2020-05-28 04:49:22 +00:00
|
|
|
existsOrg = false
|
|
|
|
}
|
|
|
|
case proj_es_model.ProjectAggregate:
|
|
|
|
project.AppendEvent(event)
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 13:32:03 +00:00
|
|
|
if !existsOrg {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-3OfIm", "org doesn't exist")
|
2020-05-28 04:49:22 +00:00
|
|
|
}
|
2020-06-19 13:32:03 +00:00
|
|
|
if !existsUser {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-Sl8uS", "user doesn't exist")
|
|
|
|
}
|
2020-07-01 05:18:05 +00:00
|
|
|
return checkProjectConditions(resourceOwner, grant, project)
|
2020-05-28 04:49:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 06:04:43 +00:00
|
|
|
//TODO: rethink this function i know it's ugly.
|
2020-06-19 13:32:03 +00:00
|
|
|
func checkProjectConditions(resourceOwner string, grant *model.UserGrant, project *proj_es_model.Project) error {
|
2020-07-02 06:04:43 +00:00
|
|
|
if grant.ProjectID != project.AggregateID {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "EVENT-ixlMx", "project doesn't exist")
|
|
|
|
}
|
2020-06-23 12:47:47 +00:00
|
|
|
if project.State == int32(proj_model.ProjectStateRemoved) {
|
2020-06-19 13:32:03 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-Lxp0s", "project doesn't exist")
|
2020-05-28 04:49:22 +00:00
|
|
|
}
|
|
|
|
if resourceOwner == project.ResourceOwner {
|
|
|
|
return checkIfProjectHasRoles(grant.RoleKeys, project.Roles)
|
|
|
|
}
|
|
|
|
|
2020-06-19 13:32:03 +00:00
|
|
|
if _, projectGrant := proj_es_model.GetProjectGrantByOrgID(project.Grants, resourceOwner); projectGrant != nil {
|
2020-05-28 04:49:22 +00:00
|
|
|
return checkIfProjectGrantHasRoles(grant.RoleKeys, projectGrant.RoleKeys)
|
|
|
|
}
|
2020-06-19 13:32:03 +00:00
|
|
|
return nil
|
2020-05-28 04:49:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 13:32:03 +00:00
|
|
|
func checkIfProjectHasRoles(roles []string, existing []*proj_es_model.ProjectRole) error {
|
2020-05-28 04:49:22 +00:00
|
|
|
for _, roleKey := range roles {
|
|
|
|
if _, role := proj_es_model.GetProjectRole(existing, roleKey); role == nil {
|
2020-06-19 13:32:03 +00:00
|
|
|
return errors.ThrowPreconditionFailedf(nil, "EVENT-Lxp0s", "project doesn't have role %v", roleKey)
|
2020-05-28 04:49:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 13:32:03 +00:00
|
|
|
return nil
|
2020-05-28 04:49:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 13:32:03 +00:00
|
|
|
func checkIfProjectGrantHasRoles(roles []string, existing []string) error {
|
2020-05-28 04:49:22 +00:00
|
|
|
roleExists := false
|
|
|
|
for _, roleKey := range roles {
|
|
|
|
for _, existingRoleKey := range existing {
|
|
|
|
if roleKey == existingRoleKey {
|
|
|
|
roleExists = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !roleExists {
|
2020-06-19 13:32:03 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-LSpwi", "project grant doesn't have role")
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 13:32:03 +00:00
|
|
|
return nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|