2020-04-23 07:54:40 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/project/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ProjectVersion = "v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Project struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
State int32 `json:"-"`
|
|
|
|
Members []*ProjectMember `json:"-"`
|
|
|
|
Roles []*ProjectRole `json:"-"`
|
|
|
|
Applications []*Application `json:"-"`
|
|
|
|
Grants []*ProjectGrant `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) Changes(changed *Project) map[string]interface{} {
|
|
|
|
changes := make(map[string]interface{}, 1)
|
|
|
|
if changed.Name != "" && p.Name != changed.Name {
|
|
|
|
changes["name"] = changed.Name
|
|
|
|
}
|
|
|
|
return changes
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectFromModel(project *model.Project) *Project {
|
|
|
|
members := ProjectMembersFromModel(project.Members)
|
|
|
|
roles := ProjectRolesFromModel(project.Roles)
|
|
|
|
apps := AppsFromModel(project.Applications)
|
|
|
|
grants := GrantsFromModel(project.Grants)
|
|
|
|
return &Project{
|
2020-05-11 12:16:29 +02:00
|
|
|
ObjectRoot: project.ObjectRoot,
|
2020-04-23 07:54:40 +02:00
|
|
|
Name: project.Name,
|
|
|
|
State: int32(project.State),
|
|
|
|
Members: members,
|
|
|
|
Roles: roles,
|
|
|
|
Applications: apps,
|
|
|
|
Grants: grants,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectToModel(project *Project) *model.Project {
|
|
|
|
members := ProjectMembersToModel(project.Members)
|
|
|
|
roles := ProjectRolesToModel(project.Roles)
|
|
|
|
apps := AppsToModel(project.Applications)
|
|
|
|
grants := GrantsToModel(project.Grants)
|
|
|
|
return &model.Project{
|
2020-05-11 12:16:29 +02:00
|
|
|
ObjectRoot: project.ObjectRoot,
|
2020-04-23 07:54:40 +02:00
|
|
|
Name: project.Name,
|
|
|
|
State: model.ProjectState(project.State),
|
|
|
|
Members: members,
|
|
|
|
Roles: roles,
|
|
|
|
Applications: apps,
|
|
|
|
Grants: grants,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectFromEvents(project *Project, events ...*es_models.Event) (*Project, error) {
|
|
|
|
if project == nil {
|
|
|
|
project = &Project{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return project, project.AppendEvents(events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) AppendEvents(events ...*es_models.Event) error {
|
|
|
|
for _, event := range events {
|
|
|
|
if err := p.AppendEvent(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) AppendEvent(event *es_models.Event) error {
|
|
|
|
p.ObjectRoot.AppendEvent(event)
|
|
|
|
|
|
|
|
switch event.Type {
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectAdded, ProjectChanged:
|
|
|
|
return p.AppendAddProjectEvent(event)
|
|
|
|
case ProjectDeactivated:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendDeactivatedEvent()
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectReactivated:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendReactivatedEvent()
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectMemberAdded:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendAddMemberEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectMemberChanged:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendChangeMemberEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectMemberRemoved:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendRemoveMemberEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectRoleAdded:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendAddRoleEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectRoleChanged:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendChangeRoleEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectRoleRemoved:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendRemoveRoleEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ApplicationAdded:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendAddAppEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ApplicationChanged:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendChangeAppEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ApplicationRemoved:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendRemoveAppEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ApplicationDeactivated:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendAppStateEvent(event, model.APPSTATE_INACTIVE)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ApplicationReactivated:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendAppStateEvent(event, model.APPSTATE_ACTIVE)
|
2020-05-11 12:16:29 +02:00
|
|
|
case OIDCConfigAdded:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendAddOIDCConfigEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case OIDCConfigChanged, OIDCConfigSecretChanged:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendChangeOIDCConfigEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectGrantAdded:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendAddGrantEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectGrantChanged:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendChangeGrantEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectGrantDeactivated:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendGrantStateEvent(event, model.PROJECTGRANTSTATE_INACTIVE)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectGrantReactivated:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendGrantStateEvent(event, model.PROJECTGRANTSTATE_ACTIVE)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectGrantRemoved:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendRemoveGrantEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectGrantMemberAdded:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendAddGrantMemberEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectGrantMemberChanged:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendChangeGrantMemberEvent(event)
|
2020-05-11 12:16:29 +02:00
|
|
|
case ProjectGrantMemberRemoved:
|
2020-04-23 07:54:40 +02:00
|
|
|
return p.appendRemoveGrantMemberEvent(event)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:16:29 +02:00
|
|
|
func (p *Project) AppendAddProjectEvent(event *es_models.Event) error {
|
|
|
|
p.setData(event)
|
|
|
|
p.State = int32(model.PROJECTSTATE_ACTIVE)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func (p *Project) appendDeactivatedEvent() error {
|
|
|
|
p.State = int32(model.PROJECTSTATE_INACTIVE)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) appendReactivatedEvent() error {
|
|
|
|
p.State = int32(model.PROJECTSTATE_ACTIVE)
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
|
|
|
|
func (p *Project) setData(event *es_models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, p); err != nil {
|
|
|
|
logging.Log("EVEN-lo9sr").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|