mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 03:47:33 +00:00

# Which Problems Are Solved As an outcome of [this issue](https://github.com/zitadel/zitadel/issues/9599) we want to implement relational tables in Zitadel. For that we use new tables as a successor of the current tables used by Zitadel in `projections`, `auth` and `admin` schemas. The new logic is based on [this proposal](https://github.com/zitadel/zitadel/pull/9870). This issue does not contain the switch from CQRS to the new tables. This is change will be implemented in a later stage. We focus on the most critical tables which is user authentication. We need a table to manage organizations. ### organization fields The following fields must be managed in this table: - `id` - `instance_id` - `name` - `state` enum (active, inactive) - `created_at` - `updated_at` - `deleted_at` DISCUSS: should we add a `primary_domain` to this table so that we do not have to join on domains to return a simple org? We must ensure the unique constraints for this table matches the current commands. ### organization repository The repository must provide the following functions: Manipulations: - create - `instance_id` - `name` - update - `name` - delete Queries: - get returns single organization matching the criteria and pagination, should return error if multiple were found - list returns list of organizations matching the criteria, pagination Criteria are the following: - by id - by name pagination: - by created_at - by updated_at - by name ### organization events The following events must be applied on the table using a projection (`internal/query/projection`) - `org.added` results in create - `org.changed` sets the `name` field - `org.deactivated` sets the `state` field - `org.reactivated` sets the `state` field - `org.removed` sets the `deleted_at` field - if answer is yes to discussion: `org.domain.primary.set` sets the `primary_domain` field - `instance.removed` sets the the `deleted_at` field if not already set ### acceptance criteria - [x] migration is implemented and gets executed - [x] domain interfaces are implemented and documented for service layer - [x] repository is implemented and implements domain interface - [x] testing - [x] the repository methods - [x] events get reduced correctly - [x] unique constraints # Additional Context Replace this example with links to related issues, discussions, discord threads, or other sources with more context. Use the Closing #issue syntax for issues that are resolved with this PR. - Closes #https://github.com/zitadel/zitadel/issues/9936 --------- Co-authored-by: adlerhurst <27845747+adlerhurst@users.noreply.github.com>
209 lines
6.9 KiB
Go
209 lines
6.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
)
|
|
|
|
// -------------------------------------------------------------
|
|
// repository
|
|
// -------------------------------------------------------------
|
|
|
|
type userHuman struct {
|
|
*user
|
|
}
|
|
|
|
var _ domain.HumanRepository = (*userHuman)(nil)
|
|
|
|
const userEmailQuery = `SELECT h.email_address, h.email_verified_at FROM user_humans h`
|
|
|
|
// GetEmail implements [domain.HumanRepository].
|
|
func (u *userHuman) GetEmail(ctx context.Context, condition database.Condition) (*domain.Email, error) {
|
|
var email domain.Email
|
|
|
|
builder := database.StatementBuilder{}
|
|
builder.WriteString(userEmailQuery)
|
|
writeCondition(&builder, condition)
|
|
|
|
err := u.client.QueryRow(ctx, builder.String(), builder.Args()...).Scan(
|
|
&email.Address,
|
|
&email.VerifiedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &email, nil
|
|
}
|
|
|
|
// Update implements [domain.HumanRepository].
|
|
func (h userHuman) Update(ctx context.Context, condition database.Condition, changes ...database.Change) error {
|
|
builder := database.StatementBuilder{}
|
|
builder.WriteString(`UPDATE human_users SET `)
|
|
database.Changes(changes).Write(&builder)
|
|
writeCondition(&builder, condition)
|
|
|
|
stmt := builder.String()
|
|
|
|
_, err := h.client.Exec(ctx, stmt, builder.Args()...)
|
|
return err
|
|
}
|
|
|
|
// -------------------------------------------------------------
|
|
// changes
|
|
// -------------------------------------------------------------
|
|
|
|
// SetFirstName implements [domain.humanChanges].
|
|
func (h userHuman) SetFirstName(firstName string) database.Change {
|
|
return database.NewChange(h.FirstNameColumn(), firstName)
|
|
}
|
|
|
|
// SetLastName implements [domain.humanChanges].
|
|
func (h userHuman) SetLastName(lastName string) database.Change {
|
|
return database.NewChange(h.LastNameColumn(), lastName)
|
|
}
|
|
|
|
// 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),
|
|
)
|
|
}
|
|
|
|
// SetEmailAddress implements [domain.humanChanges].
|
|
func (h userHuman) SetEmailAddress(address string) database.Change {
|
|
return database.NewChange(h.EmailAddressColumn(), address)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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),
|
|
)
|
|
}
|
|
|
|
// SetPhoneNumber implements [domain.humanChanges].
|
|
func (h userHuman) SetPhoneNumber(number string) database.Change {
|
|
return database.NewChange(h.PhoneNumberColumn(), number)
|
|
}
|
|
|
|
// SetPhoneVerifiedAt implements [domain.humanChanges].
|
|
func (h userHuman) SetPhoneVerifiedAt(at time.Time) database.Change {
|
|
if at.IsZero() {
|
|
return database.NewChange(h.PhoneVerifiedAtColumn(), database.NowInstruction)
|
|
}
|
|
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)
|
|
}
|
|
|
|
// LastNameCondition implements [domain.humanConditions].
|
|
func (h userHuman) LastNameCondition(op database.TextOperation, lastName string) database.Condition {
|
|
return database.NewTextCondition(h.LastNameColumn(), op, lastName)
|
|
}
|
|
|
|
// EmailAddressCondition implements [domain.humanConditions].
|
|
func (h userHuman) EmailAddressCondition(op database.TextOperation, email string) database.Condition {
|
|
return database.NewTextCondition(h.EmailAddressColumn(), op, email)
|
|
}
|
|
|
|
// EmailVerifiedCondition implements [domain.humanConditions].
|
|
func (h *userHuman) EmailVerifiedCondition(isVerified bool) database.Condition {
|
|
if isVerified {
|
|
return database.IsNotNull(h.EmailVerifiedAtColumn())
|
|
}
|
|
return database.IsNull(h.EmailVerifiedAtColumn())
|
|
}
|
|
|
|
// EmailVerifiedAtCondition implements [domain.humanConditions].
|
|
func (h userHuman) EmailVerifiedAtCondition(op database.NumberOperation, verifiedAt time.Time) database.Condition {
|
|
return database.NewNumberCondition(h.EmailVerifiedAtColumn(), op, verifiedAt)
|
|
}
|
|
|
|
// PhoneNumberCondition implements [domain.humanConditions].
|
|
func (h userHuman) PhoneNumberCondition(op database.TextOperation, phoneNumber string) database.Condition {
|
|
return database.NewTextCondition(h.PhoneNumberColumn(), op, phoneNumber)
|
|
}
|
|
|
|
// PhoneVerifiedCondition implements [domain.humanConditions].
|
|
func (h userHuman) PhoneVerifiedCondition(isVerified bool) database.Condition {
|
|
if isVerified {
|
|
return database.IsNotNull(h.PhoneVerifiedAtColumn())
|
|
}
|
|
return database.IsNull(h.PhoneVerifiedAtColumn())
|
|
}
|
|
|
|
// PhoneVerifiedAtCondition implements [domain.humanConditions].
|
|
func (h userHuman) PhoneVerifiedAtCondition(op database.NumberOperation, verifiedAt time.Time) database.Condition {
|
|
return database.NewNumberCondition(h.PhoneVerifiedAtColumn(), op, verifiedAt)
|
|
}
|
|
|
|
// -------------------------------------------------------------
|
|
// columns
|
|
// -------------------------------------------------------------
|
|
|
|
// FirstNameColumn implements [domain.humanColumns].
|
|
func (h userHuman) FirstNameColumn() database.Column {
|
|
return database.NewColumn("first_name")
|
|
}
|
|
|
|
// LastNameColumn implements [domain.humanColumns].
|
|
func (h userHuman) LastNameColumn() database.Column {
|
|
return database.NewColumn("last_name")
|
|
}
|
|
|
|
// EmailAddressColumn implements [domain.humanColumns].
|
|
func (h userHuman) EmailAddressColumn() database.Column {
|
|
return database.NewIgnoreCaseColumn("email_address", "_lower")
|
|
}
|
|
|
|
// EmailVerifiedAtColumn implements [domain.humanColumns].
|
|
func (h userHuman) EmailVerifiedAtColumn() database.Column {
|
|
return database.NewColumn("email_verified_at")
|
|
}
|
|
|
|
// PhoneNumberColumn implements [domain.humanColumns].
|
|
func (h userHuman) PhoneNumberColumn() database.Column {
|
|
return database.NewColumn("phone_number")
|
|
}
|
|
|
|
// PhoneVerifiedAtColumn implements [domain.humanColumns].
|
|
func (h userHuman) PhoneVerifiedAtColumn() database.Column {
|
|
return database.NewColumn("phone_verified_at")
|
|
}
|
|
|
|
// 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)
|
|
// }
|