2020-05-11 10:16:29 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/project/model"
|
|
|
|
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-06-15 12:50:39 +00:00
|
|
|
ProjectGrantKeyProjectID = "project_id"
|
|
|
|
ProjectGrantKeyGrantID = "grant_id"
|
|
|
|
ProjectGrantKeyOrgID = "org_id"
|
|
|
|
ProjectGrantKeyResourceOwner = "resource_owner"
|
|
|
|
ProjectGrantKeyName = "project_name"
|
2020-06-19 13:32:03 +00:00
|
|
|
ProjectGrantKeyRoleKeys = "granted_role_keys"
|
2020-05-11 10:16:29 +00:00
|
|
|
)
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
type ProjectGrantView struct {
|
2020-06-19 13:32:03 +00:00
|
|
|
GrantID string `json:"-" gorm:"column:grant_id;primary_key"`
|
|
|
|
ProjectID string `json:"-" gorm:"column:project_id"`
|
|
|
|
OrgID string `json:"-" gorm:"column:org_id"`
|
|
|
|
Name string `json:"name" gorm:"column:project_name"`
|
|
|
|
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
|
|
|
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
|
|
|
State int32 `json:"-" gorm:"column:project_state"`
|
|
|
|
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
|
|
|
|
ResourceOwnerName string `json:"-" gorm:"column:resource_owner_name"`
|
|
|
|
OrgName string `json:"-" gorm:"column:org_name"`
|
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
|
|
|
GrantedRoleKeys pq.StringArray `json:"-" gorm:"column:granted_role_keys"`
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGrant struct {
|
|
|
|
GrantID string `json:"grantId"`
|
|
|
|
GrantedOrgID string `json:"grantedOrgId"`
|
|
|
|
RoleKeys []string `json:"roleKeys"`
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func ProjectGrantFromModel(project *model.ProjectGrantView) *ProjectGrantView {
|
|
|
|
return &ProjectGrantView{
|
2020-06-19 13:32:03 +00:00
|
|
|
ProjectID: project.ProjectID,
|
|
|
|
OrgID: project.OrgID,
|
|
|
|
Name: project.Name,
|
|
|
|
ChangeDate: project.ChangeDate,
|
|
|
|
CreationDate: project.CreationDate,
|
|
|
|
State: int32(project.State),
|
|
|
|
ResourceOwner: project.ResourceOwner,
|
|
|
|
ResourceOwnerName: project.ResourceOwnerName,
|
|
|
|
OrgName: project.OrgName,
|
|
|
|
GrantID: project.GrantID,
|
|
|
|
GrantedRoleKeys: project.GrantedRoleKeys,
|
|
|
|
Sequence: project.Sequence,
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func ProjectGrantToModel(project *ProjectGrantView) *model.ProjectGrantView {
|
|
|
|
return &model.ProjectGrantView{
|
2020-06-19 13:32:03 +00:00
|
|
|
ProjectID: project.ProjectID,
|
|
|
|
OrgID: project.OrgID,
|
|
|
|
Name: project.Name,
|
|
|
|
ChangeDate: project.ChangeDate,
|
|
|
|
CreationDate: project.CreationDate,
|
|
|
|
State: model.ProjectState(project.State),
|
|
|
|
ResourceOwner: project.ResourceOwner,
|
|
|
|
ResourceOwnerName: project.ResourceOwnerName,
|
|
|
|
OrgName: project.OrgName,
|
|
|
|
GrantID: project.GrantID,
|
|
|
|
Sequence: project.Sequence,
|
|
|
|
GrantedRoleKeys: project.GrantedRoleKeys,
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func ProjectGrantsToModel(projects []*ProjectGrantView) []*model.ProjectGrantView {
|
|
|
|
result := make([]*model.ProjectGrantView, len(projects))
|
2020-05-11 10:16:29 +00:00
|
|
|
for i, p := range projects {
|
2020-06-15 12:50:39 +00:00
|
|
|
result[i] = ProjectGrantToModel(p)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (p *ProjectGrantView) AppendEvent(event *models.Event) (err error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
p.ChangeDate = event.CreationDate
|
|
|
|
p.Sequence = event.Sequence
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.ProjectGrantAdded:
|
2020-06-23 12:47:47 +00:00
|
|
|
p.State = int32(model.ProjectStateActive)
|
2020-05-11 10:16:29 +00:00
|
|
|
p.CreationDate = event.CreationDate
|
|
|
|
p.setRootData(event)
|
|
|
|
err = p.setProjectGrantData(event)
|
2020-06-19 13:32:03 +00:00
|
|
|
case es_model.ProjectGrantChanged, es_model.ProjectGrantCascadeChanged:
|
2020-05-11 10:16:29 +00:00
|
|
|
err = p.setProjectGrantData(event)
|
|
|
|
case es_model.ProjectGrantDeactivated:
|
2020-06-23 12:47:47 +00:00
|
|
|
p.State = int32(model.ProjectStateInactive)
|
2020-05-11 10:16:29 +00:00
|
|
|
case es_model.ProjectGrantReactivated:
|
2020-06-23 12:47:47 +00:00
|
|
|
p.State = int32(model.ProjectStateActive)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (p *ProjectGrantView) setRootData(event *models.Event) {
|
2020-05-11 10:16:29 +00:00
|
|
|
p.ProjectID = event.AggregateID
|
|
|
|
p.ResourceOwner = event.ResourceOwner
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (p *ProjectGrantView) setData(event *models.Event) error {
|
2020-05-11 10:16:29 +00:00
|
|
|
if err := json.Unmarshal(event.Data, p); err != nil {
|
|
|
|
logging.Log("EVEN-dlo92").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (p *ProjectGrantView) setProjectGrantData(event *models.Event) error {
|
2020-05-11 10:16:29 +00:00
|
|
|
grant := new(ProjectGrant)
|
|
|
|
err := grant.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if grant.GrantedOrgID != "" {
|
|
|
|
p.OrgID = grant.GrantedOrgID
|
|
|
|
}
|
|
|
|
p.GrantID = grant.GrantID
|
|
|
|
p.GrantedRoleKeys = grant.RoleKeys
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectGrant) SetData(event *models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, p); err != nil {
|
|
|
|
logging.Log("EVEN-dlo92").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return caos_errs.ThrowInternal(err, "MODEL-s9ols", "Could not unmarshal data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|