Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/pkg/integration/user.go
2498 views
1
// Copyright (c) 2021 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 integration
6
7
import (
8
"github.com/google/uuid"
9
"golang.org/x/xerrors"
10
)
11
12
func CreateUser(username string, admin bool, api *ComponentAPI) (userId string, err error) {
13
userUUID, err := uuid.NewRandom()
14
if err != nil {
15
return
16
}
17
userId = userUUID.String()
18
19
rolesOrPermissions := "[]"
20
if admin {
21
rolesOrPermissions = `["admin"]`
22
}
23
24
db, err := api.DB()
25
if err != nil {
26
return
27
}
28
29
_, err = db.Exec("INSERT INTO d_b_user (id, creationDate, name, rolesOrPermissions) VALUES (?, NOW(), ?, ?)",
30
userId,
31
username,
32
rolesOrPermissions,
33
)
34
return
35
}
36
37
func DeleteUser(userId string, api *ComponentAPI) (err error) {
38
db, err := api.DB()
39
if err != nil {
40
return
41
}
42
43
_, err = db.Exec("DELETE FROM d_b_user WHERE id = ?", userId)
44
return
45
}
46
47
func IsUserBlocked(userId string, api *ComponentAPI) (blocked bool, err error) {
48
db, err := api.DB()
49
if err != nil {
50
return
51
}
52
53
rows, err := db.Query("SELECT blocked FROM d_b_user WHERE id = ?", userId)
54
if err != nil {
55
return
56
}
57
defer rows.Close()
58
59
if !rows.Next() {
60
return false, xerrors.Errorf("no rows selected - should not happen")
61
}
62
63
err = rows.Scan(&blocked)
64
return
65
}
66
67