Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/user.go
2497 views
1
// Copyright (c) 2023 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
12
"github.com/google/uuid"
13
"gorm.io/gorm"
14
)
15
16
type User struct {
17
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;"`
18
19
// If the User is exclusively owned by an Organization, the owning Organization will be set.
20
OrganizationID *uuid.UUID `gorm:"column:organizationId;type:char;size:255;"`
21
UsageAttributionID AttributionID `gorm:"column:usageAttributionId;type:char;size:60;default:'';not null;"`
22
23
Name string `gorm:"column:name;type:char;size:255;"`
24
FullName string `gorm:"column:fullName;type:char;size:255;"`
25
AvatarURL string `gorm:"column:avatarUrl;type:char;size:255;"`
26
27
Blocked bool `gorm:"column:blocked;type:tinyint;default:0;not null;" json:"blocked"`
28
Privileged bool `gorm:"column:privileged;type:tinyint;default:0;not null;" json:"privileged"`
29
30
RolesOrPermissions *string `gorm:"column:rolesOrPermissions;type:text;"`
31
FeatureFlags *string `gorm:"column:featureFlags;type:text;"`
32
33
VerificationPhoneNumber string `gorm:"column:verificationPhoneNumber;type:char;size:30;default:'';not null;"`
34
LastVerificationTime VarcharTime `gorm:"column:lastVerificationTime;type:char;size:30;default:'';not null;"`
35
36
AdditionalData *string `gorm:"column:additionalData;type:text;"`
37
38
// Identities can be loaded with Preload("Identities") from the `d_b_identity` table as
39
// a One to Many relationship
40
Identities []Identity `gorm:"foreignKey:userId;references:id"`
41
42
CreationDate VarcharTime `gorm:"column:creationDate;type:varchar;size:255;"`
43
44
MarkedDeleted bool `gorm:"column:markedDeleted;type:tinyint;default:0;" json:"markedDeleted"`
45
46
// More undefined fields
47
}
48
49
func (user *User) TableName() string {
50
return "d_b_user"
51
}
52
53
func GetUser(ctx context.Context, conn *gorm.DB, id uuid.UUID) (User, error) {
54
if id == uuid.Nil {
55
return User{}, errors.New("id must be nil")
56
}
57
58
var user User
59
tx := conn.
60
Model(&User{}).
61
Preload("Identities").
62
First(&user, id)
63
64
if tx.Error != nil {
65
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
66
return User{}, fmt.Errorf("user with ID %s does not exist: %w", id, ErrorNotFound)
67
}
68
69
return User{}, fmt.Errorf("Failed to retrieve user: %v", tx.Error)
70
}
71
72
return user, nil
73
}
74
75