2020-05-12 04:30:53 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-05-26 14:46:16 +00:00
|
|
|
"time"
|
|
|
|
|
2020-05-12 04:30:53 +00:00
|
|
|
"github.com/caos/logging"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/usergrant/model"
|
|
|
|
es_model "github.com/caos/zitadel/internal/usergrant/repository/eventsourcing/model"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
UserGrantKeyID = "id"
|
|
|
|
UserGrantKeyUserID = "user_id"
|
|
|
|
UserGrantKeyProjectID = "project_id"
|
2020-08-27 12:44:52 +00:00
|
|
|
UserGrantKeyGrantID = "grant_id"
|
2020-05-12 04:30:53 +00:00
|
|
|
UserGrantKeyResourceOwner = "resource_owner"
|
|
|
|
UserGrantKeyState = "state"
|
2020-06-05 05:50:04 +00:00
|
|
|
UserGrantKeyOrgName = "org_name"
|
2020-06-19 13:32:03 +00:00
|
|
|
UserGrantKeyRole = "role_keys"
|
2020-05-12 04:30:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserGrantView struct {
|
|
|
|
ID string `json:"-" gorm:"column:id;primary_key"`
|
|
|
|
ResourceOwner string `json:"-" gorm:"resource_owner"`
|
|
|
|
UserID string `json:"userId" gorm:"user_id"`
|
|
|
|
ProjectID string `json:"projectId" gorm:"column:project_id"`
|
2020-07-09 13:14:01 +00:00
|
|
|
GrantID string `json:"grantId" gorm:"column:grant_id"`
|
2020-05-12 04:30:53 +00:00
|
|
|
UserName string `json:"-" gorm:"column:user_name"`
|
|
|
|
FirstName string `json:"-" gorm:"column:first_name"`
|
|
|
|
LastName string `json:"-" gorm:"column:last_name"`
|
2020-07-01 11:18:30 +00:00
|
|
|
DisplayName string `json:"-" gorm:"column:display_name"`
|
2020-05-12 04:30:53 +00:00
|
|
|
Email string `json:"-" gorm:"column:email"`
|
|
|
|
ProjectName string `json:"-" gorm:"column:project_name"`
|
|
|
|
OrgName string `json:"-" gorm:"column:org_name"`
|
|
|
|
RoleKeys pq.StringArray `json:"roleKeys" gorm:"column:role_keys"`
|
|
|
|
|
|
|
|
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
|
|
|
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
|
|
|
State int32 `json:"-" gorm:"column:grant_state"`
|
|
|
|
|
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantToModel(grant *UserGrantView) *model.UserGrantView {
|
|
|
|
return &model.UserGrantView{
|
|
|
|
ID: grant.ID,
|
|
|
|
ResourceOwner: grant.ResourceOwner,
|
|
|
|
UserID: grant.UserID,
|
|
|
|
ProjectID: grant.ProjectID,
|
|
|
|
ChangeDate: grant.ChangeDate,
|
|
|
|
CreationDate: grant.CreationDate,
|
|
|
|
State: model.UserGrantState(grant.State),
|
|
|
|
UserName: grant.UserName,
|
|
|
|
FirstName: grant.FirstName,
|
|
|
|
LastName: grant.LastName,
|
2020-07-01 11:18:30 +00:00
|
|
|
DisplayName: grant.DisplayName,
|
2020-05-12 04:30:53 +00:00
|
|
|
Email: grant.Email,
|
|
|
|
ProjectName: grant.ProjectName,
|
|
|
|
OrgName: grant.OrgName,
|
|
|
|
RoleKeys: grant.RoleKeys,
|
|
|
|
Sequence: grant.Sequence,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantsToModel(grants []*UserGrantView) []*model.UserGrantView {
|
|
|
|
result := make([]*model.UserGrantView, len(grants))
|
|
|
|
for i, g := range grants {
|
|
|
|
result[i] = UserGrantToModel(g)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *UserGrantView) AppendEvent(event *models.Event) (err error) {
|
|
|
|
g.ChangeDate = event.CreationDate
|
|
|
|
g.Sequence = event.Sequence
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.UserGrantAdded:
|
2020-06-23 12:47:47 +00:00
|
|
|
g.State = int32(model.UserGrantStateActive)
|
2020-05-12 04:30:53 +00:00
|
|
|
g.CreationDate = event.CreationDate
|
|
|
|
g.setRootData(event)
|
|
|
|
err = g.setData(event)
|
2020-06-19 13:32:03 +00:00
|
|
|
case es_model.UserGrantChanged, es_model.UserGrantCascadeChanged:
|
2020-05-12 04:30:53 +00:00
|
|
|
err = g.setData(event)
|
|
|
|
case es_model.UserGrantDeactivated:
|
2020-06-23 12:47:47 +00:00
|
|
|
g.State = int32(model.UserGrantStateInactive)
|
2020-05-12 04:30:53 +00:00
|
|
|
case es_model.UserGrantReactivated:
|
2020-06-23 12:47:47 +00:00
|
|
|
g.State = int32(model.UserGrantStateActive)
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserGrantView) setRootData(event *models.Event) {
|
|
|
|
u.ID = event.AggregateID
|
|
|
|
u.ResourceOwner = event.ResourceOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserGrantView) setData(event *models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, u); err != nil {
|
|
|
|
logging.Log("EVEN-l9sw4").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return caos_errs.ThrowInternal(nil, "MODEL-7xhke", "could not unmarshal data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|