zitadel/internal/project/repository/view/model/project_grant.go
Florian Forster fa9f581d56
chore(v2): move to new org (#3499)
* chore: move to new org

* logging

* fix: org rename caos -> zitadel

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
2022-04-26 23:01:45 +00:00

101 lines
3.3 KiB
Go

package model
import (
"encoding/json"
"time"
"github.com/lib/pq"
"github.com/zitadel/logging"
caos_errs "github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
"github.com/zitadel/zitadel/internal/project/model"
"github.com/zitadel/zitadel/internal/repository/project"
)
const (
ProjectGrantKeyProjectID = "project_id"
ProjectGrantKeyGrantID = "grant_id"
ProjectGrantKeyOrgID = "org_id"
ProjectGrantKeyResourceOwner = "resource_owner"
ProjectGrantKeyName = "project_name"
ProjectGrantKeyRoleKeys = "granted_role_keys"
)
type ProjectGrantView struct {
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"`
}
type ProjectGrant struct {
GrantID string `json:"grantId"`
GrantedOrgID string `json:"grantedOrgId"`
RoleKeys []string `json:"roleKeys"`
InstanceID string `json:"instanceID"`
}
func (p *ProjectGrantView) AppendEvent(event *models.Event) (err error) {
p.ChangeDate = event.CreationDate
p.Sequence = event.Sequence
switch eventstore.EventType(event.Type) {
case project.GrantAddedType:
p.State = int32(model.ProjectStateActive)
p.CreationDate = event.CreationDate
p.setRootData(event)
err = p.setProjectGrantData(event)
case project.GrantChangedType, project.GrantCascadeChangedType:
err = p.setProjectGrantData(event)
case project.GrantDeactivatedType:
p.State = int32(model.ProjectStateInactive)
case project.GrantReactivatedType:
p.State = int32(model.ProjectStateActive)
}
return err
}
func (p *ProjectGrantView) setRootData(event *models.Event) {
p.ProjectID = event.AggregateID
p.ResourceOwner = event.ResourceOwner
}
func (p *ProjectGrantView) 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 err
}
return nil
}
func (p *ProjectGrantView) setProjectGrantData(event *models.Event) error {
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
}