2025-05-08 07:42:53 +02:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-07-22 19:09:56 +02:00
|
|
|
"encoding/json"
|
2025-05-08 07:42:53 +02:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// repository
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
var _ domain.OrganizationRepository = (*org)(nil)
|
|
|
|
|
2025-05-08 07:42:53 +02:00
|
|
|
type org struct {
|
|
|
|
repository
|
2025-07-22 19:09:56 +02:00
|
|
|
shouldLoadDomains bool
|
|
|
|
domainRepo domain.OrganizationDomainRepository
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
func OrganizationRepository(client database.QueryExecutor) domain.OrganizationRepository {
|
2025-05-08 07:42:53 +02:00
|
|
|
return &org{
|
|
|
|
repository: repository{
|
|
|
|
client: client,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
const queryOrganizationStmt = `SELECT organizations.id, organizations.name, organizations.instance_id, organizations.state, organizations.created_at, organizations.updated_at` +
|
|
|
|
` , CASE WHEN count(org_domains.domain) > 0 THEN jsonb_agg(json_build_object('domain', org_domains.domain, 'isVerified', org_domains.is_verified, 'isPrimary', org_domains.is_primary, 'validationType', org_domains.validation_type, 'createdAt', org_domains.created_at, 'updatedAt', org_domains.updated_at)) ELSE NULL::JSONB END domains` +
|
2025-07-14 21:27:14 +02:00
|
|
|
` FROM zitadel.organizations`
|
2025-05-08 07:42:53 +02:00
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// Get implements [domain.OrganizationRepository].
|
2025-07-22 19:09:56 +02:00
|
|
|
func (o *org) Get(ctx context.Context, opts ...database.QueryOption) (*domain.Organization, error) {
|
|
|
|
opts = append(opts,
|
|
|
|
o.joinDomains(),
|
|
|
|
database.WithGroupBy(o.InstanceIDColumn(true), o.IDColumn(true)),
|
|
|
|
)
|
|
|
|
|
|
|
|
options := new(database.QueryOpts)
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(options)
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
var builder database.StatementBuilder
|
2025-07-14 21:27:14 +02:00
|
|
|
builder.WriteString(queryOrganizationStmt)
|
2025-07-22 19:09:56 +02:00
|
|
|
options.Write(&builder)
|
2025-07-14 21:27:14 +02:00
|
|
|
|
|
|
|
return scanOrganization(ctx, o.client, &builder)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// List implements [domain.OrganizationRepository].
|
2025-07-22 19:09:56 +02:00
|
|
|
func (o *org) List(ctx context.Context, opts ...database.QueryOption) ([]*domain.Organization, error) {
|
|
|
|
opts = append(opts,
|
|
|
|
o.joinDomains(),
|
|
|
|
database.WithGroupBy(o.InstanceIDColumn(true), o.IDColumn(true)),
|
|
|
|
)
|
|
|
|
|
|
|
|
options := new(database.QueryOpts)
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
var builder database.StatementBuilder
|
2025-07-14 21:27:14 +02:00
|
|
|
builder.WriteString(queryOrganizationStmt)
|
2025-07-22 19:09:56 +02:00
|
|
|
options.Write(&builder)
|
2025-07-14 21:27:14 +02:00
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
return scanOrganizations(ctx, o.client, &builder)
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
func (o *org) joinDomains() database.QueryOption {
|
|
|
|
columns := make([]database.Condition, 0, 3)
|
|
|
|
columns = append(columns,
|
|
|
|
database.NewColumnCondition(o.InstanceIDColumn(true), o.Domains(false).InstanceIDColumn(true)),
|
|
|
|
database.NewColumnCondition(o.IDColumn(true), o.Domains(false).OrgIDColumn(true)),
|
|
|
|
)
|
|
|
|
|
|
|
|
// If domains should not be joined, we make sure to return null for the domain columns
|
|
|
|
// the query optimizer of the dialect should optimize this away if no domains are requested
|
|
|
|
if !o.shouldLoadDomains {
|
|
|
|
columns = append(columns, database.IsNull(o.domainRepo.OrgIDColumn(true)))
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
return database.WithLeftJoin(
|
|
|
|
"zitadel.org_domains",
|
|
|
|
database.And(columns...),
|
|
|
|
)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
const createOrganizationStmt = `INSERT INTO zitadel.organizations (id, name, instance_id, state)` +
|
|
|
|
` VALUES ($1, $2, $3, $4)` +
|
|
|
|
` RETURNING created_at, updated_at`
|
|
|
|
|
|
|
|
// Create implements [domain.OrganizationRepository].
|
|
|
|
func (o *org) Create(ctx context.Context, organization *domain.Organization) error {
|
|
|
|
builder := database.StatementBuilder{}
|
|
|
|
builder.AppendArgs(organization.ID, organization.Name, organization.InstanceID, organization.State)
|
|
|
|
builder.WriteString(createOrganizationStmt)
|
|
|
|
|
2025-07-17 15:32:50 +02:00
|
|
|
return o.client.QueryRow(ctx, builder.String(), builder.Args()...).Scan(&organization.CreatedAt, &organization.UpdatedAt)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// Update implements [domain.OrganizationRepository].
|
2025-07-16 18:36:21 +02:00
|
|
|
func (o *org) Update(ctx context.Context, id domain.OrgIdentifierCondition, instanceID string, changes ...database.Change) (int64, error) {
|
2025-07-22 19:09:56 +02:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return 0, database.NoChangesError
|
2025-07-14 21:27:14 +02:00
|
|
|
}
|
|
|
|
builder := database.StatementBuilder{}
|
|
|
|
builder.WriteString(`UPDATE zitadel.organizations SET `)
|
|
|
|
|
|
|
|
instanceIDCondition := o.InstanceIDCondition(instanceID)
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
conditions := []database.Condition{id, instanceIDCondition}
|
2025-07-14 21:27:14 +02:00
|
|
|
database.Changes(changes).Write(&builder)
|
|
|
|
writeCondition(&builder, database.And(conditions...))
|
|
|
|
|
|
|
|
stmt := builder.String()
|
|
|
|
|
|
|
|
rowsAffected, err := o.client.Exec(ctx, stmt, builder.Args()...)
|
|
|
|
return rowsAffected, err
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// Delete implements [domain.OrganizationRepository].
|
2025-07-16 18:36:21 +02:00
|
|
|
func (o *org) Delete(ctx context.Context, id domain.OrgIdentifierCondition, instanceID string) (int64, error) {
|
2025-07-14 21:27:14 +02:00
|
|
|
builder := database.StatementBuilder{}
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
builder.WriteString(`DELETE FROM zitadel.organizations`)
|
2025-07-14 21:27:14 +02:00
|
|
|
|
|
|
|
instanceIDCondition := o.InstanceIDCondition(instanceID)
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
conditions := []database.Condition{id, instanceIDCondition}
|
2025-07-14 21:27:14 +02:00
|
|
|
writeCondition(&builder, database.And(conditions...))
|
|
|
|
|
|
|
|
return o.client.Exec(ctx, builder.String(), builder.Args()...)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// changes
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// SetName implements [domain.organizationChanges].
|
|
|
|
func (o org) SetName(name string) database.Change {
|
2025-07-22 19:09:56 +02:00
|
|
|
return database.NewChange(o.NameColumn(false), name)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// SetState implements [domain.organizationChanges].
|
|
|
|
func (o org) SetState(state domain.OrgState) database.Change {
|
2025-07-22 19:09:56 +02:00
|
|
|
return database.NewChange(o.StateColumn(false), state)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// conditions
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// IDCondition implements [domain.organizationConditions].
|
|
|
|
func (o org) IDCondition(id string) domain.OrgIdentifierCondition {
|
2025-07-22 19:09:56 +02:00
|
|
|
return database.NewTextCondition(o.IDColumn(true), database.TextOperationEqual, id)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// NameCondition implements [domain.organizationConditions].
|
|
|
|
func (o org) NameCondition(name string) domain.OrgIdentifierCondition {
|
2025-07-22 19:09:56 +02:00
|
|
|
return database.NewTextCondition(o.NameColumn(true), database.TextOperationEqual, name)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// InstanceIDCondition implements [domain.organizationConditions].
|
|
|
|
func (o org) InstanceIDCondition(instanceID string) database.Condition {
|
2025-07-22 19:09:56 +02:00
|
|
|
return database.NewTextCondition(o.InstanceIDColumn(true), database.TextOperationEqual, instanceID)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// StateCondition implements [domain.organizationConditions].
|
|
|
|
func (o org) StateCondition(state domain.OrgState) database.Condition {
|
2025-07-22 19:09:56 +02:00
|
|
|
return database.NewTextCondition(o.StateColumn(true), database.TextOperationEqual, state)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// columns
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// IDColumn implements [domain.organizationColumns].
|
2025-07-22 19:09:56 +02:00
|
|
|
func (org) IDColumn(qualified bool) database.Column {
|
|
|
|
if qualified {
|
|
|
|
return database.NewColumn("organizations.id")
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
return database.NewColumn("id")
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// NameColumn implements [domain.organizationColumns].
|
2025-07-22 19:09:56 +02:00
|
|
|
func (org) NameColumn(qualified bool) database.Column {
|
|
|
|
if qualified {
|
|
|
|
return database.NewColumn("organizations.name")
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
return database.NewColumn("name")
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// InstanceIDColumn implements [domain.organizationColumns].
|
2025-07-22 19:09:56 +02:00
|
|
|
func (org) InstanceIDColumn(qualified bool) database.Column {
|
|
|
|
if qualified {
|
|
|
|
return database.NewColumn("organizations.instance_id")
|
|
|
|
}
|
2025-05-08 07:42:53 +02:00
|
|
|
return database.NewColumn("instance_id")
|
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// StateColumn implements [domain.organizationColumns].
|
2025-07-22 19:09:56 +02:00
|
|
|
func (org) StateColumn(qualified bool) database.Column {
|
|
|
|
if qualified {
|
|
|
|
return database.NewColumn("organizations.state")
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
return database.NewColumn("state")
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// CreatedAtColumn implements [domain.organizationColumns].
|
2025-07-22 19:09:56 +02:00
|
|
|
func (org) CreatedAtColumn(qualified bool) database.Column {
|
|
|
|
if qualified {
|
|
|
|
return database.NewColumn("organizations.created_at")
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
return database.NewColumn("created_at")
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// UpdatedAtColumn implements [domain.organizationColumns].
|
2025-07-22 19:09:56 +02:00
|
|
|
func (org) UpdatedAtColumn(qualified bool) database.Column {
|
|
|
|
if qualified {
|
|
|
|
return database.NewColumn("organizations.updated_at")
|
|
|
|
}
|
2025-05-08 07:42:53 +02:00
|
|
|
return database.NewColumn("updated_at")
|
|
|
|
}
|
|
|
|
|
2025-07-16 09:26:46 +02:00
|
|
|
// -------------------------------------------------------------
|
|
|
|
// scanners
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
type rawOrganization struct {
|
|
|
|
*domain.Organization
|
|
|
|
RawDomains json.RawMessage `json:"domains,omitempty" db:"domains"`
|
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
func scanOrganization(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*domain.Organization, error) {
|
|
|
|
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
var org rawOrganization
|
|
|
|
if err := rows.(database.CollectableRows).CollectExactlyOneRow(&org); err != nil {
|
2025-07-14 21:27:14 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2025-07-22 19:09:56 +02:00
|
|
|
if len(org.RawDomains) > 0 {
|
|
|
|
if err := json.Unmarshal(org.RawDomains, &org.Domains); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
return org.Organization, nil
|
2025-07-14 21:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func scanOrganizations(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) ([]*domain.Organization, error) {
|
|
|
|
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
var rawOrgs []*rawOrganization
|
|
|
|
if err := rows.(database.CollectableRows).Collect(&rawOrgs); err != nil {
|
2025-07-14 21:27:14 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2025-07-22 19:09:56 +02:00
|
|
|
|
|
|
|
organizations := make([]*domain.Organization, len(rawOrgs))
|
|
|
|
for i, org := range rawOrgs {
|
|
|
|
if len(org.RawDomains) > 0 {
|
|
|
|
if err := json.Unmarshal(org.RawDomains, &org.Domains); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
organizations[i] = org.Organization
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
return organizations, nil
|
|
|
|
}
|
2025-07-16 09:26:46 +02:00
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// sub repositories
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-22 19:09:56 +02:00
|
|
|
// Domains implements [domain.OrganizationRepository].
|
|
|
|
func (o *org) Domains(shouldLoad bool) domain.OrganizationDomainRepository {
|
|
|
|
if !o.shouldLoadDomains {
|
|
|
|
o.shouldLoadDomains = shouldLoad
|
|
|
|
}
|
2025-07-16 09:26:46 +02:00
|
|
|
|
|
|
|
if o.domainRepo != nil {
|
|
|
|
return o.domainRepo
|
|
|
|
}
|
|
|
|
|
|
|
|
o.domainRepo = &orgDomain{
|
|
|
|
repository: o.repository,
|
|
|
|
org: o,
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.domainRepo
|
|
|
|
}
|