mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-14 11:58:02 +00:00
77b4fc5487
* beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
134 lines
4.8 KiB
Go
134 lines
4.8 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/zitadel/logging"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
org_model "github.com/zitadel/zitadel/internal/org/model"
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
es_model "github.com/zitadel/zitadel/internal/user/repository/eventsourcing/model"
|
|
)
|
|
|
|
const (
|
|
NotifyUserKeyUserID = "id"
|
|
NotifyUserKeyResourceOwner = "resource_owner"
|
|
NotifyUserKeyInstanceID = "instance_id"
|
|
)
|
|
|
|
type NotifyUser struct {
|
|
ID string `json:"-" gorm:"column:id;primary_key"`
|
|
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
|
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
|
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
|
|
UserName string `json:"userName" gorm:"column:user_name"`
|
|
LoginNames database.StringArray `json:"-" gorm:"column:login_names"`
|
|
PreferredLoginName string `json:"-" gorm:"column:preferred_login_name"`
|
|
FirstName string `json:"firstName" gorm:"column:first_name"`
|
|
LastName string `json:"lastName" gorm:"column:last_name"`
|
|
NickName string `json:"nickName" gorm:"column:nick_name"`
|
|
DisplayName string `json:"displayName" gorm:"column:display_name"`
|
|
PreferredLanguage string `json:"preferredLanguage" gorm:"column:preferred_language"`
|
|
Gender int32 `json:"gender" gorm:"column:gender"`
|
|
LastEmail string `json:"email" gorm:"column:last_email"`
|
|
VerifiedEmail string `json:"-" gorm:"column:verified_email"`
|
|
LastPhone string `json:"phone" gorm:"column:last_phone"`
|
|
VerifiedPhone string `json:"-" gorm:"column:verified_phone"`
|
|
PasswordSet bool `json:"-" gorm:"column:password_set"`
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
|
State int32 `json:"-" gorm:"-"`
|
|
InstanceID string `json:"instanceID" gorm:"column:instance_id;primary_key"`
|
|
}
|
|
|
|
func (u *NotifyUser) GenerateLoginName(domain string, appendDomain bool) string {
|
|
if !appendDomain {
|
|
return u.UserName
|
|
}
|
|
return u.UserName + "@" + domain
|
|
}
|
|
|
|
func (u *NotifyUser) SetLoginNames(userLoginMustBeDomain bool, domains []*org_model.OrgDomain) {
|
|
loginNames := make([]string, 0)
|
|
for _, d := range domains {
|
|
if d.Verified {
|
|
loginNames = append(loginNames, u.GenerateLoginName(d.Domain, true))
|
|
}
|
|
}
|
|
if !userLoginMustBeDomain {
|
|
loginNames = append(loginNames, u.UserName)
|
|
}
|
|
u.LoginNames = loginNames
|
|
}
|
|
|
|
func (u *NotifyUser) AppendEvent(event *models.Event) (err error) {
|
|
u.ChangeDate = event.CreationDate
|
|
u.Sequence = event.Sequence
|
|
switch eventstore.EventType(event.Type) {
|
|
case user.UserV1AddedType,
|
|
user.UserV1RegisteredType,
|
|
user.HumanRegisteredType,
|
|
user.HumanAddedType,
|
|
user.MachineAddedEventType:
|
|
u.CreationDate = event.CreationDate
|
|
u.setRootData(event)
|
|
err = u.setData(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = u.setPasswordData(event)
|
|
case user.UserV1ProfileChangedType,
|
|
user.UserV1EmailChangedType,
|
|
user.UserV1PhoneChangedType,
|
|
user.HumanProfileChangedType,
|
|
user.HumanEmailChangedType,
|
|
user.HumanPhoneChangedType,
|
|
user.UserUserNameChangedType:
|
|
err = u.setData(event)
|
|
case user.UserV1EmailVerifiedType,
|
|
user.HumanEmailVerifiedType:
|
|
u.VerifiedEmail = u.LastEmail
|
|
case user.UserV1PhoneRemovedType,
|
|
user.HumanPhoneRemovedType:
|
|
u.VerifiedPhone = ""
|
|
u.LastPhone = ""
|
|
case user.UserV1PhoneVerifiedType,
|
|
user.HumanPhoneVerifiedType:
|
|
u.VerifiedPhone = u.LastPhone
|
|
case user.UserV1PasswordChangedType,
|
|
user.HumanPasswordChangedType:
|
|
err = u.setPasswordData(event)
|
|
case user.UserRemovedType:
|
|
u.State = int32(UserStateDeleted)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (u *NotifyUser) setRootData(event *models.Event) {
|
|
u.ID = event.AggregateID
|
|
u.ResourceOwner = event.ResourceOwner
|
|
u.InstanceID = event.InstanceID
|
|
}
|
|
|
|
func (u *NotifyUser) setData(event *models.Event) error {
|
|
if err := json.Unmarshal(event.Data, u); err != nil {
|
|
logging.Log("MODEL-lso9e").WithError(err).Error("could not unmarshal event data")
|
|
return caos_errs.ThrowInternal(nil, "MODEL-8iows", "could not unmarshal data")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (u *NotifyUser) setPasswordData(event *models.Event) error {
|
|
password := new(es_model.Password)
|
|
if err := json.Unmarshal(event.Data, password); err != nil {
|
|
logging.Log("MODEL-dfhw6").WithError(err).Error("could not unmarshal event data")
|
|
return caos_errs.ThrowInternal(nil, "MODEL-BHFD2", "could not unmarshal data")
|
|
}
|
|
u.PasswordSet = password.Secret != nil
|
|
return nil
|
|
}
|