mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 01:37:31 +00:00
refactor: cleanup unused code (#7130)
* refactor: drop unused code * refactor: drop unused code
This commit is contained in:
@@ -1,231 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
http_util "github.com/zitadel/zitadel/internal/api/http"
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/zitadel/zitadel/internal/project/model"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
ApplicationKeyID = "id"
|
||||
ApplicationKeyProjectID = "project_id"
|
||||
ApplicationKeyResourceOwner = "resource_owner"
|
||||
ApplicationKeyOIDCClientID = "oidc_client_id"
|
||||
ApplicationKeyName = "app_name"
|
||||
)
|
||||
|
||||
type ApplicationView struct {
|
||||
ID string `json:"appId" gorm:"column:id;primary_key"`
|
||||
ProjectID string `json:"-" gorm:"column:project_id"`
|
||||
Name string `json:"name" gorm:"column:app_name"`
|
||||
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
||||
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
||||
State int32 `json:"-" gorm:"column:app_state"`
|
||||
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
|
||||
ProjectRoleAssertion bool `json:"projectRoleAssertion" gorm:"column:project_role_assertion"`
|
||||
ProjectRoleCheck bool `json:"projectRoleCheck" gorm:"column:project_role_check"`
|
||||
HasProjectCheck bool `json:"hasProjectCheck" gorm:"column:has_project_check"`
|
||||
PrivateLabelingSetting domain.PrivateLabelingSetting `json:"privateLabelingSetting" gorm:"column:private_labeling_setting"`
|
||||
|
||||
IsOIDC bool `json:"-" gorm:"column:is_oidc"`
|
||||
OIDCVersion int32 `json:"oidcVersion" gorm:"column:oidc_version"`
|
||||
OIDCClientID string `json:"clientId" gorm:"column:oidc_client_id"`
|
||||
OIDCRedirectUris database.TextArray[string] `json:"redirectUris" gorm:"column:oidc_redirect_uris"`
|
||||
OIDCResponseTypes database.Array[domain.OIDCResponseType] `json:"responseTypes" gorm:"column:oidc_response_types"`
|
||||
OIDCGrantTypes database.Array[domain.OIDCGrantType] `json:"grantTypes" gorm:"column:oidc_grant_types"`
|
||||
OIDCApplicationType int32 `json:"applicationType" gorm:"column:oidc_application_type"`
|
||||
OIDCAuthMethodType int32 `json:"authMethodType" gorm:"column:oidc_auth_method_type"`
|
||||
OIDCPostLogoutRedirectUris database.TextArray[string] `json:"postLogoutRedirectUris" gorm:"column:oidc_post_logout_redirect_uris"`
|
||||
NoneCompliant bool `json:"-" gorm:"column:none_compliant"`
|
||||
ComplianceProblems database.TextArray[string] `json:"-" gorm:"column:compliance_problems"`
|
||||
DevMode bool `json:"devMode" gorm:"column:dev_mode"`
|
||||
OriginAllowList database.TextArray[string] `json:"-" gorm:"column:origin_allow_list"`
|
||||
AdditionalOrigins database.TextArray[string] `json:"additionalOrigins" gorm:"column:additional_origins"`
|
||||
AccessTokenType int32 `json:"accessTokenType" gorm:"column:access_token_type"`
|
||||
AccessTokenRoleAssertion bool `json:"accessTokenRoleAssertion" gorm:"column:access_token_role_assertion"`
|
||||
IDTokenRoleAssertion bool `json:"idTokenRoleAssertion" gorm:"column:id_token_role_assertion"`
|
||||
IDTokenUserinfoAssertion bool `json:"idTokenUserinfoAssertion" gorm:"column:id_token_userinfo_assertion"`
|
||||
ClockSkew time.Duration `json:"clockSkew" gorm:"column:clock_skew"`
|
||||
|
||||
IsSAML bool `json:"-" gorm:"column:is_saml"`
|
||||
Metadata []byte `json:"metadata" gorm:"column:metadata"`
|
||||
MetadataURL string `json:"metadata_url" gorm:"column:metadata_url"`
|
||||
|
||||
Sequence uint64 `json:"-" gorm:"sequence"`
|
||||
}
|
||||
|
||||
func OIDCResponseTypesToModel(oidctypes []domain.OIDCResponseType) []model.OIDCResponseType {
|
||||
result := make([]model.OIDCResponseType, len(oidctypes))
|
||||
for i, t := range oidctypes {
|
||||
result[i] = model.OIDCResponseType(t)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func OIDCGrantTypesToModel(granttypes []domain.OIDCGrantType) []model.OIDCGrantType {
|
||||
result := make([]model.OIDCGrantType, len(granttypes))
|
||||
for i, t := range granttypes {
|
||||
result[i] = model.OIDCGrantType(t)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (a *ApplicationView) AppendEventIfMyApp(event *models.Event) (err error) {
|
||||
view := new(ApplicationView)
|
||||
switch event.Type() {
|
||||
case project.ApplicationAddedType:
|
||||
err = view.SetData(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case project.ApplicationChangedType,
|
||||
project.OIDCConfigAddedType,
|
||||
project.OIDCConfigChangedType,
|
||||
project.APIConfigAddedType,
|
||||
project.APIConfigChangedType,
|
||||
project.ApplicationDeactivatedType,
|
||||
project.ApplicationReactivatedType,
|
||||
project.SAMLConfigAddedType,
|
||||
project.SAMLConfigChangedType:
|
||||
err = view.SetData(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case project.ApplicationRemovedType:
|
||||
err = view.SetData(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case project.ProjectChangedType:
|
||||
return a.AppendEvent(event)
|
||||
case project.ProjectRemovedType:
|
||||
return a.AppendEvent(event)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
if view.ID == a.ID {
|
||||
return a.AppendEvent(event)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ApplicationView) AppendEvent(event *models.Event) (err error) {
|
||||
a.Sequence = event.Seq
|
||||
a.ChangeDate = event.CreationDate
|
||||
switch event.Type() {
|
||||
case project.ApplicationAddedType:
|
||||
a.setRootData(event)
|
||||
a.CreationDate = event.CreationDate
|
||||
a.ResourceOwner = event.ResourceOwner
|
||||
err = a.SetData(event)
|
||||
case project.OIDCConfigAddedType:
|
||||
a.IsOIDC = true
|
||||
err = a.SetData(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.setCompliance()
|
||||
return a.setOriginAllowList()
|
||||
case project.SAMLConfigAddedType:
|
||||
a.IsSAML = true
|
||||
return a.SetData(event)
|
||||
case project.APIConfigAddedType:
|
||||
a.IsOIDC = false
|
||||
return a.SetData(event)
|
||||
case project.ApplicationChangedType:
|
||||
return a.SetData(event)
|
||||
case project.OIDCConfigChangedType:
|
||||
err = a.SetData(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.setCompliance()
|
||||
return a.setOriginAllowList()
|
||||
case project.SAMLConfigChangedType:
|
||||
return a.SetData(event)
|
||||
case project.APIConfigChangedType:
|
||||
return a.SetData(event)
|
||||
case project.ProjectChangedType:
|
||||
return a.setProjectChanges(event)
|
||||
case project.ApplicationDeactivatedType:
|
||||
a.State = int32(model.AppStateInactive)
|
||||
case project.ApplicationReactivatedType:
|
||||
a.State = int32(model.AppStateActive)
|
||||
case project.ApplicationRemovedType, project.ProjectRemovedType:
|
||||
a.State = int32(model.AppStateRemoved)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *ApplicationView) setRootData(event *models.Event) {
|
||||
a.ProjectID = event.AggregateID
|
||||
}
|
||||
|
||||
func (a *ApplicationView) SetData(event *models.Event) error {
|
||||
if err := json.Unmarshal(event.Data, a); err != nil {
|
||||
logging.Log("EVEN-lo9ds").WithError(err).Error("could not unmarshal event data")
|
||||
return zerrors.ThrowInternal(err, "MODEL-8suie", "Could not unmarshal data")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ApplicationView) setOriginAllowList() error {
|
||||
allowList := make(database.TextArray[string], 0)
|
||||
for _, redirect := range a.OIDCRedirectUris {
|
||||
origin, err := http_util.GetOriginFromURLString(redirect)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !http_util.IsOriginAllowed(allowList, origin) {
|
||||
allowList = append(allowList, origin)
|
||||
}
|
||||
}
|
||||
for _, origin := range a.AdditionalOrigins {
|
||||
if !http_util.IsOriginAllowed(allowList, origin) {
|
||||
allowList = append(allowList, origin)
|
||||
}
|
||||
}
|
||||
a.OriginAllowList = allowList
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ApplicationView) setCompliance() {
|
||||
compliance := model.GetOIDCCompliance(model.OIDCVersion(a.OIDCVersion), model.OIDCApplicationType(a.OIDCApplicationType), OIDCGrantTypesToModel(a.OIDCGrantTypes), OIDCResponseTypesToModel(a.OIDCResponseTypes), model.OIDCAuthMethodType(a.OIDCAuthMethodType), a.OIDCRedirectUris)
|
||||
a.NoneCompliant = compliance.NoneCompliant
|
||||
a.ComplianceProblems = compliance.Problems
|
||||
}
|
||||
|
||||
func (a *ApplicationView) setProjectChanges(event *models.Event) error {
|
||||
changes := struct {
|
||||
ProjectRoleAssertion *bool `json:"projectRoleAssertion,omitempty"`
|
||||
ProjectRoleCheck *bool `json:"projectRoleCheck,omitempty"`
|
||||
HasProjectCheck *bool `json:"hasProjectCheck,omitempty"`
|
||||
PrivateLabelingSetting *domain.PrivateLabelingSetting `json:"privateLabelingSetting,omitempty"`
|
||||
}{}
|
||||
if err := json.Unmarshal(event.Data, &changes); err != nil {
|
||||
logging.Log("EVEN-DFbfg").WithError(err).Error("could not unmarshal event data")
|
||||
return zerrors.ThrowInternal(err, "MODEL-Bw221", "Could not unmarshal data")
|
||||
}
|
||||
if changes.ProjectRoleAssertion != nil {
|
||||
a.ProjectRoleAssertion = *changes.ProjectRoleAssertion
|
||||
}
|
||||
if changes.ProjectRoleCheck != nil {
|
||||
a.ProjectRoleCheck = *changes.ProjectRoleCheck
|
||||
}
|
||||
if changes.HasProjectCheck != nil {
|
||||
a.HasProjectCheck = *changes.HasProjectCheck
|
||||
}
|
||||
if changes.PrivateLabelingSetting != nil {
|
||||
a.PrivateLabelingSetting = *changes.PrivateLabelingSetting
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -1,102 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/zitadel/zitadel/internal/project/model"
|
||||
es_model "github.com/zitadel/zitadel/internal/project/repository/eventsourcing/model"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
)
|
||||
|
||||
func mockAppData(app *es_model.Application) []byte {
|
||||
data, _ := json.Marshal(app)
|
||||
return data
|
||||
}
|
||||
|
||||
func mockOIDCConfigData(config *es_model.OIDCConfig) []byte {
|
||||
data, _ := json.Marshal(config)
|
||||
return data
|
||||
}
|
||||
|
||||
func TestApplicationAppendEvent(t *testing.T) {
|
||||
type args struct {
|
||||
event *es_models.Event
|
||||
app *ApplicationView
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
result *ApplicationView
|
||||
}{
|
||||
{
|
||||
name: "append added app event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ApplicationAddedType, Data: mockAppData(&es_model.Application{Name: "AppName"})},
|
||||
app: &ApplicationView{},
|
||||
},
|
||||
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
||||
},
|
||||
{
|
||||
name: "append changed app event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ApplicationChangedType, Data: mockAppData(&es_model.Application{Name: "AppNameChanged"})},
|
||||
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
||||
},
|
||||
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppNameChanged", State: int32(model.AppStateActive)},
|
||||
},
|
||||
{
|
||||
name: "append deactivate app event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ApplicationDeactivatedType},
|
||||
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
||||
},
|
||||
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateInactive)},
|
||||
},
|
||||
{
|
||||
name: "append reactivate app event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ApplicationReactivatedType},
|
||||
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateInactive)},
|
||||
},
|
||||
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
||||
},
|
||||
{
|
||||
name: "append added oidc config event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.OIDCConfigAddedType, Data: mockOIDCConfigData(&es_model.OIDCConfig{ClientID: "clientID"})},
|
||||
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
||||
},
|
||||
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", IsOIDC: true, OIDCClientID: "clientID", State: int32(model.AppStateActive)},
|
||||
},
|
||||
{
|
||||
name: "append changed oidc config event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.OIDCConfigAddedType, Data: mockOIDCConfigData(&es_model.OIDCConfig{ClientID: "clientIDChanged"})},
|
||||
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", OIDCClientID: "clientID", State: int32(model.AppStateActive)},
|
||||
},
|
||||
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", IsOIDC: true, OIDCClientID: "clientIDChanged", State: int32(model.AppStateActive)},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.args.app.AppendEvent(tt.args.event)
|
||||
if tt.args.app.ProjectID != tt.result.ProjectID {
|
||||
t.Errorf("got wrong result projectID: expected: %v, actual: %v ", tt.result.ProjectID, tt.args.app.ProjectID)
|
||||
}
|
||||
if tt.args.app.Name != tt.result.Name {
|
||||
t.Errorf("got wrong result name: expected: %v, actual: %v ", tt.result.Name, tt.args.app.Name)
|
||||
}
|
||||
if tt.args.app.State != tt.result.State {
|
||||
t.Errorf("got wrong result state: expected: %v, actual: %v ", tt.result.State, tt.args.app.State)
|
||||
}
|
||||
if tt.args.app.IsOIDC != tt.result.IsOIDC {
|
||||
t.Errorf("got wrong result IsOIDC: expected: %v, actual: %v ", tt.result.IsOIDC, tt.args.app.IsOIDC)
|
||||
}
|
||||
if tt.args.app.OIDCClientID != tt.result.OIDCClientID {
|
||||
t.Errorf("got wrong result OIDCClientID: expected: %v, actual: %v ", tt.result.OIDCClientID, tt.args.app.OIDCClientID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
package model
|
||||
|
||||
const (
|
||||
OrgProjectMappingKeyProjectID = "project_id"
|
||||
OrgProjectMappingKeyOrgID = "org_id"
|
||||
OrgProjectMappingKeyProjectGrantID = "project_grant_id"
|
||||
OrgProjectMappingKeyInstanceID = "instance_id"
|
||||
OrgProjectMappingOwnerRemoved = "owner_removed"
|
||||
)
|
||||
|
||||
type OrgProjectMapping struct {
|
||||
ProjectID string `json:"-" gorm:"column:project_id;primary_key"`
|
||||
OrgID string `json:"-" gorm:"column:org_id;primary_key"`
|
||||
ProjectGrantID string `json:"-" gorm:"column:project_grant_id"`
|
||||
InstanceID string `json:"instanceID" gorm:"column:instance_id"`
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
proj_model "github.com/zitadel/zitadel/internal/project/model"
|
||||
"github.com/zitadel/zitadel/internal/view/repository"
|
||||
)
|
||||
|
||||
type OrgProjectMappingSearchRequest proj_model.OrgProjectMappingViewSearchRequest
|
||||
type OrgProjectMappingSearchQuery proj_model.OrgProjectMappingViewSearchQuery
|
||||
type OrgProjectMappingSearchKey proj_model.OrgProjectMappingViewSearchKey
|
||||
|
||||
func (req OrgProjectMappingSearchRequest) GetLimit() uint64 {
|
||||
return req.Limit
|
||||
}
|
||||
|
||||
func (req OrgProjectMappingSearchRequest) GetOffset() uint64 {
|
||||
return req.Offset
|
||||
}
|
||||
|
||||
func (req OrgProjectMappingSearchRequest) GetSortingColumn() repository.ColumnKey {
|
||||
if req.SortingColumn == proj_model.OrgProjectMappingSearchKeyUnspecified {
|
||||
return nil
|
||||
}
|
||||
return OrgProjectMappingSearchKey(req.SortingColumn)
|
||||
}
|
||||
|
||||
func (req OrgProjectMappingSearchRequest) GetAsc() bool {
|
||||
return req.Asc
|
||||
}
|
||||
|
||||
func (req OrgProjectMappingSearchRequest) GetQueries() []repository.SearchQuery {
|
||||
result := make([]repository.SearchQuery, len(req.Queries))
|
||||
for i, q := range req.Queries {
|
||||
result[i] = OrgProjectMappingSearchQuery{Key: q.Key, Value: q.Value, Method: q.Method}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (req OrgProjectMappingSearchQuery) GetKey() repository.ColumnKey {
|
||||
return OrgProjectMappingSearchKey(req.Key)
|
||||
}
|
||||
|
||||
func (req OrgProjectMappingSearchQuery) GetMethod() domain.SearchMethod {
|
||||
return req.Method
|
||||
}
|
||||
|
||||
func (req OrgProjectMappingSearchQuery) GetValue() interface{} {
|
||||
return req.Value
|
||||
}
|
||||
|
||||
func (key OrgProjectMappingSearchKey) ToColumnName() string {
|
||||
switch proj_model.OrgProjectMappingViewSearchKey(key) {
|
||||
case proj_model.OrgProjectMappingSearchKeyOrgID:
|
||||
return OrgProjectMappingKeyOrgID
|
||||
case proj_model.OrgProjectMappingSearchKeyProjectID:
|
||||
return OrgProjectMappingKeyProjectID
|
||||
case proj_model.OrgProjectMappingSearchKeyProjectGrantID:
|
||||
return OrgProjectMappingKeyProjectGrantID
|
||||
case proj_model.OrgProjectMappingSearchKeyInstanceID:
|
||||
return OrgProjectMappingKeyInstanceID
|
||||
case proj_model.OrgProjectMappingSearchKeyOwnerRemoved:
|
||||
return OrgProjectMappingOwnerRemoved
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
@@ -1,81 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/zitadel/zitadel/internal/project/model"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
ProjectKeyProjectID = "project_id"
|
||||
ProjectKeyResourceOwner = "resource_owner"
|
||||
ProjectKeyName = "project_name"
|
||||
)
|
||||
|
||||
type ProjectView struct {
|
||||
ProjectID string `json:"-" gorm:"column:project_id;primary_key"`
|
||||
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"`
|
||||
ProjectRoleAssertion bool `json:"projectRoleAssertion" gorm:"column:project_role_assertion"`
|
||||
ProjectRoleCheck bool `json:"projectRoleCheck" gorm:"column:project_role_check"`
|
||||
HasProjectCheck bool `json:"hasProjectCheck" gorm:"column:has_project_check"`
|
||||
PrivateLabelingSetting domain.PrivateLabelingSetting `json:"privateLabelingSetting" gorm:"column:private_labeling_setting"`
|
||||
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
||||
}
|
||||
|
||||
func (p *ProjectView) AppendEvent(event *models.Event) (err error) {
|
||||
p.ChangeDate = event.CreationDate
|
||||
p.Sequence = event.Seq
|
||||
switch event.Type() {
|
||||
case project.ProjectAddedType:
|
||||
p.State = int32(model.ProjectStateActive)
|
||||
p.CreationDate = event.CreationDate
|
||||
p.setRootData(event)
|
||||
err = p.setData(event)
|
||||
case project.ProjectChangedType:
|
||||
err = p.setData(event)
|
||||
case project.ProjectDeactivatedType:
|
||||
p.State = int32(model.ProjectStateInactive)
|
||||
case project.ProjectReactivatedType:
|
||||
p.State = int32(model.ProjectStateActive)
|
||||
case project.ProjectRemovedType:
|
||||
p.State = int32(model.ProjectStateRemoved)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *ProjectView) setRootData(event *models.Event) {
|
||||
p.ProjectID = event.AggregateID
|
||||
p.ResourceOwner = event.ResourceOwner
|
||||
}
|
||||
|
||||
func (p *ProjectView) 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 *ProjectView) setProjectData(event *models.Event) error {
|
||||
project := new(ProjectView)
|
||||
return project.SetData(event)
|
||||
}
|
||||
|
||||
func (p *ProjectView) SetData(event *models.Event) error {
|
||||
if err := json.Unmarshal(event.Data, p); err != nil {
|
||||
logging.Log("EVEN-sk9Sj").WithError(err).Error("could not unmarshal event data")
|
||||
return zerrors.ThrowInternal(err, "MODEL-s9ols", "Could not unmarshal data")
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -1,99 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/zitadel/zitadel/internal/project/model"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
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 database.TextArray[string] `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.Seq
|
||||
switch 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 zerrors.ThrowInternal(err, "MODEL-s9ols", "Could not unmarshal data")
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -1,68 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
ProjectGrantMemberKeyUserID = "user_id"
|
||||
ProjectGrantMemberKeyGrantID = "grant_id"
|
||||
ProjectGrantMemberKeyProjectID = "project_id"
|
||||
ProjectGrantMemberKeyUserName = "user_name"
|
||||
ProjectGrantMemberKeyEmail = "email"
|
||||
ProjectGrantMemberKeyFirstName = "first_name"
|
||||
ProjectGrantMemberKeyLastName = "last_name"
|
||||
)
|
||||
|
||||
type ProjectGrantMemberView struct {
|
||||
UserID string `json:"userId" gorm:"column:user_id;primary_key"`
|
||||
GrantID string `json:"grantId" gorm:"column:grant_id;primary_key"`
|
||||
ProjectID string `json:"-" gorm:"column:project_id"`
|
||||
UserName string `json:"-" gorm:"column:user_name"`
|
||||
Email string `json:"-" gorm:"column:email_address"`
|
||||
FirstName string `json:"-" gorm:"column:first_name"`
|
||||
LastName string `json:"-" gorm:"column:last_name"`
|
||||
DisplayName string `json:"-" gorm:"column:display_name"`
|
||||
Roles database.TextArray[string] `json:"roles" gorm:"column:roles"`
|
||||
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
||||
PreferredLoginName string `json:"-" gorm:"column:preferred_login_name"`
|
||||
AvatarKey string `json:"-" gorm:"column:avatar_key"`
|
||||
UserResourceOwner string `json:"-" gorm:"column:user_resource_owner"`
|
||||
|
||||
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
||||
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
||||
}
|
||||
|
||||
func (r *ProjectGrantMemberView) AppendEvent(event *models.Event) (err error) {
|
||||
r.Sequence = event.Seq
|
||||
r.ChangeDate = event.CreationDate
|
||||
switch event.Type() {
|
||||
case project.GrantMemberAddedType:
|
||||
r.setRootData(event)
|
||||
r.CreationDate = event.CreationDate
|
||||
err = r.SetData(event)
|
||||
case project.GrantMemberChangedType:
|
||||
err = r.SetData(event)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ProjectGrantMemberView) setRootData(event *models.Event) {
|
||||
r.ProjectID = event.AggregateID
|
||||
}
|
||||
|
||||
func (r *ProjectGrantMemberView) SetData(event *models.Event) error {
|
||||
if err := json.Unmarshal(event.Data, r); err != nil {
|
||||
logging.Log("EVEN-slo9s").WithError(err).Error("could not unmarshal event data")
|
||||
return zerrors.ThrowInternal(err, "MODEL-0plew", "Could not unmarshal data")
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -1,62 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
es_model "github.com/zitadel/zitadel/internal/project/repository/eventsourcing/model"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
)
|
||||
|
||||
func mockProjectGrantMemberData(member *es_model.ProjectGrantMember) []byte {
|
||||
data, _ := json.Marshal(member)
|
||||
return data
|
||||
}
|
||||
|
||||
func TestGrantedProjectMemberAppendEvent(t *testing.T) {
|
||||
type args struct {
|
||||
event *es_models.Event
|
||||
member *ProjectGrantMemberView
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
result *ProjectGrantMemberView
|
||||
}{
|
||||
{
|
||||
name: "append added member event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantMemberAddedType, ResourceOwner: "OrgID", Data: mockProjectGrantMemberData(&es_model.ProjectGrantMember{GrantID: "ProjectGrantID", UserID: "UserID", Roles: []string{"Role"}})},
|
||||
member: &ProjectGrantMemberView{},
|
||||
},
|
||||
result: &ProjectGrantMemberView{ProjectID: "AggregateID", UserID: "UserID", GrantID: "ProjectGrantID", Roles: []string{"Role"}},
|
||||
},
|
||||
{
|
||||
name: "append changed member event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantMemberAddedType, ResourceOwner: "OrgID", Data: mockProjectGrantMemberData(&es_model.ProjectGrantMember{GrantID: "ProjectGrantID", Roles: []string{"RoleChanged"}})},
|
||||
member: &ProjectGrantMemberView{ProjectID: "AggregateID", UserID: "UserID", GrantID: "ProjectGrantID", Roles: []string{"Role"}},
|
||||
},
|
||||
result: &ProjectGrantMemberView{ProjectID: "AggregateID", UserID: "UserID", GrantID: "ProjectGrantID", Roles: []string{"RoleChanged"}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.args.member.AppendEvent(tt.args.event)
|
||||
if tt.args.member.ProjectID != tt.result.ProjectID {
|
||||
t.Errorf("got wrong result projectID: expected: %v, actual: %v ", tt.result.ProjectID, tt.args.member.ProjectID)
|
||||
}
|
||||
if tt.args.member.UserID != tt.result.UserID {
|
||||
t.Errorf("got wrong result userID: expected: %v, actual: %v ", tt.result.UserID, tt.args.member.UserID)
|
||||
}
|
||||
if tt.args.member.GrantID != tt.result.GrantID {
|
||||
t.Errorf("got wrong result ProjectGrantID: expected: %v, actual: %v ", tt.result.GrantID, tt.args.member.GrantID)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.args.member.Roles, tt.result.Roles) {
|
||||
t.Errorf("got wrong result Roles: expected: %v, actual: %v ", tt.result.Roles, tt.args.member.Roles)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,90 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/zitadel/zitadel/internal/project/model"
|
||||
es_model "github.com/zitadel/zitadel/internal/project/repository/eventsourcing/model"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
)
|
||||
|
||||
func mockProjectData(project *es_model.Project) []byte {
|
||||
data, _ := json.Marshal(project)
|
||||
return data
|
||||
}
|
||||
|
||||
func mockProjectGrantData(grant *es_model.ProjectGrant) []byte {
|
||||
data, _ := json.Marshal(grant)
|
||||
return data
|
||||
}
|
||||
|
||||
func TestProjectGrantAppendEvent(t *testing.T) {
|
||||
type args struct {
|
||||
event *es_models.Event
|
||||
project *ProjectGrantView
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
result *ProjectGrantView
|
||||
}{
|
||||
{
|
||||
name: "append added project grant event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantAddedType, ResourceOwner: "GrantedOrgID", Data: mockProjectGrantData(&es_model.ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "GrantedOrgID", RoleKeys: []string{"Role"}})},
|
||||
project: &ProjectGrantView{},
|
||||
},
|
||||
result: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"Role"}},
|
||||
},
|
||||
{
|
||||
name: "append change project grant event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantChangedType, ResourceOwner: "GrantedOrgID", Data: mockProjectGrantData(&es_model.ProjectGrant{GrantID: "ProjectGrantID", RoleKeys: []string{"RoleChanged"}})},
|
||||
project: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"Role"}},
|
||||
},
|
||||
result: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"RoleChanged"}},
|
||||
},
|
||||
{
|
||||
name: "append deactivate project grant event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantDeactivatedType, ResourceOwner: "GrantedOrgID", Data: mockProjectGrantData(&es_model.ProjectGrant{GrantID: "ProjectGrantID"})},
|
||||
project: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"Role"}},
|
||||
},
|
||||
result: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateInactive), GrantedRoleKeys: []string{"Role"}},
|
||||
},
|
||||
{
|
||||
name: "append reactivate project grant event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantReactivatedType, ResourceOwner: "GrantedOrgID", Data: mockProjectGrantData(&es_model.ProjectGrant{GrantID: "ProjectGrantID"})},
|
||||
project: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateInactive), GrantedRoleKeys: []string{"Role"}},
|
||||
},
|
||||
result: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"Role"}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.args.project.AppendEvent(tt.args.event)
|
||||
if tt.args.project.ProjectID != tt.result.ProjectID {
|
||||
t.Errorf("got wrong result projectID: expected: %v, actual: %v ", tt.result.ProjectID, tt.args.project.ProjectID)
|
||||
}
|
||||
if tt.args.project.OrgID != tt.result.OrgID {
|
||||
t.Errorf("got wrong result orgID: expected: %v, actual: %v ", tt.result.OrgID, tt.args.project.OrgID)
|
||||
}
|
||||
if tt.args.project.ResourceOwner != tt.result.ResourceOwner {
|
||||
t.Errorf("got wrong result ResourceOwner: expected: %v, actual: %v ", tt.result.ResourceOwner, tt.args.project.ResourceOwner)
|
||||
}
|
||||
if tt.args.project.Name != tt.result.Name {
|
||||
t.Errorf("got wrong result name: expected: %v, actual: %v ", tt.result.Name, tt.args.project.Name)
|
||||
}
|
||||
if tt.args.project.State != tt.result.State {
|
||||
t.Errorf("got wrong result state: expected: %v, actual: %v ", tt.result.State, tt.args.project.State)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.args.project.GrantedRoleKeys, tt.result.GrantedRoleKeys) {
|
||||
t.Errorf("got wrong result state: expected: %v, actual: %v ", tt.result.GrantedRoleKeys, tt.args.project.GrantedRoleKeys)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,66 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
ProjectMemberKeyUserID = "user_id"
|
||||
ProjectMemberKeyProjectID = "project_id"
|
||||
ProjectMemberKeyUserName = "user_name"
|
||||
ProjectMemberKeyEmail = "email"
|
||||
ProjectMemberKeyFirstName = "first_name"
|
||||
ProjectMemberKeyLastName = "last_name"
|
||||
)
|
||||
|
||||
type ProjectMemberView struct {
|
||||
UserID string `json:"userId" gorm:"column:user_id;primary_key"`
|
||||
ProjectID string `json:"-" gorm:"column:project_id;primary_key"`
|
||||
UserName string `json:"-" gorm:"column:user_name"`
|
||||
Email string `json:"-" gorm:"column:email_address"`
|
||||
FirstName string `json:"-" gorm:"column:first_name"`
|
||||
LastName string `json:"-" gorm:"column:last_name"`
|
||||
DisplayName string `json:"-" gorm:"column:display_name"`
|
||||
Roles database.TextArray[string] `json:"roles" gorm:"column:roles"`
|
||||
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
||||
PreferredLoginName string `json:"-" gorm:"column:preferred_login_name"`
|
||||
AvatarKey string `json:"-" gorm:"column:avatar_key"`
|
||||
UserResourceOwner string `json:"-" gorm:"column:user_resource_owner"`
|
||||
|
||||
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
||||
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
||||
}
|
||||
|
||||
func (r *ProjectMemberView) AppendEvent(event *models.Event) (err error) {
|
||||
r.Sequence = event.Seq
|
||||
r.ChangeDate = event.CreationDate
|
||||
switch event.Type() {
|
||||
case project.MemberAddedType:
|
||||
r.setRootData(event)
|
||||
r.CreationDate = event.CreationDate
|
||||
err = r.SetData(event)
|
||||
case project.MemberChangedType:
|
||||
err = r.SetData(event)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ProjectMemberView) setRootData(event *models.Event) {
|
||||
r.ProjectID = event.AggregateID
|
||||
}
|
||||
|
||||
func (r *ProjectMemberView) SetData(event *models.Event) error {
|
||||
if err := json.Unmarshal(event.Data, r); err != nil {
|
||||
logging.Log("EVEN-slo9s").WithError(err).Error("could not unmarshal event data")
|
||||
return zerrors.ThrowInternal(err, "MODEL-lub6s", "Could not unmarshal data")
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -1,59 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
es_model "github.com/zitadel/zitadel/internal/project/repository/eventsourcing/model"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
)
|
||||
|
||||
func mockProjectMemberData(member *es_model.ProjectMember) []byte {
|
||||
data, _ := json.Marshal(member)
|
||||
return data
|
||||
}
|
||||
|
||||
func TestProjectMemberAppendEvent(t *testing.T) {
|
||||
type args struct {
|
||||
event *es_models.Event
|
||||
member *ProjectMemberView
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
result *ProjectMemberView
|
||||
}{
|
||||
{
|
||||
name: "append added member event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.MemberAddedType, ResourceOwner: "OrgID", Data: mockProjectMemberData(&es_model.ProjectMember{UserID: "UserID", Roles: []string{"Role"}})},
|
||||
member: &ProjectMemberView{},
|
||||
},
|
||||
result: &ProjectMemberView{ProjectID: "AggregateID", UserID: "UserID", Roles: []string{"Role"}},
|
||||
},
|
||||
{
|
||||
name: "append changed member event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.MemberAddedType, ResourceOwner: "OrgID", Data: mockProjectMemberData(&es_model.ProjectMember{UserID: "UserID", Roles: []string{"RoleChanged"}})},
|
||||
member: &ProjectMemberView{ProjectID: "AggregateID", UserID: "UserID", Roles: []string{"Role"}},
|
||||
},
|
||||
result: &ProjectMemberView{ProjectID: "AggregateID", UserID: "UserID", Roles: []string{"RoleChanged"}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.args.member.AppendEvent(tt.args.event)
|
||||
if tt.args.member.ProjectID != tt.result.ProjectID {
|
||||
t.Errorf("got wrong result projectID: expected: %v, actual: %v ", tt.result.ProjectID, tt.args.member.ProjectID)
|
||||
}
|
||||
if tt.args.member.UserID != tt.result.UserID {
|
||||
t.Errorf("got wrong result userID: expected: %v, actual: %v ", tt.result.UserID, tt.args.member.UserID)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.args.member.Roles, tt.result.Roles) {
|
||||
t.Errorf("got wrong result Roles: expected: %v, actual: %v ", tt.result.Roles, tt.args.member.Roles)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,72 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/zitadel/zitadel/internal/project/model"
|
||||
es_model "github.com/zitadel/zitadel/internal/project/repository/eventsourcing/model"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
)
|
||||
|
||||
func TestProjectAppendEvent(t *testing.T) {
|
||||
type args struct {
|
||||
event *es_models.Event
|
||||
project *ProjectView
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
result *ProjectView
|
||||
}{
|
||||
{
|
||||
name: "append added project event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ProjectAddedType, ResourceOwner: "GrantedOrgID", Data: mockProjectData(&es_model.Project{Name: "ProjectName"})},
|
||||
project: &ProjectView{},
|
||||
},
|
||||
result: &ProjectView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", Name: "ProjectName", State: int32(model.ProjectStateActive)},
|
||||
},
|
||||
{
|
||||
name: "append change project event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ProjectChangedType, ResourceOwner: "GrantedOrgID", Data: mockProjectData(&es_model.Project{Name: "ProjectNameChanged"})},
|
||||
project: &ProjectView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", Name: "ProjectName", State: int32(model.ProjectStateActive)},
|
||||
},
|
||||
result: &ProjectView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", Name: "ProjectNameChanged", State: int32(model.ProjectStateActive)},
|
||||
},
|
||||
{
|
||||
name: "append project deactivate event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ProjectDeactivatedType, ResourceOwner: "GrantedOrgID"},
|
||||
project: &ProjectView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", Name: "ProjectName", State: int32(model.ProjectStateActive)},
|
||||
},
|
||||
result: &ProjectView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", Name: "ProjectName", State: int32(model.ProjectStateInactive)},
|
||||
},
|
||||
{
|
||||
name: "append project reactivate event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ProjectReactivatedType, ResourceOwner: "GrantedOrgID"},
|
||||
project: &ProjectView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", Name: "ProjectName", State: int32(model.ProjectStateInactive)},
|
||||
},
|
||||
result: &ProjectView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", Name: "ProjectName", State: int32(model.ProjectStateActive)},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.args.project.AppendEvent(tt.args.event)
|
||||
if tt.args.project.ProjectID != tt.result.ProjectID {
|
||||
t.Errorf("got wrong result projectID: expected: %v, actual: %v ", tt.result.ProjectID, tt.args.project.ProjectID)
|
||||
}
|
||||
if tt.args.project.ResourceOwner != tt.result.ResourceOwner {
|
||||
t.Errorf("got wrong result ResourceOwner: expected: %v, actual: %v ", tt.result.ResourceOwner, tt.args.project.ResourceOwner)
|
||||
}
|
||||
if tt.args.project.Name != tt.result.Name {
|
||||
t.Errorf("got wrong result name: expected: %v, actual: %v ", tt.result.Name, tt.args.project.Name)
|
||||
}
|
||||
if tt.args.project.State != tt.result.State {
|
||||
t.Errorf("got wrong result state: expected: %v, actual: %v ", tt.result.State, tt.args.project.State)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,70 +0,0 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
proj_model "github.com/zitadel/zitadel/internal/project/model"
|
||||
"github.com/zitadel/zitadel/internal/project/repository/view/model"
|
||||
"github.com/zitadel/zitadel/internal/view/repository"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func OrgProjectMappingByIDs(db *gorm.DB, table, orgID, projectID, instanceID string) (*model.OrgProjectMapping, error) {
|
||||
orgProjectMapping := new(model.OrgProjectMapping)
|
||||
|
||||
projectIDQuery := model.OrgProjectMappingSearchQuery{Key: proj_model.OrgProjectMappingSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals}
|
||||
orgIDQuery := model.OrgProjectMappingSearchQuery{Key: proj_model.OrgProjectMappingSearchKeyOrgID, Value: orgID, Method: domain.SearchMethodEquals}
|
||||
instanceIDQuery := model.OrgProjectMappingSearchQuery{Key: proj_model.OrgProjectMappingSearchKeyInstanceID, Value: instanceID, Method: domain.SearchMethodEquals}
|
||||
ownerRemovedQuery := model.OrgProjectMappingSearchQuery{Key: proj_model.OrgProjectMappingSearchKeyOwnerRemoved, Value: false, Method: domain.SearchMethodEquals}
|
||||
query := repository.PrepareGetByQuery(table, projectIDQuery, orgIDQuery, instanceIDQuery, ownerRemovedQuery)
|
||||
err := query(db, orgProjectMapping)
|
||||
if zerrors.IsNotFound(err) {
|
||||
return nil, zerrors.ThrowNotFound(nil, "VIEW-fn9fs", "Errors.OrgProjectMapping.NotExisting")
|
||||
}
|
||||
return orgProjectMapping, err
|
||||
}
|
||||
|
||||
func PutOrgProjectMapping(db *gorm.DB, table string, grant *model.OrgProjectMapping) error {
|
||||
save := repository.PrepareSave(table)
|
||||
return save(db, grant)
|
||||
}
|
||||
|
||||
func DeleteOrgProjectMapping(db *gorm.DB, table, orgID, projectID, instanceID string) error {
|
||||
projectIDSearch := repository.Key{Key: model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyProjectID), Value: projectID}
|
||||
orgIDSearch := repository.Key{Key: model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyOrgID), Value: orgID}
|
||||
instanceIDSearch := repository.Key{Key: model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyInstanceID), Value: instanceID}
|
||||
delete := repository.PrepareDeleteByKeys(table, projectIDSearch, orgIDSearch, instanceIDSearch)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteInstanceOrgProjectMappings(db *gorm.DB, table, instanceID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyInstanceID), instanceID)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func UpdateOwnerRemovedOrgProjectMappings(db *gorm.DB, table, instanceID, orgID string) error {
|
||||
update := repository.PrepareUpdateByKeys(table,
|
||||
model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyOwnerRemoved),
|
||||
true,
|
||||
repository.Key{Key: model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyInstanceID), Value: instanceID},
|
||||
repository.Key{Key: model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyOrgID), Value: orgID},
|
||||
)
|
||||
return update(db)
|
||||
}
|
||||
|
||||
func DeleteOrgProjectMappingsByProjectID(db *gorm.DB, table, projectID, instanceID string) error {
|
||||
delete := repository.PrepareDeleteByKeys(table,
|
||||
repository.Key{model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyProjectID), projectID},
|
||||
repository.Key{model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyInstanceID), instanceID},
|
||||
)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteOrgProjectMappingsByProjectGrantID(db *gorm.DB, table, projectGrantID, instanceID string) error {
|
||||
delete := repository.PrepareDeleteByKeys(table,
|
||||
repository.Key{model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyProjectGrantID), projectGrantID},
|
||||
repository.Key{model.OrgProjectMappingSearchKey(proj_model.OrgProjectMappingSearchKeyInstanceID), instanceID},
|
||||
)
|
||||
return delete(db)
|
||||
}
|
Reference in New Issue
Block a user