Fabi 6e105f662e
feat: project view (#90)
* init for views (spooler, handler)

* init for views (spooler, handler)

* start view in management

* granted project

* implement granted project view

* search granted projects

* fix search column

* update all projects on project change

* search roles

* filter org

* project members

* project grant members

* fix tests

* application view

* project grant search

* mock

* test appendevents

* test appendevents

* Update internal/view/query.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* Update internal/eventstore/spooler/spooler.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* Update internal/view/query.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* merge request changes

* Update internal/project/repository/view/model/application.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* merge request changes

* Project view sql (#92)

* sql and configs

* error handling

* sql start in eventstore

* on error handling, config

* read user on members

* Update internal/project/repository/view/application_view.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/application.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/application.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/application.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/application.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/application.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/application_query.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_grant_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_grant_member_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_grant_member_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_member_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_member_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/granted_project.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* return caos errors

* Update internal/project/repository/view/model/granted_project_query.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/project_grant_member.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/project_grant_member_query.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/project_member.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/project_member_query.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/project_role.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/project/repository/view/model/project_role_query.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/application_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/application_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update pkg/management/api/grpc/project_converter.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* converter fix

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: Silvan <silvan.reusser@gmail.com>
2020-05-11 12:16:29 +02:00

150 lines
4.9 KiB
Go

package model
import (
"encoding/json"
"github.com/caos/logging"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/project/model"
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
"github.com/lib/pq"
"time"
)
const (
GrantedProjectKeyProjectID = "project_id"
GrantedProjectKeyGrantID = "grant_id"
GrantedProjectKeyOrgID = "org_id"
GrantedProjectKeyResourceOwner = "resource_owner"
GrantedProjectKeyName = "name"
)
type GrantedProjectView struct {
ProjectID string `json:"-" gorm:"column:project_id;primary_key"`
OrgID string `json:"-" gorm:"column:org_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"`
Type int32 `json:"-" gorm:"column:project_type"`
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
OrgName string `json:"-" gorm:"column:org_name"`
OrgDomain string `json:"-" gorm:"column:org_domain"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
GrantID string `json:"-" gorm:"column:grant_id"`
GrantedRoleKeys pq.StringArray `json:"-" gorm:"column:granted_role_keys"`
}
type ProjectGrant struct {
GrantID string `json:"grantId"`
GrantedOrgID string `json:"grantedOrgId"`
RoleKeys []string `json:"roleKeys"`
}
func GrantedProjectFromModel(project *model.GrantedProjectView) *GrantedProjectView {
return &GrantedProjectView{
ProjectID: project.ProjectID,
OrgID: project.OrgID,
Name: project.Name,
ChangeDate: project.ChangeDate,
CreationDate: project.CreationDate,
State: int32(project.State),
Type: int32(project.Type),
ResourceOwner: project.ResourceOwner,
OrgName: project.OrgName,
GrantID: project.GrantID,
GrantedRoleKeys: project.GrantedRoleKeys,
Sequence: project.Sequence,
}
}
func GrantedProjectToModel(project *GrantedProjectView) *model.GrantedProjectView {
return &model.GrantedProjectView{
ProjectID: project.ProjectID,
OrgID: project.OrgID,
Name: project.Name,
ChangeDate: project.ChangeDate,
CreationDate: project.CreationDate,
State: model.ProjectState(project.State),
Type: model.ProjectType(project.Type),
ResourceOwner: project.ResourceOwner,
OrgName: project.OrgName,
GrantID: project.GrantID,
Sequence: project.Sequence,
}
}
func GrantedProjectsToModel(projects []*GrantedProjectView) []*model.GrantedProjectView {
result := make([]*model.GrantedProjectView, len(projects))
for i, p := range projects {
result[i] = GrantedProjectToModel(p)
}
return result
}
func (p *GrantedProjectView) AppendEvent(event *models.Event) (err error) {
p.ChangeDate = event.CreationDate
p.Sequence = event.Sequence
switch event.Type {
case es_model.ProjectAdded:
p.State = int32(model.PROJECTSTATE_ACTIVE)
p.CreationDate = event.CreationDate
p.setRootData(event)
err = p.setData(event)
case es_model.ProjectChanged:
err = p.setData(event)
case es_model.ProjectDeactivated:
p.State = int32(model.PROJECTSTATE_INACTIVE)
case es_model.ProjectReactivated:
p.State = int32(model.PROJECTSTATE_ACTIVE)
case es_model.ProjectGrantAdded:
p.State = int32(model.PROJECTSTATE_ACTIVE)
p.CreationDate = event.CreationDate
p.setRootData(event)
err = p.setProjectGrantData(event)
case es_model.ProjectGrantChanged:
err = p.setProjectGrantData(event)
case es_model.ProjectGrantDeactivated:
p.State = int32(model.PROJECTSTATE_INACTIVE)
case es_model.ProjectGrantReactivated:
p.State = int32(model.PROJECTSTATE_ACTIVE)
}
return err
}
func (p *GrantedProjectView) setRootData(event *models.Event) {
p.ProjectID = event.AggregateID
p.OrgID = event.ResourceOwner
p.ResourceOwner = event.ResourceOwner
}
func (p *GrantedProjectView) 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 *GrantedProjectView) 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
}