2020-04-07 11:23:04 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2020-06-15 14:50:09 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2020-10-16 05:49:38 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-04-07 11:23:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Project struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
2020-10-16 05:49:38 +00:00
|
|
|
State ProjectState
|
|
|
|
Name string
|
|
|
|
Members []*ProjectMember
|
|
|
|
Roles []*ProjectRole
|
|
|
|
Applications []*Application
|
|
|
|
Grants []*ProjectGrant
|
|
|
|
ProjectRoleAssertion bool
|
|
|
|
ProjectRoleCheck bool
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
2020-06-15 14:50:09 +00:00
|
|
|
type ProjectChanges struct {
|
|
|
|
Changes []*ProjectChange
|
|
|
|
LastSequence uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectChange struct {
|
2021-06-10 09:29:34 +00:00
|
|
|
ChangeDate *timestamp.Timestamp `json:"changeDate,omitempty"`
|
|
|
|
EventType string `json:"eventType,omitempty"`
|
|
|
|
Sequence uint64 `json:"sequence,omitempty"`
|
|
|
|
ModifierId string `json:"modifierUser,omitempty"`
|
|
|
|
ModifierName string `json:"-"`
|
|
|
|
ModifierLoginName string `json:"-"`
|
2021-06-14 11:53:40 +00:00
|
|
|
ModifierAvatarURL string `json:"-"`
|
2021-06-10 09:29:34 +00:00
|
|
|
Data interface{} `json:"data,omitempty"`
|
2020-06-15 14:50:09 +00:00
|
|
|
}
|
2020-04-07 11:23:04 +00:00
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
type ProjectState int32
|
2020-04-07 11:23:04 +00:00
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
ProjectStateActive ProjectState = iota
|
|
|
|
ProjectStateInactive
|
|
|
|
ProjectStateRemoved
|
2020-04-21 15:00:32 +00:00
|
|
|
)
|
2020-04-07 11:23:04 +00:00
|
|
|
|
|
|
|
func NewProject(id string) *Project {
|
2020-06-23 12:47:47 +00:00
|
|
|
return &Project{ObjectRoot: es_models.ObjectRoot{AggregateID: id}, State: ProjectStateActive}
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) IsActive() bool {
|
2020-06-23 12:47:47 +00:00
|
|
|
return p.State == ProjectStateActive
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) IsValid() bool {
|
2020-04-21 15:00:32 +00:00
|
|
|
return p.Name != ""
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
2020-04-15 15:11:42 +00:00
|
|
|
|
2020-04-23 05:54:40 +00:00
|
|
|
func (p *Project) GetMember(userID string) (int, *ProjectMember) {
|
|
|
|
for i, m := range p.Members {
|
|
|
|
if m.UserID == userID {
|
|
|
|
return i, m
|
2020-04-15 15:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 05:54:40 +00:00
|
|
|
return -1, nil
|
2020-04-15 15:11:42 +00:00
|
|
|
}
|
2020-04-21 15:00:32 +00:00
|
|
|
|
|
|
|
func (p *Project) ContainsRole(role *ProjectRole) bool {
|
|
|
|
for _, r := range p.Roles {
|
|
|
|
if r.Key == role.Key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:54:40 +00:00
|
|
|
func (p *Project) GetApp(appID string) (int, *Application) {
|
|
|
|
for i, a := range p.Applications {
|
|
|
|
if a.AppID == appID {
|
|
|
|
return i, a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) GetGrant(grantID string) (int, *ProjectGrant) {
|
|
|
|
for i, g := range p.Grants {
|
|
|
|
if g.GrantID == grantID {
|
|
|
|
return i, g
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 05:54:40 +00:00
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) ContainsGrantForOrg(orgID string) bool {
|
|
|
|
for _, g := range p.Grants {
|
|
|
|
if g.GrantedOrgID == orgID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) ContainsRoles(roleKeys []string) bool {
|
|
|
|
for _, r := range roleKeys {
|
|
|
|
if !p.ContainsRole(&ProjectRole{Key: r}) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Project) ContainsGrantMember(member *ProjectGrantMember) bool {
|
|
|
|
for _, g := range p.Grants {
|
|
|
|
if g.GrantID != member.GrantID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, m := g.GetMember(member.UserID); m != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|