Path: blob/main/components/gitpod-db/go/organization.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package db56import (7"context"8"errors"9"fmt"10"time"1112"github.com/google/uuid"13"gorm.io/gorm"14)1516type Organization struct {17ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`18Name string `gorm:"column:name;type:varchar;size:255;" json:"name"`19Slug string `gorm:"column:slug;type:varchar;size:255;" json:"slug"`2021CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`22LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`2324MarkedDeleted bool `gorm:"column:markedDeleted;type:tinyint;default:0;" json:"marked_deleted"`25}2627// TableName sets the insert table name for this struct type28func (d *Organization) TableName() string {29return "d_b_team"30}3132func CreateOrganization(ctx context.Context, conn *gorm.DB, org Organization) (Organization, error) {33if org.ID == uuid.Nil {34return Organization{}, errors.New("id must be set")35}3637if org.Name == "" {38return Organization{}, errors.New("name must be set")39}40if org.Slug == "" {41return Organization{}, errors.New("slug must be set")42}4344tx := conn.45WithContext(ctx).46Create(&org)47if tx.Error != nil {48return Organization{}, fmt.Errorf("failed to create organization: %w", tx.Error)49}5051return org, nil52}5354func GetOrganizationBySlug(ctx context.Context, conn *gorm.DB, slug string) (Organization, error) {55if slug == "" {56return Organization{}, fmt.Errorf("Slug is required")57}5859var org Organization6061tx := conn.WithContext(ctx).62Where("slug = ?", slug).63Find(&org)6465if tx.Error != nil {66if errors.Is(tx.Error, gorm.ErrRecordNotFound) {67return Organization{}, fmt.Errorf("Organization with slug %s does not exist: %w", slug, ErrorNotFound)68}69return Organization{}, fmt.Errorf("Failed to retrieve organization: %v", tx.Error)70}7172return org, nil73}7475// GetSingleOrganizationWithActiveSSO returns the single team with SSO enabled.76// If there is more than one team with SSO enabled, an error is returned.77func GetSingleOrganizationWithActiveSSO(ctx context.Context, conn *gorm.DB) (Organization, error) {78var orgs []Organization7980tx := conn.81WithContext(ctx).82Table(fmt.Sprintf("%s as team", (&Organization{}).TableName())).83Joins(fmt.Sprintf("JOIN %s AS config ON team.id = config.organizationId", (&OIDCClientConfig{}).TableName())).84Where("config.deleted = ?", 0).85Where("config.active = ?", 1).86Find(&orgs)8788if tx.Error != nil {89if errors.Is(tx.Error, gorm.ErrRecordNotFound) {90return Organization{}, fmt.Errorf("No single organization found: %w", ErrorNotFound)91}92return Organization{}, fmt.Errorf("Failed to retrieve organization: %v", tx.Error)93}9495if len(orgs) == 0 {96return Organization{}, fmt.Errorf("No single organization with active SSO found: %w", ErrorNotFound)97}9899if len(orgs) > 1 {100return Organization{}, fmt.Errorf("More than one organization with active SSO found: %w", ErrorNotFound)101}102103return orgs[0], nil104}105106107