Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/organization.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package db
6
7
import (
8
"context"
9
"errors"
10
"fmt"
11
"time"
12
13
"github.com/google/uuid"
14
"gorm.io/gorm"
15
)
16
17
type Organization struct {
18
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
19
Name string `gorm:"column:name;type:varchar;size:255;" json:"name"`
20
Slug string `gorm:"column:slug;type:varchar;size:255;" json:"slug"`
21
22
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`
23
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
24
25
MarkedDeleted bool `gorm:"column:markedDeleted;type:tinyint;default:0;" json:"marked_deleted"`
26
}
27
28
// TableName sets the insert table name for this struct type
29
func (d *Organization) TableName() string {
30
return "d_b_team"
31
}
32
33
func CreateOrganization(ctx context.Context, conn *gorm.DB, org Organization) (Organization, error) {
34
if org.ID == uuid.Nil {
35
return Organization{}, errors.New("id must be set")
36
}
37
38
if org.Name == "" {
39
return Organization{}, errors.New("name must be set")
40
}
41
if org.Slug == "" {
42
return Organization{}, errors.New("slug must be set")
43
}
44
45
tx := conn.
46
WithContext(ctx).
47
Create(&org)
48
if tx.Error != nil {
49
return Organization{}, fmt.Errorf("failed to create organization: %w", tx.Error)
50
}
51
52
return org, nil
53
}
54
55
func GetOrganizationBySlug(ctx context.Context, conn *gorm.DB, slug string) (Organization, error) {
56
if slug == "" {
57
return Organization{}, fmt.Errorf("Slug is required")
58
}
59
60
var org Organization
61
62
tx := conn.WithContext(ctx).
63
Where("slug = ?", slug).
64
Find(&org)
65
66
if tx.Error != nil {
67
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
68
return Organization{}, fmt.Errorf("Organization with slug %s does not exist: %w", slug, ErrorNotFound)
69
}
70
return Organization{}, fmt.Errorf("Failed to retrieve organization: %v", tx.Error)
71
}
72
73
return org, nil
74
}
75
76
// GetSingleOrganizationWithActiveSSO returns the single team with SSO enabled.
77
// If there is more than one team with SSO enabled, an error is returned.
78
func GetSingleOrganizationWithActiveSSO(ctx context.Context, conn *gorm.DB) (Organization, error) {
79
var orgs []Organization
80
81
tx := conn.
82
WithContext(ctx).
83
Table(fmt.Sprintf("%s as team", (&Organization{}).TableName())).
84
Joins(fmt.Sprintf("JOIN %s AS config ON team.id = config.organizationId", (&OIDCClientConfig{}).TableName())).
85
Where("config.deleted = ?", 0).
86
Where("config.active = ?", 1).
87
Find(&orgs)
88
89
if tx.Error != nil {
90
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
91
return Organization{}, fmt.Errorf("No single organization found: %w", ErrorNotFound)
92
}
93
return Organization{}, fmt.Errorf("Failed to retrieve organization: %v", tx.Error)
94
}
95
96
if len(orgs) == 0 {
97
return Organization{}, fmt.Errorf("No single organization with active SSO found: %w", ErrorNotFound)
98
}
99
100
if len(orgs) > 1 {
101
return Organization{}, fmt.Errorf("More than one organization with active SSO found: %w", ErrorNotFound)
102
}
103
104
return orgs[0], nil
105
}
106
107