2020-06-25 06:12:29 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
2020-07-07 09:42:39 +00:00
|
|
|
es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
|
2020-06-25 06:12:29 +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/iam/model"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-26 07:56:23 +00:00
|
|
|
IAMMemberKeyUserID = "user_id"
|
|
|
|
IAMMemberKeyIamID = "iam_id"
|
|
|
|
IAMMemberKeyUserName = "user_name"
|
|
|
|
IAMMemberKeyEmail = "email"
|
|
|
|
IAMMemberKeyFirstName = "first_name"
|
|
|
|
IAMMemberKeyLastName = "last_name"
|
2020-06-25 06:12:29 +00:00
|
|
|
)
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
type IAMMemberView struct {
|
2020-07-22 12:21:41 +00:00
|
|
|
UserID string `json:"userId" gorm:"column:user_id;primary_key"`
|
2020-08-26 07:56:23 +00:00
|
|
|
IAMID string `json:"-" gorm:"column:iam_id"`
|
2020-07-22 12:21:41 +00:00
|
|
|
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 pq.StringArray `json:"roles" gorm:"column:roles"`
|
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
2020-06-25 06:12:29 +00:00
|
|
|
|
|
|
|
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
|
|
|
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func IAMMemberViewFromModel(member *model.IAMMemberView) *IAMMemberView {
|
|
|
|
return &IAMMemberView{
|
2020-06-25 06:12:29 +00:00
|
|
|
UserID: member.UserID,
|
2020-08-26 07:56:23 +00:00
|
|
|
IAMID: member.IAMID,
|
2020-06-25 06:12:29 +00:00
|
|
|
UserName: member.UserName,
|
|
|
|
Email: member.Email,
|
|
|
|
FirstName: member.FirstName,
|
|
|
|
LastName: member.LastName,
|
2020-07-22 12:21:41 +00:00
|
|
|
DisplayName: member.DisplayName,
|
2020-06-25 06:12:29 +00:00
|
|
|
Roles: member.Roles,
|
|
|
|
Sequence: member.Sequence,
|
|
|
|
CreationDate: member.CreationDate,
|
|
|
|
ChangeDate: member.ChangeDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func IAMMemberToModel(member *IAMMemberView) *model.IAMMemberView {
|
|
|
|
return &model.IAMMemberView{
|
2020-06-25 06:12:29 +00:00
|
|
|
UserID: member.UserID,
|
2020-08-26 07:56:23 +00:00
|
|
|
IAMID: member.IAMID,
|
2020-06-25 06:12:29 +00:00
|
|
|
UserName: member.UserName,
|
|
|
|
Email: member.Email,
|
|
|
|
FirstName: member.FirstName,
|
|
|
|
LastName: member.LastName,
|
2020-07-22 12:21:41 +00:00
|
|
|
DisplayName: member.DisplayName,
|
2020-06-25 06:12:29 +00:00
|
|
|
Roles: member.Roles,
|
|
|
|
Sequence: member.Sequence,
|
|
|
|
CreationDate: member.CreationDate,
|
|
|
|
ChangeDate: member.ChangeDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func IAMMembersToModel(roles []*IAMMemberView) []*model.IAMMemberView {
|
|
|
|
result := make([]*model.IAMMemberView, len(roles))
|
2020-06-25 06:12:29 +00:00
|
|
|
for i, r := range roles {
|
2020-08-26 07:56:23 +00:00
|
|
|
result[i] = IAMMemberToModel(r)
|
2020-06-25 06:12:29 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func (r *IAMMemberView) AppendEvent(event *models.Event) (err error) {
|
2020-06-25 06:12:29 +00:00
|
|
|
r.Sequence = event.Sequence
|
|
|
|
r.ChangeDate = event.CreationDate
|
|
|
|
switch event.Type {
|
2020-08-26 07:56:23 +00:00
|
|
|
case es_model.IAMMemberAdded:
|
2020-06-25 06:12:29 +00:00
|
|
|
r.setRootData(event)
|
|
|
|
r.CreationDate = event.CreationDate
|
|
|
|
err = r.SetData(event)
|
2020-08-26 07:56:23 +00:00
|
|
|
case es_model.IAMMemberChanged:
|
2020-06-25 06:12:29 +00:00
|
|
|
err = r.SetData(event)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func (r *IAMMemberView) setRootData(event *models.Event) {
|
|
|
|
r.IAMID = event.AggregateID
|
2020-06-25 06:12:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func (r *IAMMemberView) SetData(event *models.Event) error {
|
2020-06-25 06:12:29 +00:00
|
|
|
if err := json.Unmarshal(event.Data, r); err != nil {
|
|
|
|
logging.Log("EVEN-Psl89").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return caos_errs.ThrowInternal(err, "MODEL-lub6s", "Could not unmarshal data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|