2025-05-08 07:42:53 +02:00
|
|
|
package repository
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
|
|
)
|
2025-04-29 06:03:47 +02:00
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// -------------------------------------------------------------
|
|
|
|
// repository
|
|
|
|
// -------------------------------------------------------------
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
type userHuman struct {
|
|
|
|
*user
|
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
var _ domain.HumanRepository = (*userHuman)(nil)
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
const userEmailQuery = `SELECT h.email_address, h.email_verified_at FROM user_humans h`
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// GetEmail implements [domain.HumanRepository].
|
|
|
|
func (u *userHuman) GetEmail(ctx context.Context, condition database.Condition) (*domain.Email, error) {
|
|
|
|
var email domain.Email
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
u.builder.WriteString(userEmailQuery)
|
2025-04-30 09:30:48 +02:00
|
|
|
u.writeCondition(condition)
|
2025-04-29 06:03:47 +02:00
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
err := u.client.QueryRow(ctx, u.builder.String(), u.builder.Args()...).Scan(
|
2025-04-29 06:03:47 +02:00
|
|
|
&email.Address,
|
2025-05-06 07:18:11 +02:00
|
|
|
&email.VerifiedAt,
|
2025-04-29 06:03:47 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &email, nil
|
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// Update implements [domain.HumanRepository].
|
|
|
|
func (h userHuman) Update(ctx context.Context, condition database.Condition, changes ...database.Change) error {
|
2025-04-30 09:30:48 +02:00
|
|
|
h.builder.WriteString(`UPDATE human_users SET `)
|
2025-05-06 07:18:11 +02:00
|
|
|
database.Changes(changes).Write(&h.builder)
|
2025-04-30 09:30:48 +02:00
|
|
|
h.writeCondition(condition)
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
stmt := h.builder.String()
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
return h.client.Exec(ctx, stmt, h.builder.Args()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// changes
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
|
|
|
// SetFirstName implements [domain.humanChanges].
|
|
|
|
func (h userHuman) SetFirstName(firstName string) database.Change {
|
|
|
|
return database.NewChange(h.FirstNameColumn(), firstName)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// SetLastName implements [domain.humanChanges].
|
|
|
|
func (h userHuman) SetLastName(lastName string) database.Change {
|
|
|
|
return database.NewChange(h.LastNameColumn(), lastName)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// SetEmail implements [domain.humanChanges].
|
|
|
|
func (h userHuman) SetEmail(address string, verified *time.Time) database.Change {
|
|
|
|
return database.NewChanges(
|
|
|
|
h.SetEmailAddress(address),
|
|
|
|
database.NewChangePtr(h.EmailVerifiedAtColumn(), verified),
|
|
|
|
)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// SetEmailAddress implements [domain.humanChanges].
|
|
|
|
func (h userHuman) SetEmailAddress(address string) database.Change {
|
|
|
|
return database.NewChange(h.EmailAddressColumn(), address)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// SetEmailVerifiedAt implements [domain.humanChanges].
|
|
|
|
func (h userHuman) SetEmailVerifiedAt(at time.Time) database.Change {
|
|
|
|
if at.IsZero() {
|
|
|
|
return database.NewChange(h.EmailVerifiedAtColumn(), database.NowInstruction)
|
|
|
|
}
|
|
|
|
return database.NewChange(h.EmailVerifiedAtColumn(), at)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// SetPhone implements [domain.humanChanges].
|
|
|
|
func (h userHuman) SetPhone(number string, verifiedAt *time.Time) database.Change {
|
|
|
|
return database.NewChanges(
|
|
|
|
h.SetPhoneNumber(number),
|
|
|
|
database.NewChangePtr(h.PhoneVerifiedAtColumn(), verifiedAt),
|
|
|
|
)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// SetPhoneNumber implements [domain.humanChanges].
|
|
|
|
func (h userHuman) SetPhoneNumber(number string) database.Change {
|
|
|
|
return database.NewChange(h.PhoneNumberColumn(), number)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// SetPhoneVerifiedAt implements [domain.humanChanges].
|
|
|
|
func (h userHuman) SetPhoneVerifiedAt(at time.Time) database.Change {
|
|
|
|
if at.IsZero() {
|
|
|
|
return database.NewChange(h.PhoneVerifiedAtColumn(), database.NowInstruction)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
2025-05-06 07:18:11 +02:00
|
|
|
return database.NewChange(h.PhoneVerifiedAtColumn(), at)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// conditions
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
|
|
|
// FirstNameCondition implements [domain.humanConditions].
|
|
|
|
func (h userHuman) FirstNameCondition(op database.TextOperation, firstName string) database.Condition {
|
|
|
|
return database.NewTextCondition(h.FirstNameColumn(), op, firstName)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// LastNameCondition implements [domain.humanConditions].
|
|
|
|
func (h userHuman) LastNameCondition(op database.TextOperation, lastName string) database.Condition {
|
|
|
|
return database.NewTextCondition(h.LastNameColumn(), op, lastName)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// EmailAddressCondition implements [domain.humanConditions].
|
|
|
|
func (h userHuman) EmailAddressCondition(op database.TextOperation, email string) database.Condition {
|
|
|
|
return database.NewTextCondition(h.EmailAddressColumn(), op, email)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// EmailVerifiedCondition implements [domain.humanConditions].
|
|
|
|
func (h *userHuman) EmailVerifiedCondition(isVerified bool) database.Condition {
|
2025-04-29 06:03:47 +02:00
|
|
|
if isVerified {
|
2025-05-06 07:18:11 +02:00
|
|
|
return database.IsNotNull(h.EmailVerifiedAtColumn())
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
2025-05-06 07:18:11 +02:00
|
|
|
return database.IsNull(h.EmailVerifiedAtColumn())
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// EmailVerifiedAtCondition implements [domain.humanConditions].
|
|
|
|
func (h userHuman) EmailVerifiedAtCondition(op database.NumberOperation, verifiedAt time.Time) database.Condition {
|
|
|
|
return database.NewNumberCondition(h.EmailVerifiedAtColumn(), op, verifiedAt)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// PhoneNumberCondition implements [domain.humanConditions].
|
|
|
|
func (h userHuman) PhoneNumberCondition(op database.TextOperation, phoneNumber string) database.Condition {
|
|
|
|
return database.NewTextCondition(h.PhoneNumberColumn(), op, phoneNumber)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// PhoneVerifiedCondition implements [domain.humanConditions].
|
|
|
|
func (h userHuman) PhoneVerifiedCondition(isVerified bool) database.Condition {
|
|
|
|
if isVerified {
|
|
|
|
return database.IsNotNull(h.PhoneVerifiedAtColumn())
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
2025-05-06 07:18:11 +02:00
|
|
|
return database.IsNull(h.PhoneVerifiedAtColumn())
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// PhoneVerifiedAtCondition implements [domain.humanConditions].
|
|
|
|
func (h userHuman) PhoneVerifiedAtCondition(op database.NumberOperation, verifiedAt time.Time) database.Condition {
|
|
|
|
return database.NewNumberCondition(h.PhoneVerifiedAtColumn(), op, verifiedAt)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// -------------------------------------------------------------
|
|
|
|
// columns
|
|
|
|
// -------------------------------------------------------------
|
2025-04-29 06:03:47 +02:00
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// FirstNameColumn implements [domain.humanColumns].
|
|
|
|
func (h userHuman) FirstNameColumn() database.Column {
|
|
|
|
return database.NewColumn("first_name")
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// LastNameColumn implements [domain.humanColumns].
|
|
|
|
func (h userHuman) LastNameColumn() database.Column {
|
|
|
|
return database.NewColumn("last_name")
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// EmailAddressColumn implements [domain.humanColumns].
|
|
|
|
func (h userHuman) EmailAddressColumn() database.Column {
|
|
|
|
return database.NewIgnoreCaseColumn("email_address", "_lower")
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// EmailVerifiedAtColumn implements [domain.humanColumns].
|
|
|
|
func (h userHuman) EmailVerifiedAtColumn() database.Column {
|
|
|
|
return database.NewColumn("email_verified_at")
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// PhoneNumberColumn implements [domain.humanColumns].
|
|
|
|
func (h userHuman) PhoneNumberColumn() database.Column {
|
|
|
|
return database.NewColumn("phone_number")
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-06 07:18:11 +02:00
|
|
|
// PhoneVerifiedAtColumn implements [domain.humanColumns].
|
|
|
|
func (h userHuman) PhoneVerifiedAtColumn() database.Column {
|
|
|
|
return database.NewColumn("phone_verified_at")
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
2025-04-30 09:30:48 +02:00
|
|
|
|
2025-06-13 17:05:37 +02:00
|
|
|
// func (h userHuman) columns() database.Columns {
|
|
|
|
// return append(h.user.columns(),
|
|
|
|
// h.FirstNameColumn(),
|
|
|
|
// h.LastNameColumn(),
|
|
|
|
// h.EmailAddressColumn(),
|
|
|
|
// h.EmailVerifiedAtColumn(),
|
|
|
|
// h.PhoneNumberColumn(),
|
|
|
|
// h.PhoneVerifiedAtColumn(),
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func (h userHuman) writeReturning(builder *database.StatementBuilder) {
|
|
|
|
// builder.WriteString(" RETURNING ")
|
|
|
|
// h.columns().Write(builder)
|
|
|
|
// }
|