forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.go
65 lines (53 loc) · 1.28 KB
/
destroy.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
package teamserver
import (
"net/http"
"github.com/pf-qiu/concourse/v6/atc/api/accessor"
)
func (s *Server) DestroyTeam(w http.ResponseWriter, r *http.Request) {
hLog := s.logger.Session("destroy-team")
hLog.Debug("destroying-team")
teamName := r.FormValue(":team_name")
acc := accessor.GetAccessor(r)
if !acc.IsAdmin() {
hLog.Info("requesting-team-is-not-admin")
w.WriteHeader(http.StatusForbidden)
return
}
team, found, err := s.teamFactory.FindTeam(teamName)
if err != nil {
hLog.Error("failed-to-get-team", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
hLog.Info("team-not-found")
w.WriteHeader(http.StatusNotFound)
return
}
if team.Admin() {
allTeams, err := s.teamFactory.GetTeams()
if err != nil {
hLog.Error("failed-to-get-teams", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
adminTeams := 0
for _, candidate := range allTeams {
if candidate.Admin() {
adminTeams = adminTeams + 1
}
}
if adminTeams == 1 {
hLog.Info("team-is-last-admin-team")
w.WriteHeader(http.StatusForbidden)
return
}
}
err = team.Delete()
if err != nil {
hLog.Error("failed-to-delete-team", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}