forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.go
70 lines (59 loc) · 1.69 KB
/
rename.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
package teamserver
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/pf-qiu/concourse/v6/atc"
"github.com/pf-qiu/concourse/v6/atc/api/accessor"
)
// RenameTeam allows an authenticated user with authority or admin to rename a team
func (s *Server) RenameTeam(w http.ResponseWriter, r *http.Request) {
logger := s.logger.Session("rename-team")
acc := accessor.GetAccessor(r)
teamName := r.FormValue(":team_name")
if !acc.IsAdmin() && !acc.IsAuthorized(teamName) {
w.WriteHeader(http.StatusForbidden)
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Error("failed-to-read-body", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var rename atc.RenameRequest
err = json.Unmarshal(data, &rename)
if err != nil {
logger.Error("failed-to-unmarshal-body", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var warnings []atc.ConfigWarning
warning := atc.ValidateIdentifier(rename.NewName, "team")
if warning != nil {
warnings = append(warnings, *warning)
}
team, found, err := s.teamFactory.FindTeam(teamName)
if err != nil {
logger.Error("failed-to-get-team", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Info("team-not-found")
w.WriteHeader(http.StatusNotFound)
return
}
err = team.Rename(rename.NewName)
if err != nil {
logger.Error("failed-to-update-team-name", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(atc.SaveConfigResponse{Warnings: warnings})
if err != nil {
s.logger.Error("failed-to-encode-response", err)
w.WriteHeader(http.StatusInternalServerError)
}
}