2022-10-17 03:02:19 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
|
|
|
ma "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
2024-11-11 18:50:17 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-10-17 03:02:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *AuthHandler) Onboarding(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
key := r.FormValue("key")
|
|
|
|
onboarding, err := h.cache.Get("onboarding/" + key)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-10-30 19:11:36 +00:00
|
|
|
h.Renderer.AuthOnboardingKO(w, r, key)
|
2022-10-17 03:02:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
onboardingmap := onboarding.(map[string]any)
|
|
|
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
if r.FormValue("password") == "" {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Msg("Password is empty")
|
2022-10-17 03:02:19 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := []string{
|
|
|
|
onboardingmap["group"].(string),
|
|
|
|
//onboardingmap["group"].(string) + ":admin",
|
|
|
|
}
|
|
|
|
|
|
|
|
if onboardingmap["admin"].(bool) {
|
|
|
|
groups = append(groups, onboardingmap["group"].(string)+":admin")
|
|
|
|
}
|
2023-02-15 10:22:18 +00:00
|
|
|
display_name := fmt.Sprint(r.FormValue("first_name")) + " " + fmt.Sprint(r.FormValue("last_name"))
|
2022-10-17 03:02:19 +00:00
|
|
|
account := &ma.Account{
|
|
|
|
Authentication: ma.AccountAuth{
|
|
|
|
Local: ma.LocalAuth{
|
|
|
|
Username: onboardingmap["username"].(string),
|
|
|
|
Password: r.FormValue("password"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Namespace: "parcoursmob",
|
2023-02-15 10:22:18 +00:00
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
Data: map[string]any{
|
2023-02-15 10:22:18 +00:00
|
|
|
"display_name": display_name,
|
|
|
|
"first_name": r.FormValue("first_name"),
|
|
|
|
"last_name": r.FormValue("last_name"),
|
|
|
|
"email": onboardingmap["username"],
|
|
|
|
"groups": groups,
|
2022-10-17 03:02:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
acc, err := mobilityaccounts.AccountFromStorageType(account)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-10-17 03:02:19 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &mobilityaccounts.RegisterRequest{
|
|
|
|
Account: acc,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = h.services.GRPC.MobilityAccounts.Register(context.TODO(), request)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-10-17 03:02:19 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-10-30 19:11:36 +00:00
|
|
|
|
|
|
|
err = h.cache.Delete("onboarding/" + key)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-10-30 19:11:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
http.Redirect(w, r, "/app/", http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Renderer.AuthOnboarding(w, r, key, onboarding)
|
|
|
|
}
|