-
Notifications
You must be signed in to change notification settings - Fork 6
/
team.go
304 lines (252 loc) · 7.59 KB
/
team.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Copyright 2021 Adevinta
*/
package store
import (
"github.com/adevinta/errors"
"github.com/adevinta/vulcan-api/pkg/api"
"github.com/jinzhu/gorm"
)
// CreateTeam inserts a new team in the database and includes the current user
// as an owner
func (db vulcanitoStore) CreateTeam(team api.Team, ownerEmail string) (*api.Team, error) {
// Start a new transaction
tx := db.Conn.Begin()
if tx.Error != nil {
return nil, db.logError(errors.Database(tx.Error))
}
// Insert the new team
result := tx.Create(&team)
if result.Error != nil {
tx.Rollback()
return nil, db.logError(errors.Create(result.Error))
}
// Retrieve the current user
owner := api.User{}
result = tx.Find(&owner, "email = ?", ownerEmail)
if result.Error != nil {
tx.Rollback()
return nil, db.logError(errors.Database(result.Error))
}
// Assign current user and default owners to team
owners := append(db.defaults["owners"], owner.ID)
err := assignOwnersToTeam(tx, team.ID, owners)
if err != nil {
tx.Rollback()
return nil, db.logError(errors.Database(err))
}
// Commit the transaction
if tx.Commit().Error != nil {
return nil, db.logError(errors.Database(tx.Error))
}
return &team, nil
}
// UpdateTeam updates a team in the database. It verifies if the team exists
// before actually making the update
func (db vulcanitoStore) UpdateTeam(team api.Team) (*api.Team, error) {
findTeam := team
res := db.Conn.Find(&findTeam)
if res.Error != nil {
if db.NotFoundError(res.Error) {
return nil, db.logError(errors.NotFound(res.Error))
}
return nil, db.logError(errors.Database(res.Error))
}
res = db.Conn.Model(&team).Updates(&team)
if res.Error != nil {
return nil, db.logError(errors.Database(res.Error))
}
db.Conn.First(&team)
return &team, nil
}
func (db vulcanitoStore) DeleteTeam(teamID string) error {
tx := db.Conn.Begin()
if tx.Error != nil {
return db.logError(errors.Database(tx.Error))
}
deletedTeam := &api.Team{}
res := tx.Raw("SELECT * FROM teams WHERE id = ? FOR UPDATE", teamID).
Scan(&deletedTeam)
if res.Error != nil {
if db.NotFoundError(res.Error) {
return db.logError(errors.NotFound(res.Error))
}
return db.logError(errors.Database(res.Error))
}
// We are not going to delete Scans and Reports
// Scans belongs to Scans Engine and will be stored there.
// Reports are linked to Scans and therefore should be preserved as well.
// Delete ChecktypeSettings
res = tx.Delete(api.ChecktypeSetting{}, "policy_id in (select p.id from policies p where p.team_id = ?)", teamID)
if res.Error != nil {
tx.Rollback()
return db.logError(errors.Delete(res.Error))
}
// Delete policies
res = tx.Delete(api.Policy{}, "team_id = ?", teamID)
if res.Error != nil {
tx.Rollback()
return db.logError(errors.Delete(res.Error))
}
// Delete asset groups
res = tx.Delete(api.AssetGroup{}, "group_id in (select g.id from groups g where g.team_id =?)", teamID)
if res.Error != nil {
tx.Rollback()
return db.logError(errors.Delete(res.Error))
}
// Delete groups
res = tx.Delete(api.Group{}, "team_id = ?", teamID)
if res.Error != nil {
tx.Rollback()
return db.logError(errors.Delete(res.Error))
}
// Delete programs
res = tx.Delete(api.Program{}, "team_id = ?", teamID)
if res.Error != nil {
tx.Rollback()
return db.logError(errors.Delete(res.Error))
}
// Delete recipients
res = tx.Delete(api.Recipient{}, "team_id = ?", teamID)
if res.Error != nil {
tx.Rollback()
return db.logError(errors.Delete(res.Error))
}
// Push to outbox so distributed tx is processed
err := db.pushToOutbox(tx, opDeleteTeam, *deletedTeam)
if err != nil {
tx.Rollback()
return err
}
// Delete assets
err = db.deleteAllAssetsTX(tx, *deletedTeam)
if err != nil {
tx.Rollback()
return err
}
// Delete team
res = tx.Delete(deletedTeam)
if res.Error != nil {
tx.Rollback()
return db.logError(errors.Delete(res.Error))
}
// Commit the transaction
if tx.Commit().Error != nil {
return db.logError(errors.Database(tx.Error))
}
return nil
}
// FindTeam returns a team filtering by id
func (db vulcanitoStore) FindTeam(teamID string) (*api.Team, error) {
team := &api.Team{ID: teamID}
res := db.Conn.Preload("UserTeam").Preload("UserTeam.User").Find(&team)
if res.Error != nil {
if db.NotFoundError(res.Error) {
return nil, db.logError(errors.NotFound(res.Error))
}
return nil, db.logError(errors.Database(res.Error))
}
return team, nil
}
// FindTeamByName returns a team filtering by name
func (db vulcanitoStore) FindTeamByName(name string) (*api.Team, error) {
team := &api.Team{}
res := db.Conn.Preload("UserTeam").Preload("UserTeam.User").Find(&team, "name = ?", name)
if res.Error != nil {
if db.NotFoundError(res.Error) {
return nil, db.logError(errors.NotFound(res.Error))
}
return nil, db.logError(errors.Database(res.Error))
}
return team, nil
}
// FindTeamByTag returns a team filtering by tag
func (db vulcanitoStore) FindTeamByTag(tag string) (*api.Team, error) {
team := &api.Team{}
res := db.Conn.Preload("UserTeam").Preload("UserTeam.User").Find(&team, "tag = ?", tag)
if res.Error != nil {
if db.NotFoundError(res.Error) {
return nil, db.logError(errors.NotFound(res.Error))
}
return nil, db.logError(errors.Database(res.Error))
}
return team, nil
}
// FindTeamByTags returns all teams with any of the specified tags.
func (db vulcanitoStore) FindTeamsByTags(tags []string) ([]*api.Team, error) {
teams := []*api.Team{}
res := db.Conn.Find(&teams, "tag IN (?)", tags)
if res.Error != nil {
return nil, db.logError(errors.Database(res.Error))
}
return teams, nil
}
// FindTeamByProgram returns the team that the given Program belongs to.
func (db vulcanitoStore) FindTeamByProgram(programID string) (*api.Team, error) {
program := &api.Program{ID: programID}
result := db.Conn.
Preload("Team").
Find(&program)
if result.Error != nil {
return nil, db.logError(errors.Database(result.Error))
}
return program.Team, nil
}
// ListTeams returns all teams
func (db vulcanitoStore) ListTeams() ([]*api.Team, error) {
teams := []*api.Team{}
// Retrieve all teams
result := db.Conn.
Find(&teams)
if result.Error != nil {
return nil, db.logError(errors.Database(result.Error))
}
return teams, nil
}
func (db vulcanitoStore) FindTeamsByUser(userID string) ([]*api.Team, error) {
teams := []*api.Team{}
user := api.User{ID: userID}
result := db.Conn.Find(&user)
if result.Error != nil {
if db.NotFoundError(result.Error) {
return nil, db.logError(errors.NotFound(result.Error))
}
return nil, db.logError(errors.Database(result.Error))
}
// Retrieve all teams
result = db.Conn.
Joins("join user_team on user_team.team_id = teams.id and user_team.user_id = ?", userID).
Find(&teams)
if result.Error != nil {
return nil, db.logError(errors.Database(result.Error))
}
return teams, nil
}
// FindTeamByIDForUser returns the membership information of a user in a team.
func (db vulcanitoStore) FindTeamByIDForUser(ID, userID string) (*api.UserTeam, error) {
teamUser := &api.UserTeam{}
res := db.Conn.Preload("User").Find(teamUser, "team_id = ? and user_id = ?", ID, userID)
if res.Error != nil {
if db.NotFoundError(res.Error) {
return nil, errors.NotFound(res.Error)
}
return nil, db.logError(errors.Database(res.Error))
}
return teamUser, nil
}
func assignOwnersToTeam(tx *gorm.DB, teamID string, owners []string) error {
for _, userID := range owners {
// Define the association between Team and Owner
teamUser := api.UserTeam{
TeamID: teamID,
UserID: userID,
Role: api.Owner,
}
// Persist the association
result := tx.Create(&teamUser)
if result.Error != nil {
return result.Error
}
}
return nil
}