Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/organization_membership.go
2497 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 OrganizationMembership struct {
18
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
19
20
OrganizationID uuid.UUID `gorm:"column:teamId;type:char;size:36;" json:"teamId"`
21
UserID uuid.UUID `gorm:"column:userId;type:char;size:36;" json:"userId"`
22
Role OrganizationMembershipRole `gorm:"column:role;type:varchar;size:255;" json:"role"`
23
24
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`
25
// Read-only (-> property).
26
LastModified time.Time `gorm:"->:column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
27
}
28
29
// TableName sets the insert table name for this struct type
30
func (d *OrganizationMembership) TableName() string {
31
return "d_b_team_membership"
32
}
33
34
type OrganizationMembershipRole string
35
36
const (
37
OrganizationMembershipRole_Owner = OrganizationMembershipRole("owner")
38
OrganizationMembershipRole_Member = OrganizationMembershipRole("member")
39
)
40
41
func GetOrganizationMembership(ctx context.Context, conn *gorm.DB, userID, orgID uuid.UUID) (OrganizationMembership, error) {
42
if userID == uuid.Nil {
43
return OrganizationMembership{}, errors.New("user ID must not be empty")
44
}
45
46
if orgID == uuid.Nil {
47
return OrganizationMembership{}, errors.New("Organization ID must not be empty")
48
}
49
50
var membership OrganizationMembership
51
tx := conn.WithContext(ctx).
52
Where("userId = ?", userID.String()).
53
Where("teamId = ?", orgID.String()).
54
First(&membership)
55
if tx.Error != nil {
56
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
57
return OrganizationMembership{}, fmt.Errorf("no membership record for user %s and organization %s exists: %w", userID.String(), orgID.String(), ErrorNotFound)
58
}
59
return OrganizationMembership{}, fmt.Errorf("failed to retrieve organization membership for user %s, organization %s: %w", userID.String(), orgID.String(), tx.Error)
60
}
61
62
return membership, nil
63
}
64
65
func DeleteOrganizationMembership(ctx context.Context, conn *gorm.DB, userID uuid.UUID, orgID uuid.UUID) error {
66
if userID == uuid.Nil {
67
return errors.New("user ID must not be empty")
68
}
69
70
if orgID == uuid.Nil {
71
return errors.New("organization ID must not be empty")
72
}
73
74
tx := conn.WithContext(ctx).
75
Model(&OrganizationMembership{}).
76
Where("userId = ?", userID.String()).
77
Where("teamId = ?", orgID.String()).
78
Delete(&OrganizationMembership{})
79
if tx.Error != nil {
80
return fmt.Errorf("failed to retrieve organization membership for user %s, organization %s: %w", userID.String(), orgID.String(), tx.Error)
81
}
82
if tx.RowsAffected == 0 {
83
return fmt.Errorf("no membership record for user %s and organization %s exists: %w", userID.String(), orgID.String(), ErrorNotFound)
84
}
85
86
return nil
87
}
88
89