forked from semaphoreui/semaphore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
160 lines (125 loc) · 3.65 KB
/
users.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
package api
import (
log "github.com/Sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
"net/http"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gorilla/context"
)
func getUsers(w http.ResponseWriter, r *http.Request) {
users, err := helpers.Store(r).GetUsers(db.RetrieveQueryParams{})
if err != nil {
panic(err)
}
helpers.WriteJSON(w, http.StatusOK, users)
}
func addUser(w http.ResponseWriter, r *http.Request) {
var user db.UserWithPwd
if !helpers.Bind(w, r, &user) {
return
}
editor := context.Get(r, "user").(*db.User)
if !editor.Admin {
log.Warn(editor.Username + " is not permitted to create users")
w.WriteHeader(http.StatusUnauthorized)
return
}
newUser, err := helpers.Store(r).CreateUser(user)
if err != nil {
log.Warn(editor.Username + " is not created: " + err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
helpers.WriteJSON(w, http.StatusCreated, newUser)
}
func getUserMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userID, err := helpers.GetIntParam("user_id", w, r)
if err != nil {
return
}
user, err := helpers.Store(r).GetUser(userID)
if err != nil {
helpers.WriteError(w, err)
return
}
editor := context.Get(r, "user").(*db.User)
if !editor.Admin && editor.ID != user.ID {
log.Warn(editor.Username + " is not permitted to edit users")
w.WriteHeader(http.StatusUnauthorized)
return
}
context.Set(r, "_user", user)
next.ServeHTTP(w, r)
})
}
func updateUser(w http.ResponseWriter, r *http.Request) {
oldUser := context.Get(r, "_user").(db.User)
editor := context.Get(r, "user").(*db.User)
var user db.UserWithPwd
if !helpers.Bind(w, r, &user) {
return
}
if !editor.Admin && editor.ID != oldUser.ID {
log.Warn(editor.Username + " is not permitted to edit users")
w.WriteHeader(http.StatusUnauthorized)
return
}
if editor.ID == oldUser.ID && oldUser.Admin != user.Admin {
log.Warn("User can't edit his own role")
w.WriteHeader(http.StatusUnauthorized)
return
}
if oldUser.External && oldUser.Username != user.Username {
log.Warn("Username is not editable for external LDAP users")
w.WriteHeader(http.StatusBadRequest)
return
}
user.ID = oldUser.ID
if err := helpers.Store(r).UpdateUser(user); err != nil {
log.Error(err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}
func updateUserPassword(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "_user").(db.User)
editor := context.Get(r, "user").(*db.User)
var pwd struct {
Pwd string `json:"password"`
}
if !editor.Admin && editor.ID != user.ID {
log.Warn(editor.Username + " is not permitted to edit users")
w.WriteHeader(http.StatusUnauthorized)
return
}
if user.External {
log.Warn("Password is not editable for external LDAP users")
w.WriteHeader(http.StatusBadRequest)
return
}
if !helpers.Bind(w, r, &pwd) {
return
}
if err := helpers.Store(r).SetUserPassword(user.ID, pwd.Pwd); err != nil {
util.LogWarning(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func deleteUser(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "_user").(db.User)
editor := context.Get(r, "user").(*db.User)
if !editor.Admin && editor.ID != user.ID {
log.Warn(editor.Username + " is not permitted to delete users")
w.WriteHeader(http.StatusUnauthorized)
return
}
if err := helpers.Store(r).DeleteUser(user.ID); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
w.WriteHeader(http.StatusNoContent)
}