-
Notifications
You must be signed in to change notification settings - Fork 0
/
public.go
64 lines (51 loc) · 1.58 KB
/
public.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package group
import (
"database/sql"
"github.com/ovh/cds/engine/api/permission"
)
// SharedInfraGroup is the name of the builtin group used to share infrastructure between projects
const SharedInfraGroup = "shared.infra"
// CreateDefaultGlobalGroup creates a group 'public' where every user will be
func CreateDefaultGlobalGroup(db *sql.DB) error {
query := `SELECT id FROM "group" where name = $1`
var id int64
err := db.QueryRow(query, SharedInfraGroup).Scan(&id)
if err == sql.ErrNoRows {
query = `INSERT INTO "group" (name) VALUES ($1)`
_, err = db.Exec(query, SharedInfraGroup)
if err != nil {
return err
}
}
return nil
}
// AddAdminInGlobalGroup insert into new admin into global group as group admin
func AddAdminInGlobalGroup(db *sql.DB, userID int64) error {
query := `SELECT id FROM "group" where name = $1`
var id int64
err := db.QueryRow(query, SharedInfraGroup).Scan(&id)
if err != nil {
return err
}
query = `INSERT INTO group_user (group_id, user_id, group_admin) VALUES ($1, $2, true)`
_, err = db.Exec(query, id, userID)
if err != nil {
return err
}
return nil
}
// AddGlobalGroupToPipeline add global group access to given pipeline
func AddGlobalGroupToPipeline(tx *sql.Tx, pipID int64) error {
query := `SELECT id FROM "group" where name = $1`
var id int64
err := tx.QueryRow(query, SharedInfraGroup).Scan(&id)
if err != nil {
return err
}
query = `INSERT INTO pipeline_group (pipeline_id, group_id, role) VALUES ($1, $2, $3)`
_, err = tx.Exec(query, pipID, id, permission.PermissionReadExecute)
if err != nil {
return err
}
return nil
}