83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.zaynet.fr/DoGaMa/DoGaMa-serv/internal/instance"
|
|
"git.zaynet.fr/DoGaMa/DoGaMa-serv/internal/webaccess"
|
|
)
|
|
|
|
func (s *server) gameContainerRuntimeForm(w http.ResponseWriter, r *http.Request) {
|
|
if !s.parseForm(w, r) {
|
|
return
|
|
}
|
|
user, err := s.currentUser(r)
|
|
session, cookieErr := r.Cookie(sessionCookie)
|
|
if err != nil || cookieErr != nil || user.Role != "admin" || !s.auth.ValidateCSRF(r.Context(), session.Value, r.FormValue("csrf_token")) {
|
|
s.problem(w, http.StatusForbidden, localized(s.language(r, ""), "error.csrf"))
|
|
return
|
|
}
|
|
if err := s.permissions.RequireRecentAdmin(user); err != nil {
|
|
s.problem(w, http.StatusForbidden, "Recent administrator authentication is required.")
|
|
return
|
|
}
|
|
parse := func(name string) (uint32, error) {
|
|
value, err := instance.ParseRuntimeID(r.FormValue(name))
|
|
if err != nil {
|
|
return 0, fmt.Errorf("%s: %w", name, err)
|
|
}
|
|
return value, nil
|
|
}
|
|
uid, err := parse("uid")
|
|
if err != nil {
|
|
s.problem(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
gid, err := parse("gid")
|
|
if err != nil {
|
|
s.problem(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
repository, ok := s.repository.(interface {
|
|
SetGameContainerRuntimeIdentity(context.Context, instance.RuntimeIdentity) error
|
|
})
|
|
if !ok {
|
|
s.problem(w, http.StatusServiceUnavailable, localized(s.language(r, user.Language), "error.internal"))
|
|
return
|
|
}
|
|
if err := repository.SetGameContainerRuntimeIdentity(r.Context(), instance.RuntimeIdentity{UID: uid, GID: gid}); err != nil {
|
|
s.problem(w, http.StatusInternalServerError, localized(s.language(r, user.Language), "error.internal"))
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/administration#game-runtime", http.StatusSeeOther)
|
|
}
|
|
|
|
func (s *server) webAccessForm(w http.ResponseWriter, r *http.Request) {
|
|
if !s.parseForm(w, r) {
|
|
return
|
|
}
|
|
user, err := s.currentUser(r)
|
|
session, cookieErr := r.Cookie(sessionCookie)
|
|
if err != nil || cookieErr != nil || user.Role != "admin" || !s.auth.ValidateCSRF(r.Context(), session.Value, r.FormValue("csrf_token")) {
|
|
s.problem(w, http.StatusForbidden, localized(s.language(r, ""), "error.csrf"))
|
|
return
|
|
}
|
|
repository, ok := s.repository.(webaccess.Repository)
|
|
if !ok {
|
|
s.problem(w, http.StatusServiceUnavailable, localized(s.language(r, user.Language), "error.internal"))
|
|
return
|
|
}
|
|
policy, err := (webaccess.Policy{RequireHTTPS: r.FormValue("require_https") == "on", CanonicalURL: r.FormValue("canonical_url")}).Validate()
|
|
if err != nil {
|
|
s.problem(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
if err := repository.SetWebAccessPolicy(r.Context(), policy); err != nil {
|
|
s.problem(w, http.StatusInternalServerError, localized(s.language(r, user.Language), "error.internal"))
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/administration#web-access", http.StatusSeeOther)
|
|
}
|