Emailing management, administration improvement, Carpooling in multimodal search
This commit is contained in:
@@ -2,16 +2,31 @@ package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
||||
groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
type GroupsByName []groupstorage.Group
|
||||
|
||||
func (a GroupsByName) Len() int { return len(a) }
|
||||
func (a GroupsByName) Less(i, j int) bool {
|
||||
return strings.Compare(a[i].Data["name"].(string), a[j].Data["name"].(string)) < 0
|
||||
}
|
||||
func (a GroupsByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
||||
func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
request := &groupsmanagement.GetGroupsRequest{
|
||||
@@ -25,13 +40,15 @@ func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Reque
|
||||
return
|
||||
}
|
||||
|
||||
var groups = []any{}
|
||||
var groups = []groupstorage.Group{}
|
||||
|
||||
for _, group := range resp.Groups {
|
||||
g := group.ToStorageType()
|
||||
groups = append(groups, g)
|
||||
}
|
||||
|
||||
sort.Sort(GroupsByName(groups))
|
||||
|
||||
h.Renderer.Administration(w, r, groups)
|
||||
}
|
||||
|
||||
@@ -151,16 +168,16 @@ func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWrite
|
||||
vars := mux.Vars(r)
|
||||
groupid := vars["groupid"]
|
||||
|
||||
// groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(&groupsmanagement.GetGroupRequest{
|
||||
// Id: groupid,
|
||||
// Namespace: "parcoursmob_organizations",
|
||||
// })
|
||||
groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
|
||||
Id: groupid,
|
||||
Namespace: "parcoursmob_organizations",
|
||||
})
|
||||
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// w.WriteHeader(http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
r.ParseForm()
|
||||
|
||||
@@ -169,9 +186,8 @@ func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWrite
|
||||
Namespace: "parcoursmob",
|
||||
})
|
||||
|
||||
fmt.Println(err)
|
||||
|
||||
if err == nil {
|
||||
// Account already exists : adding the existing account to admin list
|
||||
account := accountresp.Account.ToStorageType()
|
||||
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid)
|
||||
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid+":admin")
|
||||
@@ -187,8 +203,44 @@ func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWrite
|
||||
|
||||
fmt.Println(err)
|
||||
|
||||
data := map[string]any{
|
||||
"group": groupresp.Group.ToStorageType().Data["name"],
|
||||
}
|
||||
|
||||
if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
|
||||
return
|
||||
} else {
|
||||
// Onboard now administrator
|
||||
onboarding := map[string]any{
|
||||
"username": r.FormValue("username"),
|
||||
"group": groupid,
|
||||
"admin": true,
|
||||
}
|
||||
|
||||
b := make([]byte, 16)
|
||||
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
key := base64.RawURLEncoding.EncodeToString(b)
|
||||
|
||||
h.cache.PutWithTTL("onboarding/"+key, onboarding, 72*time.Hour)
|
||||
|
||||
data := map[string]any{
|
||||
"group": groupresp.Group.ToStorageType().Data["name"],
|
||||
"key": key,
|
||||
}
|
||||
|
||||
if err := h.emailing.Send("onboarding.new_administrator", r.FormValue("username"), data); err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
|
||||
|
||||
@@ -37,7 +37,7 @@ type EventsForm struct {
|
||||
Enddate *time.Time `json:"enddate"`
|
||||
Starttime string `json:"starttime"`
|
||||
Endtime string `json:"endtime"`
|
||||
MaxSubscribers int `json:"max_subscribers" validate:"required"`
|
||||
MaxSubscribers int `json:"max_subscribers"`
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) AgendaHome(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -159,10 +159,15 @@ func (h *ApplicationHandler) AgendaDisplayEvent(w http.ResponseWriter, r *http.R
|
||||
|
||||
subscribers := map[string]any{}
|
||||
|
||||
accids := []string{}
|
||||
for _, v := range resp.Event.Subscriptions {
|
||||
accids = append(accids, v.Subscriber)
|
||||
}
|
||||
|
||||
subscriberresp, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(
|
||||
context.TODO(),
|
||||
&mobilityaccounts.GetAccountsBatchRequest{
|
||||
Accountids: resp.Event.Subscribers,
|
||||
Accountids: accids,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -183,7 +188,7 @@ func (h *ApplicationHandler) AgendaDisplayEvent(w http.ResponseWriter, r *http.R
|
||||
|
||||
accountids := []string{}
|
||||
for _, m := range group.Members {
|
||||
if !contains(resp.Event.Subscribers, m) {
|
||||
if !contains(resp.Event.Subscriptions, m) {
|
||||
accountids = append(accountids, m)
|
||||
}
|
||||
}
|
||||
@@ -292,11 +297,20 @@ func parseEventsForm(r *http.Request) (*EventsForm, error) {
|
||||
return formData, nil
|
||||
}
|
||||
|
||||
func contains[V string](s []V, e V) bool {
|
||||
func contains(s []*agenda.Subscription, e string) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
if a.Subscriber == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// func contains[V string](s []V, e V) bool {
|
||||
// for _, a := range s {
|
||||
// if a == e {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/renderer"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/services"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/cache"
|
||||
"git.coopgo.io/coopgo-platform/emailing"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -14,9 +15,10 @@ type ApplicationHandler struct {
|
||||
Renderer *renderer.Renderer
|
||||
services *services.ServicesHandler
|
||||
cache *cache.CacheHandler
|
||||
emailing *emailing.Mailer
|
||||
}
|
||||
|
||||
func NewApplicationHandler(cfg *viper.Viper, svc *services.ServicesHandler, cache *cache.CacheHandler) (*ApplicationHandler, error) {
|
||||
func NewApplicationHandler(cfg *viper.Viper, svc *services.ServicesHandler, cache *cache.CacheHandler, emailing *emailing.Mailer) (*ApplicationHandler, error) {
|
||||
templates_root := cfg.GetString("templates.root")
|
||||
renderer := renderer.NewRenderer(cfg, templates_root)
|
||||
return &ApplicationHandler{
|
||||
@@ -24,6 +26,7 @@ func NewApplicationHandler(cfg *viper.Viper, svc *services.ServicesHandler, cach
|
||||
Renderer: renderer,
|
||||
services: svc,
|
||||
cache: cache,
|
||||
emailing: emailing,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ func (h *ApplicationHandler) Dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// We only display the 10 last here
|
||||
count := len(resp.Accounts)
|
||||
min := count - 10
|
||||
min := count - 5
|
||||
if min < 0 {
|
||||
min = 0
|
||||
}
|
||||
|
||||
7
handlers/application/directory.go
Normal file
7
handlers/application/directory.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package application
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (h *ApplicationHandler) DirectoryHome(w http.ResponseWriter, r *http.Request) {
|
||||
h.Renderer.DirectoryHome(w, r)
|
||||
}
|
||||
128
handlers/application/group.go
Normal file
128
handlers/application/group.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||
)
|
||||
|
||||
func (h *ApplicationHandler) GroupSettingsDisplay(w http.ResponseWriter, r *http.Request) {
|
||||
g := r.Context().Value(identification.GroupKey)
|
||||
if g == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
group := g.(storage.Group)
|
||||
|
||||
members, err := h.members()
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
admins := []any{}
|
||||
groupMembers := []any{}
|
||||
|
||||
for _, m := range members {
|
||||
mm := m.ToStorageType()
|
||||
for _, g := range mm.Data["groups"].([]any) {
|
||||
if g.(string) == group.ID {
|
||||
groupMembers = append(groupMembers, mm)
|
||||
}
|
||||
if g.(string) == group.ID+":admin" {
|
||||
admins = append(admins, mm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.Renderer.GroupSettingsDisplay(w, r, group, groupMembers, admins)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) GroupSettingsInviteMember(w http.ResponseWriter, r *http.Request) {
|
||||
g := r.Context().Value(identification.GroupKey)
|
||||
if g == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
group := g.(storage.Group)
|
||||
|
||||
r.ParseForm()
|
||||
|
||||
accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
|
||||
Username: r.FormValue("username"),
|
||||
Namespace: "parcoursmob",
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
// Account already exists : adding the existing account to admin list
|
||||
account := accountresp.Account.ToStorageType()
|
||||
//account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid)
|
||||
account.Data["groups"] = append(account.Data["groups"].([]any), group.ID)
|
||||
|
||||
as, _ := accounts.AccountFromStorageType(&account)
|
||||
|
||||
_, err = h.services.GRPC.MobilityAccounts.UpdateData(
|
||||
context.TODO(),
|
||||
&accounts.UpdateDataRequest{
|
||||
Account: as,
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println(err)
|
||||
|
||||
data := map[string]any{
|
||||
"group": group.Data["name"],
|
||||
}
|
||||
|
||||
if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/app/group/settings", http.StatusFound)
|
||||
return
|
||||
} else {
|
||||
// Onboard now administrator
|
||||
onboarding := map[string]any{
|
||||
"username": r.FormValue("username"),
|
||||
"group": group.ID,
|
||||
"admin": false,
|
||||
}
|
||||
|
||||
b := make([]byte, 16)
|
||||
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
key := base64.RawURLEncoding.EncodeToString(b)
|
||||
|
||||
h.cache.PutWithTTL("onboarding/"+key, onboarding, 72*time.Hour)
|
||||
|
||||
data := map[string]any{
|
||||
"group": group.Data["name"],
|
||||
"key": key,
|
||||
}
|
||||
|
||||
if err := h.emailing.Send("onboarding.new_member", r.FormValue("username"), data); err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/app/group/settings", http.StatusFound)
|
||||
return
|
||||
}
|
||||
141
handlers/application/journeys.go
Normal file
141
handlers/application/journeys.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
|
||||
geojson "github.com/paulmach/go.geojson"
|
||||
"gitlab.scity.coop/maas/navitia-golang"
|
||||
"gitlab.scity.coop/maas/navitia-golang/types"
|
||||
)
|
||||
|
||||
func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
locTime, errTime := time.LoadLocation("Europe/Paris")
|
||||
if errTime != nil {
|
||||
fmt.Println("Loading timezone location Europe/Paris error : ")
|
||||
fmt.Println("Missing zones in container ? ")
|
||||
panic(errTime)
|
||||
}
|
||||
|
||||
departuredate := r.FormValue("departuredate")
|
||||
departuretime := r.FormValue("departuretime")
|
||||
departuredatetime, _ := time.ParseInLocation("2006-01-02 15:04", fmt.Sprintf("%s %s", departuredate, departuretime), locTime)
|
||||
fmt.Println(departuredatetime)
|
||||
|
||||
departure := r.FormValue("departure")
|
||||
destination := r.FormValue("destination")
|
||||
|
||||
searched := false
|
||||
|
||||
var (
|
||||
departuregeo *geojson.Feature
|
||||
destinationgeo *geojson.Feature
|
||||
journeys *navitia.JourneyResults
|
||||
carpoolresults any
|
||||
vehicles = []any{}
|
||||
)
|
||||
|
||||
if departuredate != "" && departuretime != "" && departure != "" && destination != "" {
|
||||
searched = true
|
||||
|
||||
var err error
|
||||
|
||||
departuregeo, err = geojson.UnmarshalFeature([]byte(departure))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
destinationgeo, err = geojson.UnmarshalFeature([]byte(destination))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
//TODO make it a library
|
||||
session, _ := navitia.NewCustom(
|
||||
h.config.GetString("services.navitia.api_key"),
|
||||
"https://api.navitia.io/v1",
|
||||
&http.Client{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println(session)
|
||||
request := navitia.JourneyRequest{
|
||||
From: types.ID(fmt.Sprintf("%f", departuregeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", departuregeo.Geometry.Point[1])),
|
||||
To: types.ID(fmt.Sprintf("%f", destinationgeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", destinationgeo.Geometry.Point[1])),
|
||||
Date: departuredatetime.Add(-2 * time.Hour),
|
||||
DateIsArrival: false, //TODO
|
||||
}
|
||||
|
||||
journeys, err = session.Journeys(context.Background(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
//CARPOOL
|
||||
// carpoolrequest := fmt.Sprintf(
|
||||
// "https://api.rdex.ridygo.fr/journeys.json?p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
|
||||
// departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
|
||||
// destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
|
||||
// departuredatetime.Format("2006-01-02"), departuredatetime.Add(24*time.Hour).Format("2006-01-02"))
|
||||
carpoolrequest := "https://api.rdex.ridygo.fr/journeys.json"
|
||||
|
||||
fmt.Println(carpoolrequest)
|
||||
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", carpoolrequest, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
req.URL.RawQuery = fmt.Sprintf(
|
||||
"p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
|
||||
departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
|
||||
destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
|
||||
departuredatetime.Format("2006-01-02"), departuredatetime.Format("2006-01-02"))
|
||||
|
||||
req.Header.Set("X-API-KEY", "123456")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&carpoolresults)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// Vehicles
|
||||
|
||||
vehiclerequest := &fleets.GetVehiclesRequest{
|
||||
Namespaces: []string{"parcoursmob"},
|
||||
}
|
||||
vehicleresp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), vehiclerequest)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, vehicle := range vehicleresp.Vehicles {
|
||||
v := vehicle.ToStorageType()
|
||||
if v.Free(departuredatetime.Add(-24*time.Hour), departuredatetime.Add(168*time.Hour)) {
|
||||
vehicles = append(vehicles, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.Renderer.JourneysSearch(w, r, carpoolresults, journeys, vehicles, searched, departuregeo, destinationgeo, departuredate, departuretime)
|
||||
}
|
||||
29
handlers/auth/auth.go
Normal file
29
handlers/auth/auth.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/renderer"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/services"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/cache"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
idp *identification.IdentificationProvider
|
||||
config *viper.Viper
|
||||
services *services.ServicesHandler
|
||||
Renderer *renderer.Renderer
|
||||
cache *cache.CacheHandler
|
||||
}
|
||||
|
||||
func NewAuthHandler(cfg *viper.Viper, idp *identification.IdentificationProvider, svc *services.ServicesHandler, cache *cache.CacheHandler) (*AuthHandler, error) {
|
||||
templates_root := cfg.GetString("templates.root")
|
||||
renderer := renderer.NewRenderer(cfg, templates_root)
|
||||
return &AuthHandler{
|
||||
idp: idp,
|
||||
config: cfg,
|
||||
services: svc,
|
||||
Renderer: renderer,
|
||||
cache: cache,
|
||||
}, nil
|
||||
}
|
||||
@@ -5,34 +5,9 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/renderer"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/services"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/cache"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
idp *identification.IdentificationProvider
|
||||
config *viper.Viper
|
||||
services *services.ServicesHandler
|
||||
Renderer *renderer.Renderer
|
||||
cache *cache.CacheHandler
|
||||
}
|
||||
|
||||
func NewAuthHandler(cfg *viper.Viper, idp *identification.IdentificationProvider, svc *services.ServicesHandler, cache *cache.CacheHandler) (*AuthHandler, error) {
|
||||
templates_root := cfg.GetString("templates.root")
|
||||
renderer := renderer.NewRenderer(cfg, templates_root)
|
||||
return &AuthHandler{
|
||||
idp: idp,
|
||||
config: cfg,
|
||||
services: svc,
|
||||
Renderer: renderer,
|
||||
cache: cache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Groups(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := h.idp.SessionsStore.Get(r, "parcoursmob_session")
|
||||
|
||||
|
||||
79
handlers/auth/onboarding.go
Normal file
79
handlers/auth/onboarding.go
Normal file
@@ -0,0 +1,79 @@
|
||||
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"
|
||||
)
|
||||
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
onboardingmap := onboarding.(map[string]any)
|
||||
|
||||
if r.Method == "POST" {
|
||||
if r.FormValue("password") == "" {
|
||||
fmt.Println("password is empty !")
|
||||
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")
|
||||
}
|
||||
|
||||
account := &ma.Account{
|
||||
Authentication: ma.AccountAuth{
|
||||
Local: ma.LocalAuth{
|
||||
Username: onboardingmap["username"].(string),
|
||||
Password: r.FormValue("password"),
|
||||
},
|
||||
},
|
||||
Namespace: "parcoursmob",
|
||||
Data: map[string]any{
|
||||
"first_name": r.FormValue("first_name"),
|
||||
"last_name": r.FormValue("last_name"),
|
||||
"email": onboardingmap["username"],
|
||||
"groups": groups,
|
||||
},
|
||||
}
|
||||
|
||||
acc, err := mobilityaccounts.AccountFromStorageType(account)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
request := &mobilityaccounts.RegisterRequest{
|
||||
Account: acc,
|
||||
}
|
||||
|
||||
_, err = h.services.GRPC.MobilityAccounts.Register(context.TODO(), request)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
}
|
||||
|
||||
h.Renderer.AuthOnboarding(w, r, key, onboarding)
|
||||
}
|
||||
Reference in New Issue
Block a user