2024-07-06 19:33:16 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/gorilla/context"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2024-07-07 19:12:21 +02:00
|
|
|
func setOption(w http.ResponseWriter, r *http.Request) {
|
|
|
|
currentUser := context.Get(r, "user").(*db.User)
|
|
|
|
|
|
|
|
if !currentUser.Admin {
|
|
|
|
helpers.WriteJSON(w, http.StatusForbidden, map[string]string{
|
|
|
|
"error": "User must be admin",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var option db.Option
|
|
|
|
if !helpers.Bind(w, r, &option) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := helpers.Store(r).SetOption(option.Key, option.Value)
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteJSON(w, http.StatusInternalServerError, map[string]string{
|
|
|
|
"error": "Can not set option",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, option)
|
|
|
|
}
|
|
|
|
|
2024-07-06 19:33:16 +02:00
|
|
|
func getOptions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
currentUser := context.Get(r, "user").(*db.User)
|
|
|
|
|
|
|
|
if !currentUser.Admin {
|
|
|
|
helpers.WriteJSON(w, http.StatusForbidden, map[string]string{
|
|
|
|
"error": "User must be admin",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2024-07-07 19:12:21 +02:00
|
|
|
|
|
|
|
options, err := helpers.Store(r).GetOptions(db.RetrieveQueryParams{})
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteJSON(w, http.StatusInternalServerError, map[string]string{
|
|
|
|
"error": "Can not get options",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, options)
|
2024-07-06 19:33:16 +02:00
|
|
|
}
|